blob: 63cf6cd174d161fd1f9cb39e3efdd62234be132c [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 Masona52d9a82007-08-27 16:49:44 -04006949again:
Chris Mason890871b2009-09-02 16:24:52 -04006950 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006951 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006952 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006953 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006954 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006955
Chris Masona52d9a82007-08-27 16:49:44 -04006956 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006957 if (em->start > start || em->start + em->len <= start)
6958 free_extent_map(em);
6959 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006960 free_extent_map(em);
6961 else
6962 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006963 }
David Sterba172ddd62011-04-21 00:48:27 +02006964 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006965 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006966 err = -ENOMEM;
6967 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006968 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006969 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006970 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006971 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006972 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006973 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006974
6975 if (!path) {
6976 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006977 if (!path) {
6978 err = -ENOMEM;
6979 goto out;
6980 }
6981 /*
6982 * Chances are we'll be called again, so go ahead and do
6983 * readahead
6984 */
David Sterbae4058b52015-11-27 16:31:35 +01006985 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006986 }
6987
Chris Mason179e29e2007-11-01 11:28:41 -04006988 ret = btrfs_lookup_file_extent(trans, root, path,
6989 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006990 if (ret < 0) {
6991 err = ret;
6992 goto out;
6993 }
6994
6995 if (ret != 0) {
6996 if (path->slots[0] == 0)
6997 goto not_found;
6998 path->slots[0]--;
6999 }
7000
Chris Mason5f39d392007-10-15 16:14:19 -04007001 leaf = path->nodes[0];
7002 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007003 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04007004 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04007005 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02007006 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04007007 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04007008 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007009 /*
7010 * If we backup past the first extent we want to move forward
7011 * and see if there is an extent in front of us, otherwise we'll
7012 * say there is a hole for our whole search range which can
7013 * cause problems.
7014 */
7015 extent_end = start;
7016 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007017 }
7018
Chris Mason5f39d392007-10-15 16:14:19 -04007019 found_type = btrfs_file_extent_type(leaf, item);
7020 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007021 if (found_type == BTRFS_FILE_EXTENT_REG ||
7022 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007023 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007024 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007025
7026 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7027 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007028 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7029 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007030 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007031 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007032 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007033
7034 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7035 path->slots[0],
7036 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007037 }
Josef Bacik25a50342013-10-14 12:08:38 -04007038next:
Yan Zheng9036c102008-10-30 14:19:41 -04007039 if (start >= extent_end) {
7040 path->slots[0]++;
7041 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7042 ret = btrfs_next_leaf(root, path);
7043 if (ret < 0) {
7044 err = ret;
7045 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007046 }
Yan Zheng9036c102008-10-30 14:19:41 -04007047 if (ret > 0)
7048 goto not_found;
7049 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007050 }
Yan Zheng9036c102008-10-30 14:19:41 -04007051 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7052 if (found_key.objectid != objectid ||
7053 found_key.type != BTRFS_EXTENT_DATA_KEY)
7054 goto not_found;
7055 if (start + len <= found_key.offset)
7056 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007057 if (start > found_key.offset)
7058 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007059 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007060 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007061 em->len = found_key.offset - start;
7062 goto not_found_em;
7063 }
7064
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007065 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007066 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007067
Yan Zhengd899e052008-10-30 14:25:28 -04007068 if (found_type == BTRFS_FILE_EXTENT_REG ||
7069 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007070 goto insert;
7071 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007072 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007073 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007074 size_t size;
7075 size_t extent_offset;
7076 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007077
Filipe Manana7ffbb592014-06-09 03:48:05 +01007078 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007079 goto out;
Yan689f9342007-10-29 11:41:07 -04007080
Chris Mason514ac8a2014-01-03 21:07:00 -08007081 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007082 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007083 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7084 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007085 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007086 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007087 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007088 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007089 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007090 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007091 if (btrfs_file_extent_compression(leaf, item) !=
7092 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007093 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007094 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007095 if (ret) {
7096 err = ret;
7097 goto out;
7098 }
Chris Masonc8b97812008-10-29 14:49:59 -04007099 } else {
7100 map = kmap(page);
7101 read_extent_buffer(leaf, map + pg_offset, ptr,
7102 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007103 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007104 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007105 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007106 copy_size);
7107 }
Chris Masonc8b97812008-10-29 14:49:59 -04007108 kunmap(page);
7109 }
Chris Mason179e29e2007-11-01 11:28:41 -04007110 flush_dcache_page(page);
7111 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007112 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007113 if (!trans) {
7114 kunmap(page);
7115 free_extent_map(em);
7116 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007117
David Sterbab3b4aa72011-04-21 01:20:15 +02007118 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007119 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007120
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007121 if (IS_ERR(trans))
7122 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007123 goto again;
7124 }
Chris Masonc8b97812008-10-29 14:49:59 -04007125 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007126 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007127 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007128 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007129 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007130 }
Chris Masond1310b22008-01-24 16:13:08 -05007131 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007132 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007133 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007134 }
7135not_found:
7136 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007137 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007138 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007139not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007140 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007141 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007142insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007143 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007144 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007145 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007146 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7147 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007148 err = -EIO;
7149 goto out;
7150 }
Chris Masond1310b22008-01-24 16:13:08 -05007151
7152 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007153 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007154 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007155 /* it is possible that someone inserted the extent into the tree
7156 * while we had the lock dropped. It is also possible that
7157 * an overlapping map exists in the tree
7158 */
Chris Masona52d9a82007-08-27 16:49:44 -04007159 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007160 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007161
7162 ret = 0;
7163
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007164 existing = search_extent_mapping(em_tree, start, len);
7165 /*
7166 * existing will always be non-NULL, since there must be
7167 * extent causing the -EEXIST.
7168 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007169 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007170 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007171 em->block_start == existing->block_start) {
7172 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007173 * The existing extent map already encompasses the
7174 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007175 */
7176 free_extent_map(em);
7177 em = existing;
7178 err = 0;
7179
7180 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007181 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007182 /*
7183 * The existing extent map is the one nearest to
7184 * the [start, start + len) range which overlaps
7185 */
7186 err = merge_extent_mapping(em_tree, existing,
7187 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007188 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007189 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007190 free_extent_map(em);
7191 em = NULL;
7192 }
7193 } else {
7194 free_extent_map(em);
7195 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007196 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007197 }
Chris Masona52d9a82007-08-27 16:49:44 -04007198 }
Chris Mason890871b2009-09-02 16:24:52 -04007199 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007200out:
liubo1abe9b82011-03-24 11:18:59 +00007201
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007202 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007203
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007204 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007205 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007206 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007207 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007208 err = ret;
7209 }
Chris Masona52d9a82007-08-27 16:49:44 -04007210 if (err) {
7211 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007212 return ERR_PTR(err);
7213 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007214 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007215 return em;
7216}
7217
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007218struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7219 struct page *page,
7220 size_t pg_offset, u64 start, u64 len,
7221 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007222{
7223 struct extent_map *em;
7224 struct extent_map *hole_em = NULL;
7225 u64 range_start = start;
7226 u64 end;
7227 u64 found;
7228 u64 found_end;
7229 int err = 0;
7230
7231 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7232 if (IS_ERR(em))
7233 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007234 /*
7235 * If our em maps to:
7236 * - a hole or
7237 * - a pre-alloc extent,
7238 * there might actually be delalloc bytes behind it.
7239 */
7240 if (em->block_start != EXTENT_MAP_HOLE &&
7241 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7242 return em;
7243 else
7244 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007245
7246 /* check to see if we've wrapped (len == -1 or similar) */
7247 end = start + len;
7248 if (end < start)
7249 end = (u64)-1;
7250 else
7251 end -= 1;
7252
7253 em = NULL;
7254
7255 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007256 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007257 end, len, EXTENT_DELALLOC, 1);
7258 found_end = range_start + found;
7259 if (found_end < range_start)
7260 found_end = (u64)-1;
7261
7262 /*
7263 * we didn't find anything useful, return
7264 * the original results from get_extent()
7265 */
7266 if (range_start > end || found_end <= start) {
7267 em = hole_em;
7268 hole_em = NULL;
7269 goto out;
7270 }
7271
7272 /* adjust the range_start to make sure it doesn't
7273 * go backwards from the start they passed in
7274 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307275 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007276 found = found_end - range_start;
7277
7278 if (found > 0) {
7279 u64 hole_start = start;
7280 u64 hole_len = len;
7281
David Sterba172ddd62011-04-21 00:48:27 +02007282 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007283 if (!em) {
7284 err = -ENOMEM;
7285 goto out;
7286 }
7287 /*
7288 * when btrfs_get_extent can't find anything it
7289 * returns one huge hole
7290 *
7291 * make sure what it found really fits our range, and
7292 * adjust to make sure it is based on the start from
7293 * the caller
7294 */
7295 if (hole_em) {
7296 u64 calc_end = extent_map_end(hole_em);
7297
7298 if (calc_end <= start || (hole_em->start > end)) {
7299 free_extent_map(hole_em);
7300 hole_em = NULL;
7301 } else {
7302 hole_start = max(hole_em->start, start);
7303 hole_len = calc_end - hole_start;
7304 }
7305 }
7306 em->bdev = NULL;
7307 if (hole_em && range_start > hole_start) {
7308 /* our hole starts before our delalloc, so we
7309 * have to return just the parts of the hole
7310 * that go until the delalloc starts
7311 */
7312 em->len = min(hole_len,
7313 range_start - hole_start);
7314 em->start = hole_start;
7315 em->orig_start = hole_start;
7316 /*
7317 * don't adjust block start at all,
7318 * it is fixed at EXTENT_MAP_HOLE
7319 */
7320 em->block_start = hole_em->block_start;
7321 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007322 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7323 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007324 } else {
7325 em->start = range_start;
7326 em->len = found;
7327 em->orig_start = range_start;
7328 em->block_start = EXTENT_MAP_DELALLOC;
7329 em->block_len = found;
7330 }
7331 } else if (hole_em) {
7332 return hole_em;
7333 }
7334out:
7335
7336 free_extent_map(hole_em);
7337 if (err) {
7338 free_extent_map(em);
7339 return ERR_PTR(err);
7340 }
7341 return em;
7342}
7343
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007344static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7345 const u64 start,
7346 const u64 len,
7347 const u64 orig_start,
7348 const u64 block_start,
7349 const u64 block_len,
7350 const u64 orig_block_len,
7351 const u64 ram_bytes,
7352 const int type)
7353{
7354 struct extent_map *em = NULL;
7355 int ret;
7356
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007357 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007358 em = create_io_em(inode, start, len, orig_start,
7359 block_start, block_len, orig_block_len,
7360 ram_bytes,
7361 BTRFS_COMPRESS_NONE, /* compress_type */
7362 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007363 if (IS_ERR(em))
7364 goto out;
7365 }
7366 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7367 len, block_len, type);
7368 if (ret) {
7369 if (em) {
7370 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007371 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007372 start + len - 1, 0);
7373 }
7374 em = ERR_PTR(ret);
7375 }
7376 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007377
7378 return em;
7379}
7380
Josef Bacik4b46fce2010-05-23 11:00:55 -04007381static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7382 u64 start, u64 len)
7383{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007384 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007385 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007386 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007387 struct btrfs_key ins;
7388 u64 alloc_hint;
7389 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007390
Josef Bacik4b46fce2010-05-23 11:00:55 -04007391 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007392 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007393 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007394 if (ret)
7395 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007396
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007397 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7398 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007399 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007400 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007401 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007402 btrfs_free_reserved_extent(fs_info, ins.objectid,
7403 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007404
Josef Bacik4b46fce2010-05-23 11:00:55 -04007405 return em;
7406}
7407
Chris Mason46bfbb52010-05-26 11:04:10 -04007408/*
7409 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7410 * block must be cow'd
7411 */
Josef Bacik00361582013-08-14 14:02:47 -04007412noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007413 u64 *orig_start, u64 *orig_block_len,
7414 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007415{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007416 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007417 struct btrfs_path *path;
7418 int ret;
7419 struct extent_buffer *leaf;
7420 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007421 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007422 struct btrfs_file_extent_item *fi;
7423 struct btrfs_key key;
7424 u64 disk_bytenr;
7425 u64 backref_offset;
7426 u64 extent_end;
7427 u64 num_bytes;
7428 int slot;
7429 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007430 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007431
Chris Mason46bfbb52010-05-26 11:04:10 -04007432 path = btrfs_alloc_path();
7433 if (!path)
7434 return -ENOMEM;
7435
David Sterbaf85b7372017-01-20 14:54:07 +01007436 ret = btrfs_lookup_file_extent(NULL, root, path,
7437 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007438 if (ret < 0)
7439 goto out;
7440
7441 slot = path->slots[0];
7442 if (ret == 1) {
7443 if (slot == 0) {
7444 /* can't find the item, must cow */
7445 ret = 0;
7446 goto out;
7447 }
7448 slot--;
7449 }
7450 ret = 0;
7451 leaf = path->nodes[0];
7452 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007453 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007454 key.type != BTRFS_EXTENT_DATA_KEY) {
7455 /* not our file or wrong item type, must cow */
7456 goto out;
7457 }
7458
7459 if (key.offset > offset) {
7460 /* Wrong offset, must cow */
7461 goto out;
7462 }
7463
7464 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7465 found_type = btrfs_file_extent_type(leaf, fi);
7466 if (found_type != BTRFS_FILE_EXTENT_REG &&
7467 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7468 /* not a regular extent, must cow */
7469 goto out;
7470 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007471
7472 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7473 goto out;
7474
Miao Xiee77751a2013-12-27 21:11:50 +08007475 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7476 if (extent_end <= offset)
7477 goto out;
7478
Chris Mason46bfbb52010-05-26 11:04:10 -04007479 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007480 if (disk_bytenr == 0)
7481 goto out;
7482
7483 if (btrfs_file_extent_compression(leaf, fi) ||
7484 btrfs_file_extent_encryption(leaf, fi) ||
7485 btrfs_file_extent_other_encoding(leaf, fi))
7486 goto out;
7487
Chris Mason46bfbb52010-05-26 11:04:10 -04007488 backref_offset = btrfs_file_extent_offset(leaf, fi);
7489
Josef Bacik7ee9e442013-06-21 16:37:03 -04007490 if (orig_start) {
7491 *orig_start = key.offset - backref_offset;
7492 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7493 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7494 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007495
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007496 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007497 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007498
7499 num_bytes = min(offset + *len, extent_end) - offset;
7500 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7501 u64 range_end;
7502
Jeff Mahoneyda170662016-06-15 09:22:56 -04007503 range_end = round_up(offset + num_bytes,
7504 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007505 ret = test_range_bit(io_tree, offset, range_end,
7506 EXTENT_DELALLOC, 0, NULL);
7507 if (ret) {
7508 ret = -EAGAIN;
7509 goto out;
7510 }
7511 }
7512
Josef Bacik1bda19e2013-10-18 12:10:36 -04007513 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007514
7515 /*
7516 * look for other files referencing this extent, if we
7517 * find any we must cow
7518 */
Josef Bacik00361582013-08-14 14:02:47 -04007519
Liu Boe4c3b2d2017-01-30 12:25:28 -08007520 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007521 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007522 if (ret) {
7523 ret = 0;
7524 goto out;
7525 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007526
7527 /*
7528 * adjust disk_bytenr and num_bytes to cover just the bytes
7529 * in this extent we are about to write. If there
7530 * are any csums in that range we have to cow in order
7531 * to keep the csums correct
7532 */
7533 disk_bytenr += backref_offset;
7534 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007535 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7536 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007537 /*
7538 * all of the above have passed, it is safe to overwrite this extent
7539 * without cow
7540 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007541 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007542 ret = 1;
7543out:
7544 btrfs_free_path(path);
7545 return ret;
7546}
7547
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007548bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7549{
7550 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007551 bool found = false;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007552 void **pagep = NULL;
7553 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007554 unsigned long start_idx;
7555 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007556
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007557 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007558
7559 /*
7560 * end is the last byte in the last page. end == start is legal
7561 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007562 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007563
7564 rcu_read_lock();
7565
7566 /* Most of the code in this while loop is lifted from
7567 * find_get_page. It's been modified to begin searching from a
7568 * page and return just the first page found in that range. If the
7569 * found idx is less than or equal to the end idx then we know that
7570 * a page exists. If no pages are found or if those pages are
7571 * outside of the range then we're fine (yay!) */
7572 while (page == NULL &&
7573 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7574 page = radix_tree_deref_slot(pagep);
7575 if (unlikely(!page))
7576 break;
7577
7578 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007579 if (radix_tree_deref_retry(page)) {
7580 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007581 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007582 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007583 /*
7584 * Otherwise, shmem/tmpfs must be storing a swap entry
7585 * here as an exceptional entry: so return it without
7586 * attempting to raise page count.
7587 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007588 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007589 break; /* TODO: Is this relevant for this use case? */
7590 }
7591
Filipe Manana91405152014-06-05 13:22:24 +01007592 if (!page_cache_get_speculative(page)) {
7593 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007594 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007595 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007596
7597 /*
7598 * Has the page moved?
7599 * This is part of the lockless pagecache protocol. See
7600 * include/linux/pagemap.h for details.
7601 */
7602 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007603 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007604 page = NULL;
7605 }
7606 }
7607
7608 if (page) {
7609 if (page->index <= end_idx)
7610 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007611 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007612 }
7613
7614 rcu_read_unlock();
7615 return found;
7616}
7617
Josef Bacikeb838e72012-07-31 16:28:48 -04007618static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7619 struct extent_state **cached_state, int writing)
7620{
7621 struct btrfs_ordered_extent *ordered;
7622 int ret = 0;
7623
7624 while (1) {
7625 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007626 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007627 /*
7628 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007629 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007630 * extents in this range.
7631 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007632 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007633 lockend - lockstart + 1);
7634
7635 /*
7636 * We need to make sure there are no buffered pages in this
7637 * range either, we could have raced between the invalidate in
7638 * generic_file_direct_write and locking the extent. The
7639 * invalidate needs to happen so that reads after a write do not
7640 * get stale data.
7641 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007642 if (!ordered &&
7643 (!writing ||
7644 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007645 break;
7646
7647 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7648 cached_state, GFP_NOFS);
7649
7650 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007651 /*
7652 * If we are doing a DIO read and the ordered extent we
7653 * found is for a buffered write, we can not wait for it
7654 * to complete and retry, because if we do so we can
7655 * deadlock with concurrent buffered writes on page
7656 * locks. This happens only if our DIO read covers more
7657 * than one extent map, if at this point has already
7658 * created an ordered extent for a previous extent map
7659 * and locked its range in the inode's io tree, and a
7660 * concurrent write against that previous extent map's
7661 * range and this range started (we unlock the ranges
7662 * in the io tree only when the bios complete and
7663 * buffered writes always lock pages before attempting
7664 * to lock range in the io tree).
7665 */
7666 if (writing ||
7667 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7668 btrfs_start_ordered_extent(inode, ordered, 1);
7669 else
7670 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007671 btrfs_put_ordered_extent(ordered);
7672 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007673 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007674 * We could trigger writeback for this range (and wait
7675 * for it to complete) and then invalidate the pages for
7676 * this range (through invalidate_inode_pages2_range()),
7677 * but that can lead us to a deadlock with a concurrent
7678 * call to readpages() (a buffered read or a defrag call
7679 * triggered a readahead) on a page lock due to an
7680 * ordered dio extent we created before but did not have
7681 * yet a corresponding bio submitted (whence it can not
7682 * complete), which makes readpages() wait for that
7683 * ordered extent to complete while holding a lock on
7684 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007685 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007686 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007687 }
7688
Filipe Mananaade77022016-02-18 14:28:55 +00007689 if (ret)
7690 break;
7691
Josef Bacikeb838e72012-07-31 16:28:48 -04007692 cond_resched();
7693 }
7694
7695 return ret;
7696}
7697
Liu Bo6f9994d2017-01-31 07:50:22 -08007698/* The callers of this must take lock_extent() */
7699static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7700 u64 orig_start, u64 block_start,
7701 u64 block_len, u64 orig_block_len,
7702 u64 ram_bytes, int compress_type,
7703 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007704{
7705 struct extent_map_tree *em_tree;
7706 struct extent_map *em;
7707 struct btrfs_root *root = BTRFS_I(inode)->root;
7708 int ret;
7709
Liu Bo6f9994d2017-01-31 07:50:22 -08007710 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7711 type == BTRFS_ORDERED_COMPRESSED ||
7712 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007713 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007714
Josef Bacik69ffb542012-09-11 15:40:07 -04007715 em_tree = &BTRFS_I(inode)->extent_tree;
7716 em = alloc_extent_map();
7717 if (!em)
7718 return ERR_PTR(-ENOMEM);
7719
7720 em->start = start;
7721 em->orig_start = orig_start;
7722 em->len = len;
7723 em->block_len = block_len;
7724 em->block_start = block_start;
7725 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007726 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007727 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007728 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007729 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007730 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007731 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007732 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007733 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7734 em->compress_type = compress_type;
7735 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007736
7737 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007738 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007739 em->start + em->len - 1, 0);
7740 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007741 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007742 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007743 /*
7744 * The caller has taken lock_extent(), who could race with us
7745 * to add em?
7746 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007747 } while (ret == -EEXIST);
7748
7749 if (ret) {
7750 free_extent_map(em);
7751 return ERR_PTR(ret);
7752 }
7753
Liu Bo6f9994d2017-01-31 07:50:22 -08007754 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007755 return em;
7756}
7757
Josef Bacik4b46fce2010-05-23 11:00:55 -04007758static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7759 struct buffer_head *bh_result, int create)
7760{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007761 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007762 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007763 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307764 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007765 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007766 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007767 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007768 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007769 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007770
Miao Xie172a5042013-02-21 02:48:22 -07007771 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007772 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007773 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007774 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007775
Josef Bacikc3298612012-08-03 16:49:19 -04007776 lockstart = start;
7777 lockend = start + len - 1;
7778
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007779 if (current->journal_info) {
7780 /*
7781 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007782 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007783 * confused.
7784 */
chandan50745b02015-08-28 21:10:13 +05307785 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007786 current->journal_info = NULL;
7787 }
7788
Josef Bacikeb838e72012-07-31 16:28:48 -04007789 /*
7790 * If this errors out it's because we couldn't invalidate pagecache for
7791 * this range and we need to fallback to buffered.
7792 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007793 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7794 create)) {
7795 ret = -ENOTBLK;
7796 goto err;
7797 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007798
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007799 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007800 if (IS_ERR(em)) {
7801 ret = PTR_ERR(em);
7802 goto unlock_err;
7803 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007804
7805 /*
7806 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7807 * io. INLINE is special, and we could probably kludge it in here, but
7808 * it's still buffered so for safety lets just fall back to the generic
7809 * buffered path.
7810 *
7811 * For COMPRESSED we _have_ to read the entire extent in so we can
7812 * decompress it, so there will be buffering required no matter what we
7813 * do, so go ahead and fallback to buffered.
7814 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007815 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007816 * to buffered IO. Don't blame me, this is the price we pay for using
7817 * the generic code.
7818 */
7819 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7820 em->block_start == EXTENT_MAP_INLINE) {
7821 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007822 ret = -ENOTBLK;
7823 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007824 }
7825
7826 /* Just a good old fashioned hole, return */
7827 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7828 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7829 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007830 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007831 }
7832
7833 /*
7834 * We don't allocate a new extent in the following cases
7835 *
7836 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7837 * existing extent.
7838 * 2) The extent is marked as PREALLOC. We're good to go here and can
7839 * just use the extent.
7840 *
7841 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007842 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007843 len = min(len, em->len - (start - em->start));
7844 lockstart = start + len;
7845 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007846 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007847
7848 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7849 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7850 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007851 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007852 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007853
7854 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7855 type = BTRFS_ORDERED_PREALLOC;
7856 else
7857 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007858 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007859 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007860
Josef Bacik00361582013-08-14 14:02:47 -04007861 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007862 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007863 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007864 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007865
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007866 em2 = btrfs_create_dio_extent(inode, start, len,
7867 orig_start, block_start,
7868 len, orig_block_len,
7869 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007870 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007871 if (type == BTRFS_ORDERED_PREALLOC) {
7872 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007873 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007874 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007875 if (em2 && IS_ERR(em2)) {
7876 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007877 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007878 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007879 /*
7880 * For inode marked NODATACOW or extent marked PREALLOC,
7881 * use the existing or preallocated extent, so does not
7882 * need to adjust btrfs_space_info's bytes_may_use.
7883 */
7884 btrfs_free_reserved_data_space_noquota(inode,
7885 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007886 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007887 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007888 }
Josef Bacik00361582013-08-14 14:02:47 -04007889
Chris Mason46bfbb52010-05-26 11:04:10 -04007890 /*
7891 * this will cow the extent, reset the len in case we changed
7892 * it above
7893 */
7894 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007895 free_extent_map(em);
7896 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007897 if (IS_ERR(em)) {
7898 ret = PTR_ERR(em);
7899 goto unlock_err;
7900 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007901 len = min(len, em->len - (start - em->start));
7902unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007903 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7904 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007905 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007906 bh_result->b_bdev = em->bdev;
7907 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007908 if (create) {
7909 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7910 set_buffer_new(bh_result);
7911
7912 /*
7913 * Need to update the i_size under the extent lock so buffered
7914 * readers will get the updated i_size when we unlock.
7915 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007916 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007917 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007918
chandan50745b02015-08-28 21:10:13 +05307919 WARN_ON(dio_data->reserve < len);
7920 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007921 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307922 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007923 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007924
Josef Bacikeb838e72012-07-31 16:28:48 -04007925 /*
7926 * In the case of write we need to clear and unlock the entire range,
7927 * in the case of read we need to unlock only the end area that we
7928 * aren't using if there is any left over space.
7929 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007930 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007931 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7932 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007933 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007934 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007935 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007936 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007937
Josef Bacik4b46fce2010-05-23 11:00:55 -04007938 free_extent_map(em);
7939
7940 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007941
7942unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007943 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007944 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007945err:
chandan50745b02015-08-28 21:10:13 +05307946 if (dio_data)
7947 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007948 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007949}
7950
Omar Sandoval58efbc92017-08-22 23:45:59 -07007951static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7952 struct bio *bio,
7953 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007954{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007955 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007956 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007957
Mike Christie37226b22016-06-05 14:31:52 -05007958 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007959
7960 bio_get(bio);
7961
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007962 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007963 if (ret)
7964 goto err;
7965
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007966 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007967err:
7968 bio_put(bio);
7969 return ret;
7970}
7971
7972static int btrfs_check_dio_repairable(struct inode *inode,
7973 struct bio *failed_bio,
7974 struct io_failure_record *failrec,
7975 int failed_mirror)
7976{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007977 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007978 int num_copies;
7979
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007980 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007981 if (num_copies == 1) {
7982 /*
7983 * we only have a single copy of the data, so don't bother with
7984 * all the retry and error correction code that follows. no
7985 * matter what the error is, it is very likely to persist.
7986 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007987 btrfs_debug(fs_info,
7988 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7989 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007990 return 0;
7991 }
7992
7993 failrec->failed_mirror = failed_mirror;
7994 failrec->this_mirror++;
7995 if (failrec->this_mirror == failed_mirror)
7996 failrec->this_mirror++;
7997
7998 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007999 btrfs_debug(fs_info,
8000 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
8001 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008002 return 0;
8003 }
8004
8005 return 1;
8006}
8007
Omar Sandoval58efbc92017-08-22 23:45:59 -07008008static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
8009 struct page *page, unsigned int pgoff,
8010 u64 start, u64 end, int failed_mirror,
8011 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08008012{
8013 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04008014 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8015 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008016 struct bio *bio;
8017 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02008018 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07008019 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08008020 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008021 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008022
Mike Christie37226b22016-06-05 14:31:52 -05008023 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008024
8025 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8026 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07008027 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08008028
8029 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8030 failed_mirror);
8031 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008032 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008033 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08008034 }
8035
Liu Bo17347ce2017-05-15 15:33:27 -07008036 segs = bio_segments(failed_bio);
8037 if (segs > 1 ||
8038 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008039 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008040
8041 isector = start - btrfs_io_bio(failed_bio)->logical;
8042 isector >>= inode->i_sb->s_blocksize_bits;
8043 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308044 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05008045 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008046
8047 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02008048 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08008049 read_mode, failrec->this_mirror, failrec->in_validation);
8050
Omar Sandoval58efbc92017-08-22 23:45:59 -07008051 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
8052 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04008053 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008054 bio_put(bio);
8055 }
8056
Omar Sandoval58efbc92017-08-22 23:45:59 -07008057 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08008058}
8059
8060struct btrfs_retry_complete {
8061 struct completion done;
8062 struct inode *inode;
8063 u64 start;
8064 int uptodate;
8065};
8066
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008067static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008068{
8069 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008070 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008071 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008072 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008073 int i;
8074
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008075 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008076 goto end;
8077
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308078 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008079 io_tree = &BTRFS_I(inode)->io_tree;
8080 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8081 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308082
Miao Xie8b110e32014-09-12 18:44:03 +08008083 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008084 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008085 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008086 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8087 io_tree, done->start, bvec->bv_page,
8088 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008089end:
8090 complete(&done->done);
8091 bio_put(bio);
8092}
8093
Omar Sandoval58efbc92017-08-22 23:45:59 -07008094static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8095 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008096{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308097 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008098 struct bio_vec bvec;
8099 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008100 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008101 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308102 unsigned int pgoff;
8103 u32 sectorsize;
8104 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008105 blk_status_t ret;
8106 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008107
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308108 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008109 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308110
Miao Xiec1dc0892014-09-12 18:43:56 +08008111 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008112 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008113 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008114
Liu Bo17347ce2017-05-15 15:33:27 -07008115 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8116 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8117 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308118
8119next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008120 done.uptodate = 0;
8121 done.start = start;
8122 init_completion(&done.done);
8123
Liu Bo17347ce2017-05-15 15:33:27 -07008124 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308125 pgoff, start, start + sectorsize - 1,
8126 io_bio->mirror_num,
8127 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008128 if (ret) {
8129 err = ret;
8130 goto next;
8131 }
Miao Xie8b110e32014-09-12 18:44:03 +08008132
David Sterba9c17f6c2017-07-19 19:26:45 +02008133 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008134
8135 if (!done.uptodate) {
8136 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308137 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008138 }
8139
Liu Bo629ebf42017-05-15 17:20:07 -07008140next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308141 start += sectorsize;
8142
Liu Bo97bf5a52017-04-07 13:11:10 -07008143 nr_sectors--;
8144 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308145 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008146 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308147 goto next_block_or_try_again;
8148 }
Miao Xie8b110e32014-09-12 18:44:03 +08008149 }
8150
Liu Bo629ebf42017-05-15 17:20:07 -07008151 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008152}
8153
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008154static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008155{
8156 struct btrfs_retry_complete *done = bio->bi_private;
8157 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008158 struct extent_io_tree *io_tree, *failure_tree;
8159 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008160 struct bio_vec *bvec;
8161 int uptodate;
8162 int ret;
8163 int i;
8164
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008165 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008166 goto end;
8167
8168 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308169
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308170 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008171 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308172
Josef Bacik7870d082017-05-05 11:57:15 -04008173 io_tree = &BTRFS_I(inode)->io_tree;
8174 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8175
David Sterbac09abff2017-07-13 18:10:07 +02008176 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008177 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008178 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8179 bvec->bv_offset, done->start,
8180 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008181 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008182 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8183 failure_tree, io_tree, done->start,
8184 bvec->bv_page,
8185 btrfs_ino(BTRFS_I(inode)),
8186 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008187 else
8188 uptodate = 0;
8189 }
8190
8191 done->uptodate = uptodate;
8192end:
8193 complete(&done->done);
8194 bio_put(bio);
8195}
8196
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008197static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8198 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008199{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308200 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008201 struct bio_vec bvec;
8202 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008203 struct btrfs_retry_complete done;
8204 u64 start;
8205 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308206 u32 sectorsize;
8207 int nr_sectors;
8208 unsigned int pgoff;
8209 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008210 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008211 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008212 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008213
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308214 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008215 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308216
Omar Sandoval58efbc92017-08-22 23:45:59 -07008217 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008218 start = io_bio->logical;
8219 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008220 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008221
Liu Bo17347ce2017-05-15 15:33:27 -07008222 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8223 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308224
Liu Bo17347ce2017-05-15 15:33:27 -07008225 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308226next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008227 if (uptodate) {
8228 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8229 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8230 bvec.bv_page, pgoff, start, sectorsize);
8231 if (likely(!ret))
8232 goto next;
8233 }
Miao Xie8b110e32014-09-12 18:44:03 +08008234try_again:
8235 done.uptodate = 0;
8236 done.start = start;
8237 init_completion(&done.done);
8238
Omar Sandoval58efbc92017-08-22 23:45:59 -07008239 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8240 pgoff, start, start + sectorsize - 1,
8241 io_bio->mirror_num, btrfs_retry_endio,
8242 &done);
8243 if (status) {
8244 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008245 goto next;
8246 }
8247
David Sterba9c17f6c2017-07-19 19:26:45 +02008248 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008249
8250 if (!done.uptodate) {
8251 /* We might have another mirror, so try again */
8252 goto try_again;
8253 }
8254next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308255 offset += sectorsize;
8256 start += sectorsize;
8257
8258 ASSERT(nr_sectors);
8259
Liu Bo97bf5a52017-04-07 13:11:10 -07008260 nr_sectors--;
8261 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308262 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008263 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308264 goto next_block;
8265 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008266 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008267
8268 return err;
8269}
8270
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008271static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8272 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008273{
8274 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8275
8276 if (skip_csum) {
8277 if (unlikely(err))
8278 return __btrfs_correct_data_nocsum(inode, io_bio);
8279 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008280 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008281 } else {
8282 return __btrfs_subio_endio_read(inode, io_bio, err);
8283 }
8284}
8285
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008286static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008287{
8288 struct btrfs_dio_private *dip = bio->bi_private;
8289 struct inode *inode = dip->inode;
8290 struct bio *dio_bio;
8291 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008292 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008293
Liu Bo99c4e3b2017-09-15 15:06:51 -06008294 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008295 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008296
Josef Bacik4b46fce2010-05-23 11:00:55 -04008297 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008298 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008299 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008300
Josef Bacik4b46fce2010-05-23 11:00:55 -04008301 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008302
Liu Bo99c4e3b2017-09-15 15:06:51 -06008303 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008304 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008305
8306 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008307 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008308 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008309}
8310
Qu Wenruo524272602017-03-08 10:25:52 +08008311static void __endio_write_update_ordered(struct inode *inode,
8312 const u64 offset, const u64 bytes,
8313 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008314{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008315 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008316 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008317 struct btrfs_workqueue *wq;
8318 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008319 u64 ordered_offset = offset;
8320 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008321 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008322 int ret;
8323
Qu Wenruo524272602017-03-08 10:25:52 +08008324 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8325 wq = fs_info->endio_freespace_worker;
8326 func = btrfs_freespace_write_helper;
8327 } else {
8328 wq = fs_info->endio_write_workers;
8329 func = btrfs_endio_write_helper;
8330 }
8331
Chris Mason163cf092010-11-28 19:56:33 -05008332again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008333 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008334 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8335 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008336 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008337 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008338 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008339 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008340
Qu Wenruo524272602017-03-08 10:25:52 +08008341 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8342 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008343out_test:
8344 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008345 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8346 * in the range, we can exit.
8347 */
8348 if (ordered_offset == last_offset)
8349 return;
8350 /*
Chris Mason163cf092010-11-28 19:56:33 -05008351 * our bio might span multiple ordered extents. If we haven't
8352 * completed the accounting for the whole dio, go back and try again
8353 */
Filipe Manana14543772015-11-24 16:23:54 +00008354 if (ordered_offset < offset + bytes) {
8355 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008356 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008357 goto again;
8358 }
Filipe Manana14543772015-11-24 16:23:54 +00008359}
8360
8361static void btrfs_endio_direct_write(struct bio *bio)
8362{
8363 struct btrfs_dio_private *dip = bio->bi_private;
8364 struct bio *dio_bio = dip->dio_bio;
8365
Qu Wenruo524272602017-03-08 10:25:52 +08008366 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008367 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008368
Josef Bacik4b46fce2010-05-23 11:00:55 -04008369 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008370
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008371 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008372 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008373 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008374}
8375
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008376static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008377 struct bio *bio, int mirror_num,
8378 unsigned long bio_flags, u64 offset)
8379{
Josef Bacikc6100a42017-05-05 11:57:13 -04008380 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008381 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008382 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008383 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008384 return 0;
8385}
8386
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008387static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008388{
8389 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008390 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008391
Miao Xie8b110e32014-09-12 18:44:03 +08008392 if (err)
8393 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008394 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008395 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8396 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008397 (unsigned long long)bio->bi_iter.bi_sector,
8398 bio->bi_iter.bi_size, err);
8399
8400 if (dip->subio_endio)
8401 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008402
8403 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008404 dip->errors = 1;
8405
8406 /*
8407 * before atomic variable goto zero, we must make sure
8408 * dip->errors is perceived to be set.
8409 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008410 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008411 }
8412
8413 /* if there are more bios still pending for this dio, just exit */
8414 if (!atomic_dec_and_test(&dip->pending_bios))
8415 goto out;
8416
Chris Mason9be33952013-05-17 18:30:14 -04008417 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008418 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008419 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008420 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008421 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008422 }
8423out:
8424 bio_put(bio);
8425}
8426
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008427static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008428 struct btrfs_dio_private *dip,
8429 struct bio *bio,
8430 u64 file_offset)
8431{
8432 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8433 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008434 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008435
8436 /*
8437 * We load all the csum data we need when we submit
8438 * the first bio to reduce the csum tree search and
8439 * contention.
8440 */
8441 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008442 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008443 file_offset);
8444 if (ret)
8445 return ret;
8446 }
8447
8448 if (bio == dip->orig_bio)
8449 return 0;
8450
8451 file_offset -= dip->logical_offset;
8452 file_offset >>= inode->i_sb->s_blocksize_bits;
8453 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8454
8455 return 0;
8456}
8457
Omar Sandoval58efbc92017-08-22 23:45:59 -07008458static inline blk_status_t
8459__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008460 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008461{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008462 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008463 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008464 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008465 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008466
Liu Bo4c274bc2017-11-01 17:19:27 -06008467 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008468 if (async_submit)
8469 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8470
Miao Xiee65e1532010-11-22 03:04:43 +00008471 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008472
8473 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008474 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008475 if (ret)
8476 goto err;
8477 }
Miao Xiee65e1532010-11-22 03:04:43 +00008478
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008479 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008480 goto map;
8481
8482 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008483 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8484 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008485 __btrfs_submit_bio_start_direct_io,
8486 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008487 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008488 } else if (write) {
8489 /*
8490 * If we aren't doing async submit, calculate the csum of the
8491 * bio now.
8492 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008493 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008494 if (ret)
8495 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008496 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008497 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008498 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008499 if (ret)
8500 goto err;
8501 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008502map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008503 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008504err:
8505 bio_put(bio);
8506 return ret;
8507}
8508
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008509static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008510{
8511 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008512 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008513 struct bio *bio;
8514 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008515 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008516 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008517 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008518 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008519 u64 submit_len;
8520 int clone_offset = 0;
8521 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308522 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008523 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008524
Kent Overstreet4f024f32013-10-11 15:44:27 -07008525 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008526 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008527 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8528 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008529 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008530 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008531
Liu Bo725130b2017-05-16 09:51:39 -07008532 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008533 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008534 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008535 goto submit;
8536 }
8537
David Woodhouse53b381b2013-01-29 18:40:14 -05008538 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008539 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008540 async_submit = 0;
8541 else
8542 async_submit = 1;
8543
Liu Bo725130b2017-05-16 09:51:39 -07008544 /* bio split */
8545 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008546 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008547 do {
Liu Bo725130b2017-05-16 09:51:39 -07008548 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008549
Liu Bo725130b2017-05-16 09:51:39 -07008550 /*
8551 * This will never fail as it's passing GPF_NOFS and
8552 * the allocation is backed by btrfs_bioset.
8553 */
Liu Boe4770942017-05-16 10:57:14 -07008554 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008555 clone_len);
8556 bio->bi_private = dip;
8557 bio->bi_end_io = btrfs_end_dio_bio;
8558 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008559
Liu Bo725130b2017-05-16 09:51:39 -07008560 ASSERT(submit_len >= clone_len);
8561 submit_len -= clone_len;
8562 if (submit_len == 0)
8563 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008564
Liu Bo725130b2017-05-16 09:51:39 -07008565 /*
8566 * Increase the count before we submit the bio so we know
8567 * the end IO handler won't happen before we increase the
8568 * count. Otherwise, the dip might get freed before we're
8569 * done setting it up.
8570 */
8571 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008572
Linus Torvalds66ba7722017-09-09 13:27:51 -07008573 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008574 async_submit);
8575 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008576 bio_put(bio);
8577 atomic_dec(&dip->pending_bios);
8578 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008579 }
Liu Bo725130b2017-05-16 09:51:39 -07008580
8581 clone_offset += clone_len;
8582 start_sector += clone_len >> 9;
8583 file_offset += clone_len;
8584
8585 map_length = submit_len;
8586 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8587 start_sector << 9, &map_length, NULL, 0);
8588 if (ret)
8589 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008590 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008591
Josef Bacik02f57c72011-04-06 14:25:44 -04008592submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008593 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008594 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008595 return 0;
8596
8597 bio_put(bio);
8598out_err:
8599 dip->errors = 1;
8600 /*
8601 * before atomic variable goto zero, we must
8602 * make sure dip->errors is perceived to be set.
8603 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008604 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008605 if (atomic_dec_and_test(&dip->pending_bios))
8606 bio_io_error(dip->orig_bio);
8607
8608 /* bio_end_io() will handle error, so we needn't return it */
8609 return 0;
8610}
8611
Mike Christie8a4c1e42016-06-05 14:31:50 -05008612static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8613 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008614{
Filipe Manana61de7182015-07-01 12:13:10 +01008615 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008616 struct bio *bio = NULL;
8617 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008618 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008619 int ret = 0;
8620
David Sterba8b6c1d52017-06-02 17:48:13 +02008621 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008622
Miao Xiec1dc0892014-09-12 18:43:56 +08008623 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008624 if (!dip) {
8625 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008626 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008627 }
8628
8629 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008630 dip->inode = inode;
8631 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008632 dip->bytes = dio_bio->bi_iter.bi_size;
8633 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008634 bio->bi_private = dip;
8635 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008636 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008637 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008638 io_bio = btrfs_io_bio(bio);
8639 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008640
Miao Xiec1dc0892014-09-12 18:43:56 +08008641 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008642 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008643 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008644 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008645 dip->subio_endio = btrfs_subio_endio_read;
8646 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008647
Filipe Mananaf28a4922015-12-08 19:23:20 +00008648 /*
8649 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8650 * even if we fail to submit a bio, because in such case we do the
8651 * corresponding error handling below and it must not be done a second
8652 * time by btrfs_direct_IO().
8653 */
8654 if (write) {
8655 struct btrfs_dio_data *dio_data = current->journal_info;
8656
8657 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8658 dip->bytes;
8659 dio_data->unsubmitted_oe_range_start =
8660 dio_data->unsubmitted_oe_range_end;
8661 }
8662
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008663 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008664 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008665 return;
Chris Mason9be33952013-05-17 18:30:14 -04008666
Liu Bo3892ac92017-04-17 15:00:28 -07008667 if (io_bio->end_io)
8668 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008669
Josef Bacik4b46fce2010-05-23 11:00:55 -04008670free_ordered:
8671 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008672 * If we arrived here it means either we failed to submit the dip
8673 * or we either failed to clone the dio_bio or failed to allocate the
8674 * dip. If we cloned the dio_bio and allocated the dip, we can just
8675 * call bio_endio against our io_bio so that we get proper resource
8676 * cleanup if we fail to submit the dip, otherwise, we must do the
8677 * same as btrfs_endio_direct_[write|read] because we can't call these
8678 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008679 */
Liu Bo3892ac92017-04-17 15:00:28 -07008680 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008681 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008682 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008683 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008684 * and all the cleanup and final put for dio_bio (through
8685 * dio_end_io()).
8686 */
8687 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008688 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008689 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008690 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008691 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008692 file_offset,
8693 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008694 false);
Filipe Manana14543772015-11-24 16:23:54 +00008695 else
Filipe Manana61de7182015-07-01 12:13:10 +01008696 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8697 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008698
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008699 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008700 /*
8701 * Releases and cleans up our dio_bio, no need to bio_put()
8702 * nor bio_endio()/bio_io_error() against dio_bio.
8703 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008704 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008705 }
Liu Bo3892ac92017-04-17 15:00:28 -07008706 if (bio)
8707 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008708 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008709}
8710
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008711static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008712 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008713{
8714 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008715 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008716 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008717 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008718
8719 if (offset & blocksize_mask)
8720 goto out;
8721
Al Viro28060d52014-03-22 05:15:17 -04008722 if (iov_iter_alignment(iter) & blocksize_mask)
8723 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008724
Al Viro28060d52014-03-22 05:15:17 -04008725 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008726 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008727 return 0;
8728 /*
8729 * Check to make sure we don't have duplicate iov_base's in this
8730 * iovec, if so return EINVAL, otherwise we'll get csum errors
8731 * when reading back.
8732 */
8733 for (seg = 0; seg < iter->nr_segs; seg++) {
8734 for (i = seg + 1; i < iter->nr_segs; i++) {
8735 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008736 goto out;
8737 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008738 }
8739 retval = 0;
8740out:
8741 return retval;
8742}
Josef Bacikeb838e72012-07-31 16:28:48 -04008743
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008744static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008745{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008746 struct file *file = iocb->ki_filp;
8747 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008748 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308749 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008750 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008751 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008752 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008753 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008754 bool wakeup = true;
8755 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008756 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008757
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008758 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008759 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008760
Jens Axboefe0f07d2015-04-15 17:05:48 -06008761 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008762
Josef Bacik0e267c42013-07-02 10:38:02 -04008763 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008764 * The generic stuff only does filemap_write_and_wait_range, which
8765 * isn't enough if we've written compressed pages to this area, so
8766 * we need to flush the dirty pages again to make absolutely sure
8767 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008768 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008769 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008770 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8771 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008772 filemap_fdatawrite_range(inode->i_mapping, offset,
8773 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008774
Omar Sandoval6f673762015-03-16 04:33:52 -07008775 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008776 /*
8777 * If the write DIO is beyond the EOF, we need update
8778 * the isize, but it is protected by i_mutex. So we can
8779 * not unlock the i_mutex at this case.
8780 */
8781 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008782 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008783 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008784 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008785 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8786 ret = -EAGAIN;
8787 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008788 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008789 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8790 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008791 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008792 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008793
8794 /*
8795 * We need to know how many extents we reserved so that we can
8796 * do the accounting properly if we go over the number we
8797 * originally calculated. Abuse current->journal_info for this.
8798 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008799 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008800 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008801 dio_data.unsubmitted_oe_range_start = (u64)offset;
8802 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308803 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308804 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008805 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8806 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008807 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008808 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8809 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008810 }
8811
Omar Sandoval17f8c842015-03-16 04:33:50 -07008812 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008813 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008814 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008815 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008816 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308817 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008818 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008819 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308820 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008821 btrfs_delalloc_release_space(inode, data_reserved,
8822 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008823 /*
8824 * On error we might have left some ordered extents
8825 * without submitting corresponding bios for them, so
8826 * cleanup them up to avoid other tasks getting them
8827 * and waiting for them to complete forever.
8828 */
8829 if (dio_data.unsubmitted_oe_range_start <
8830 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008831 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008832 dio_data.unsubmitted_oe_range_start,
8833 dio_data.unsubmitted_oe_range_end -
8834 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008835 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008836 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008837 btrfs_delalloc_release_space(inode, data_reserved,
8838 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008839 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008840 }
Miao Xie38851cc2013-02-08 07:04:11 +00008841out:
Miao Xie2e60a512013-02-08 07:01:08 +00008842 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008843 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008844 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008845 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008846
Qu Wenruo364ecf32017-02-27 15:10:38 +08008847 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008848 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008849}
8850
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008851#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8852
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008853static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8854 __u64 start, __u64 len)
8855{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008856 int ret;
8857
8858 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8859 if (ret)
8860 return ret;
8861
David Sterba2135fb92017-06-23 04:09:57 +02008862 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008863}
8864
Chris Mason9ebefb182007-06-15 13:50:00 -04008865int btrfs_readpage(struct file *file, struct page *page)
8866{
Chris Masond1310b22008-01-24 16:13:08 -05008867 struct extent_io_tree *tree;
8868 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008869 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008870}
Chris Mason1832a6d2007-12-21 16:27:21 -05008871
Chris Mason39279cc2007-06-12 06:35:45 -04008872static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8873{
Chris Masond1310b22008-01-24 16:13:08 -05008874 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008875 struct inode *inode = page->mapping->host;
8876 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008877
8878 if (current->flags & PF_MEMALLOC) {
8879 redirty_page_for_writepage(wbc, page);
8880 unlock_page(page);
8881 return 0;
8882 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008883
8884 /*
8885 * If we are under memory pressure we will call this directly from the
8886 * VM, we need to make sure we have the inode referenced for the ordered
8887 * extent. If not just return like we didn't do anything.
8888 */
8889 if (!igrab(inode)) {
8890 redirty_page_for_writepage(wbc, page);
8891 return AOP_WRITEPAGE_ACTIVATE;
8892 }
Chris Masond1310b22008-01-24 16:13:08 -05008893 tree = &BTRFS_I(page->mapping->host)->io_tree;
David Sterbadeac6422017-06-23 03:47:28 +02008894 ret = extent_write_full_page(tree, page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008895 btrfs_add_delayed_iput(inode);
8896 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008897}
Chris Mason39279cc2007-06-12 06:35:45 -04008898
Eric Sandeen48a3b632013-04-25 20:41:01 +00008899static int btrfs_writepages(struct address_space *mapping,
8900 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008901{
Chris Masond1310b22008-01-24 16:13:08 -05008902 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008903
Chris Masond1310b22008-01-24 16:13:08 -05008904 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba43317592017-06-23 03:46:07 +02008905 return extent_writepages(tree, mapping, wbc);
Chris Masonb293f022007-11-01 19:45:34 -04008906}
8907
Chris Mason3ab2fb52007-11-08 10:59:22 -05008908static int
8909btrfs_readpages(struct file *file, struct address_space *mapping,
8910 struct list_head *pages, unsigned nr_pages)
8911{
Chris Masond1310b22008-01-24 16:13:08 -05008912 struct extent_io_tree *tree;
8913 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008914 return extent_readpages(tree, mapping, pages, nr_pages,
8915 btrfs_get_extent);
8916}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008917static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008918{
Chris Masond1310b22008-01-24 16:13:08 -05008919 struct extent_io_tree *tree;
8920 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008921 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008922
Chris Masond1310b22008-01-24 16:13:08 -05008923 tree = &BTRFS_I(page->mapping->host)->io_tree;
8924 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008925 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008926 if (ret == 1) {
8927 ClearPagePrivate(page);
8928 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008929 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008930 }
8931 return ret;
8932}
Chris Mason39279cc2007-06-12 06:35:45 -04008933
Chris Masone6dcd2d2008-07-17 12:53:50 -04008934static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8935{
Chris Mason98509cf2008-09-11 15:51:43 -04008936 if (PageWriteback(page) || PageDirty(page))
8937 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008938 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008939}
8940
Lukas Czernerd47992f2013-05-21 23:17:23 -04008941static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8942 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008943{
Josef Bacik5fd02042012-05-02 14:00:54 -04008944 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008945 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008946 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008947 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008948 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008949 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308950 u64 start;
8951 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008952 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008953
Chris Mason8b62b722009-09-02 16:53:46 -04008954 /*
8955 * we have the page locked, so new writeback can't start,
8956 * and the dirty bit won't be cleared while we are here.
8957 *
8958 * Wait for IO on this page so that we can safely clear
8959 * the PagePrivate2 bit and do ordered accounting
8960 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008961 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008962
Josef Bacik5fd02042012-05-02 14:00:54 -04008963 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008964 if (offset) {
8965 btrfs_releasepage(page, GFP_NOFS);
8966 return;
8967 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008968
8969 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008970 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308971again:
8972 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008973 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308974 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008975 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308976 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008977 /*
8978 * IO on this page will never be started, so we need
8979 * to account for any ordered extents now
8980 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008981 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308982 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008983 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008984 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008985 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008986 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008987 /*
8988 * whoever cleared the private bit is responsible
8989 * for the finish_ordered_io
8990 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008991 if (TestClearPagePrivate2(page)) {
8992 struct btrfs_ordered_inode_tree *tree;
8993 u64 new_len;
8994
8995 tree = &BTRFS_I(inode)->ordered_tree;
8996
8997 spin_lock_irq(&tree->lock);
8998 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308999 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04009000 if (new_len < ordered->truncated_len)
9001 ordered->truncated_len = new_len;
9002 spin_unlock_irq(&tree->lock);
9003
9004 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309005 start,
9006 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04009007 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04009008 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009009 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009010 if (!inode_evicting) {
9011 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309012 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009013 &cached_state);
9014 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309015
9016 start = end + 1;
9017 if (start < page_end)
9018 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009019 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009020
Qu Wenruob9d0b382015-09-29 10:35:16 +08009021 /*
9022 * Qgroup reserved space handler
9023 * Page here will be either
9024 * 1) Already written to disk
9025 * In this case, its reserved space is released from data rsv map
9026 * and will be freed by delayed_ref handler finally.
9027 * So even we call qgroup_free_data(), it won't decrease reserved
9028 * space.
9029 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009030 * This means the reserved space should be freed here. However,
9031 * if a truncate invalidates the page (by clearing PageDirty)
9032 * and the page is accounted for while allocating extent
9033 * in btrfs_check_data_free_space() we let delayed_ref to
9034 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009035 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009036 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08009037 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009038 if (!inode_evicting) {
9039 clear_extent_bit(tree, page_start, page_end,
9040 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009041 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9042 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01009043 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009044
9045 __btrfs_releasepage(page, GFP_NOFS);
9046 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009047
Chris Mason4a096752008-07-21 10:29:44 -04009048 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009049 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009050 ClearPagePrivate(page);
9051 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009052 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009053 }
Chris Mason39279cc2007-06-12 06:35:45 -04009054}
9055
Chris Mason9ebefb182007-06-15 13:50:00 -04009056/*
9057 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9058 * called from a page fault handler when a page is first dirtied. Hence we must
9059 * be careful to check for EOF conditions here. We set the page up correctly
9060 * for a written page which means we get ENOSPC checking when writing into
9061 * holes and correct delalloc and unwritten extent mapping on filesystems that
9062 * support these features.
9063 *
9064 * We are not allowed to take the i_mutex here so we have to play games to
9065 * protect against truncate races as the page could now be beyond EOF. Because
9066 * vmtruncate() writes the inode size before removing pages, once we have the
9067 * page lock we can determine safely if the page is beyond EOF. If it is not
9068 * beyond EOF, then the page is guaranteed safe against truncation until we
9069 * unlock the page.
9070 */
Dave Jiang11bac802017-02-24 14:56:41 -08009071int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009072{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009073 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009074 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009075 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009076 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9077 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009078 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009079 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009080 char *kaddr;
9081 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009082 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009083 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009084 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309085 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009086 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009087 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309088 u64 end;
9089
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009090 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009091
Jan Karab2b5ef52012-06-12 16:20:45 +02009092 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009093 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009094 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309095 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009096
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309097 /*
9098 * Reserving delalloc space after obtaining the page lock can lead to
9099 * deadlock. For example, if a dirty page is locked by this function
9100 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9101 * dirty page write out, then the btrfs_writepage() function could
9102 * end up waiting indefinitely to get a lock on the page currently
9103 * being processed by btrfs_page_mkwrite() function.
9104 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009105 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309106 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009107 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009108 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009109 reserved = 1;
9110 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009111 if (ret) {
9112 if (ret == -ENOMEM)
9113 ret = VM_FAULT_OOM;
9114 else /* -ENOSPC, -EIO, etc */
9115 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009116 if (reserved)
9117 goto out;
9118 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009119 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009120
Nick Piggin56a76f82009-03-31 15:23:23 -07009121 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009122again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009123 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009124 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009125
Chris Mason9ebefb182007-06-15 13:50:00 -04009126 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009127 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009128 /* page got truncated out from underneath us */
9129 goto out_unlock;
9130 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009131 wait_on_page_writeback(page);
9132
David Sterbaff13db42015-12-03 14:30:40 +01009133 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009134 set_page_extent_mapped(page);
9135
Chris Masoneb84ae02008-07-17 13:53:27 -04009136 /*
9137 * we can't set the delalloc bits if there are pending ordered
9138 * extents. Drop our locks and wait for them to finish
9139 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009140 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9141 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009142 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009143 unlock_extent_cached(io_tree, page_start, page_end,
9144 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009145 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009146 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009147 btrfs_put_ordered_extent(ordered);
9148 goto again;
9149 }
9150
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009151 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009152 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009153 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009154 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309155 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009156 btrfs_delalloc_release_space(inode, data_reserved,
9157 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309158 }
9159 }
9160
Josef Bacikfbf19082009-10-01 17:10:23 -04009161 /*
Liu Bo54160342016-12-13 12:15:19 -08009162 * page_mkwrite gets called when the page is firstly dirtied after it's
9163 * faulted in, but write(2) could also dirty a page and set delalloc
9164 * bits, thus in this case for space account reason, we still need to
9165 * clear any delalloc bits within this page range since we have to
9166 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009167 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309168 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009169 EXTENT_DIRTY | EXTENT_DELALLOC |
9170 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009171 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009172
Filipe Mananae3b8a482017-11-04 00:16:59 +00009173 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009174 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009175 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009176 unlock_extent_cached(io_tree, page_start, page_end,
9177 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009178 ret = VM_FAULT_SIGBUS;
9179 goto out_unlock;
9180 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009181 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009182
9183 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009184 if (page_start + PAGE_SIZE > size)
9185 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009186 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009187 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009188
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009189 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009190 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009191 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009192 flush_dcache_page(page);
9193 kunmap(page);
9194 }
Chris Mason247e7432008-07-17 12:53:51 -04009195 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009196 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009197 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009198
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009199 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009200 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009201 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009202
Josef Bacik2ac55d42010-02-03 19:33:23 +00009203 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009204
9205out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009206 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009207 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009208 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009209 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009210 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009211 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009212 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009213out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009214 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009215 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9216 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009217out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009218 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009219 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009220 return ret;
9221}
9222
Josef Bacika41ad392011-01-31 15:30:16 -05009223static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009224{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009225 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009226 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009227 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009228 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009229 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009230 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009231 u64 mask = fs_info->sectorsize - 1;
9232 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009233
Josef Bacik0ef8b722013-10-25 16:13:35 -04009234 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9235 (u64)-1);
9236 if (ret)
9237 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009238
Josef Bacikfcb80c22011-05-03 10:40:22 -04009239 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009240 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009241 * 3 things going on here
9242 *
9243 * 1) We need to reserve space for our orphan item and the space to
9244 * delete our orphan item. Lord knows we don't want to have a dangling
9245 * orphan item because we didn't reserve space to remove it.
9246 *
9247 * 2) We need to reserve space to update our inode.
9248 *
9249 * 3) We need to have something to cache all the space that is going to
9250 * be free'd up by the truncate operation, but also have some slack
9251 * space reserved in case it uses space during the truncate (thank you
9252 * very much snapshotting).
9253 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009254 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009255 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009256 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009257 * doesn't end up using space reserved for updating the inode or
9258 * removing the orphan item. We also need to be able to stop the
9259 * transaction and start a new one, which means we need to be able to
9260 * update the inode several times, and we have no idea of knowing how
9261 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009262 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009263 * Then there is the orphan item, which does indeed need to be held on
9264 * to for the whole operation, and we need nobody to touch this reserved
9265 * space except the orphan code.
9266 *
9267 * So that leaves us with
9268 *
9269 * 1) root->orphan_block_rsv - for the orphan deletion.
9270 * 2) rsv - for the truncate reservation, which we will steal from the
9271 * transaction reservation.
9272 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9273 * updating the inode.
9274 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009275 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009276 if (!rsv)
9277 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009278 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009279 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009280
Josef Bacik907cbce2011-08-08 13:46:15 -04009281 /*
Josef Bacik07127182011-08-19 10:29:59 -04009282 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009283 * 1 for updating the inode.
9284 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009285 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009286 if (IS_ERR(trans)) {
9287 err = PTR_ERR(trans);
9288 goto out;
9289 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009290
Josef Bacik907cbce2011-08-08 13:46:15 -04009291 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009292 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009293 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009294 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009295
Chris Mason5a3f23d2009-03-31 13:27:11 -04009296 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009297 * So if we truncate and then write and fsync we normally would just
9298 * write the extents that changed, which is a problem if we need to
9299 * first truncate that entire inode. So set this flag so we write out
9300 * all of the extents in the inode to the sync log so we're completely
9301 * safe.
9302 */
9303 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009304 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009305
Yan, Zheng80825102009-11-12 09:35:36 +00009306 while (1) {
9307 ret = btrfs_truncate_inode_items(trans, root, inode,
9308 inode->i_size,
9309 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009310 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009311 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009312 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009313 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009314 }
Chris Mason39279cc2007-06-12 06:35:45 -04009315
Yan, Zheng80825102009-11-12 09:35:36 +00009316 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009317 if (ret) {
9318 err = ret;
9319 break;
9320 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009321
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009322 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009323 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009324
9325 trans = btrfs_start_transaction(root, 2);
9326 if (IS_ERR(trans)) {
9327 ret = err = PTR_ERR(trans);
9328 trans = NULL;
9329 break;
9330 }
9331
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009332 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009333 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009334 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009335 BUG_ON(ret); /* shouldn't happen */
9336 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009337 }
9338
Josef Bacikddfae632017-10-19 14:16:02 -04009339 /*
9340 * We can't call btrfs_truncate_block inside a trans handle as we could
9341 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9342 * we've truncated everything except the last little bit, and can do
9343 * btrfs_truncate_block and then update the disk_i_size.
9344 */
9345 if (ret == NEED_TRUNCATE_BLOCK) {
9346 btrfs_end_transaction(trans);
9347 btrfs_btree_balance_dirty(fs_info);
9348
9349 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9350 if (ret)
9351 goto out;
9352 trans = btrfs_start_transaction(root, 1);
9353 if (IS_ERR(trans)) {
9354 ret = PTR_ERR(trans);
9355 goto out;
9356 }
9357 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9358 }
9359
Yan, Zheng80825102009-11-12 09:35:36 +00009360 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009361 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009362 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009363 if (ret)
9364 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009365 }
9366
Chris Mason917c16b2011-11-08 14:49:59 -05009367 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009368 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009369 ret = btrfs_update_inode(trans, root, inode);
9370 if (ret && !err)
9371 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009372
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009373 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009374 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009375 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009376out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009377 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009378
Josef Bacik3893e332011-01-31 16:03:11 -05009379 if (ret && !err)
9380 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009381
Josef Bacik3893e332011-01-31 16:03:11 -05009382 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009383}
9384
Sven Wegener3b963622008-06-09 21:57:42 -04009385/*
Chris Masond352ac62008-09-29 15:18:18 -04009386 * create a new subvolume directory/inode (helper for the ioctl).
9387 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009388int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009389 struct btrfs_root *new_root,
9390 struct btrfs_root *parent_root,
9391 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009392{
Chris Mason39279cc2007-06-12 06:35:45 -04009393 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009394 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009395 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009396
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009397 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9398 new_dirid, new_dirid,
9399 S_IFDIR | (~current_umask() & S_IRWXUGO),
9400 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009401 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009402 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009403 inode->i_op = &btrfs_dir_inode_operations;
9404 inode->i_fop = &btrfs_dir_file_operations;
9405
Miklos Szeredibfe86842011-10-28 14:13:29 +02009406 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009407 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009408 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009409
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009410 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9411 if (err)
9412 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009413 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009414 new_root->root_key.objectid, err);
9415
Yan, Zheng76dda932009-09-21 16:00:26 -04009416 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009417
Yan, Zheng76dda932009-09-21 16:00:26 -04009418 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009419 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009420}
9421
Chris Mason39279cc2007-06-12 06:35:45 -04009422struct inode *btrfs_alloc_inode(struct super_block *sb)
9423{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009424 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009425 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009426 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009427
David Sterba712e36c2017-10-31 17:08:27 +01009428 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009429 if (!ei)
9430 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009431
9432 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009433 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009434 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009435 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009436 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009437 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009438 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009439 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009440 ei->disk_i_size = 0;
9441 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009442 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009443 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009444 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009445 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009446 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009447 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009448
Josef Bacik9e0baf62011-07-15 15:16:44 +00009449 spin_lock_init(&ei->lock);
9450 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009451 if (sb->s_magic != BTRFS_TEST_MAGIC)
9452 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9453 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009454 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009455 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009456 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009457
Miao Xie16cdcec2011-04-22 18:12:22 +08009458 ei->delayed_node = NULL;
9459
chandan r9cc97d62012-07-04 12:48:07 +05309460 ei->i_otime.tv_sec = 0;
9461 ei->i_otime.tv_nsec = 0;
9462
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009463 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009464 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009465 extent_io_tree_init(&ei->io_tree, inode);
9466 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009467 ei->io_tree.track_uptodate = 1;
9468 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009469 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009470 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009471 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009472 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009473 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009474 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009475 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009476 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009477
9478 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009479}
9480
Josef Bacikaaedb552013-10-11 14:44:09 -04009481#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9482void btrfs_test_destroy_inode(struct inode *inode)
9483{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009484 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009485 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9486}
9487#endif
9488
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009489static void btrfs_i_callback(struct rcu_head *head)
9490{
9491 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009492 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9493}
9494
Chris Mason39279cc2007-06-12 06:35:45 -04009495void btrfs_destroy_inode(struct inode *inode)
9496{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009497 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009498 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009499 struct btrfs_root *root = BTRFS_I(inode)->root;
9500
Al Virob3d9b7a2012-06-09 13:51:19 -04009501 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009502 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009503 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9504 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009505 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009506 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009507 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009508 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009509 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009510
Chris Mason5a3f23d2009-03-31 13:27:11 -04009511 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009512 * This can happen where we create an inode, but somebody else also
9513 * created the same inode and we need to destroy the one we already
9514 * created.
9515 */
9516 if (!root)
9517 goto free;
9518
Josef Bacik8a35d952012-05-23 14:26:42 -04009519 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9520 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009521 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009522 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009523 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009524 }
Josef Bacik7b128762008-07-24 12:17:14 -04009525
Chris Masond3977122009-01-05 21:25:51 -05009526 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009527 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9528 if (!ordered)
9529 break;
9530 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009531 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009532 "found ordered extent %llu %llu on inode cleanup",
9533 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009534 btrfs_remove_ordered_extent(inode, ordered);
9535 btrfs_put_ordered_extent(ordered);
9536 btrfs_put_ordered_extent(ordered);
9537 }
9538 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009539 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009540 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009541 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009542free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009543 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009544}
9545
Al Viro45321ac2010-06-07 13:43:19 -04009546int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009547{
9548 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009549
Naohiro Aota6379ef92013-06-06 09:56:34 +00009550 if (root == NULL)
9551 return 1;
9552
Liu Bofa6ac872013-02-20 14:10:23 +00009553 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009554 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009555 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009556 else
Al Viro45321ac2010-06-07 13:43:19 -04009557 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009558}
9559
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009560static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009561{
9562 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9563
9564 inode_init_once(&ei->vfs_inode);
9565}
9566
9567void btrfs_destroy_cachep(void)
9568{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009569 /*
9570 * Make sure all delayed rcu free inodes are flushed before we
9571 * destroy cache.
9572 */
9573 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009574 kmem_cache_destroy(btrfs_inode_cachep);
9575 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009576 kmem_cache_destroy(btrfs_path_cachep);
9577 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009578}
9579
Liu Bof5c29bd2017-11-02 17:21:50 -06009580int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009581{
David Sterba837e1972012-09-07 03:00:48 -06009582 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009583 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009584 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9585 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009586 if (!btrfs_inode_cachep)
9587 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009588
David Sterba837e1972012-09-07 03:00:48 -06009589 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009590 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009591 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009592 if (!btrfs_trans_handle_cachep)
9593 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009594
David Sterba837e1972012-09-07 03:00:48 -06009595 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009596 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009597 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009598 if (!btrfs_path_cachep)
9599 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009600
David Sterba837e1972012-09-07 03:00:48 -06009601 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009602 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009603 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009604 if (!btrfs_free_space_cachep)
9605 goto fail;
9606
Chris Mason39279cc2007-06-12 06:35:45 -04009607 return 0;
9608fail:
9609 btrfs_destroy_cachep();
9610 return -ENOMEM;
9611}
9612
David Howellsa528d352017-01-31 16:46:22 +00009613static int btrfs_getattr(const struct path *path, struct kstat *stat,
9614 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009615{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009616 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009617 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009618 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009619 u32 bi_flags = BTRFS_I(inode)->flags;
9620
9621 stat->result_mask |= STATX_BTIME;
9622 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9623 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9624 if (bi_flags & BTRFS_INODE_APPEND)
9625 stat->attributes |= STATX_ATTR_APPEND;
9626 if (bi_flags & BTRFS_INODE_COMPRESS)
9627 stat->attributes |= STATX_ATTR_COMPRESSED;
9628 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9629 stat->attributes |= STATX_ATTR_IMMUTABLE;
9630 if (bi_flags & BTRFS_INODE_NODUMP)
9631 stat->attributes |= STATX_ATTR_NODUMP;
9632
9633 stat->attributes_mask |= (STATX_ATTR_APPEND |
9634 STATX_ATTR_COMPRESSED |
9635 STATX_ATTR_IMMUTABLE |
9636 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009637
Chris Mason39279cc2007-06-12 06:35:45 -04009638 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009639 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009640
9641 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009642 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009643 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009644 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009645 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009646 return 0;
9647}
9648
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009649static int btrfs_rename_exchange(struct inode *old_dir,
9650 struct dentry *old_dentry,
9651 struct inode *new_dir,
9652 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009653{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009654 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009655 struct btrfs_trans_handle *trans;
9656 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009657 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009658 struct inode *new_inode = new_dentry->d_inode;
9659 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009660 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009661 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009662 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9663 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664 u64 old_idx = 0;
9665 u64 new_idx = 0;
9666 u64 root_objectid;
9667 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009668 bool root_log_pinned = false;
9669 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009670
9671 /* we only allow rename subvolume link between subvolumes */
9672 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9673 return -EXDEV;
9674
9675 /* close the race window with snapshot create/destroy ioctl */
9676 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009677 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009678 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009679 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009680
9681 /*
9682 * We want to reserve the absolute worst case amount of items. So if
9683 * both inodes are subvols and we need to unlink them then that would
9684 * require 4 item modifications, but if they are both normal inodes it
9685 * would require 5 item modifications, so we'll assume their normal
9686 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9687 * should cover the worst case number of items we'll modify.
9688 */
9689 trans = btrfs_start_transaction(root, 12);
9690 if (IS_ERR(trans)) {
9691 ret = PTR_ERR(trans);
9692 goto out_notrans;
9693 }
9694
9695 /*
9696 * We need to find a free sequence number both in the source and
9697 * in the destination directory for the exchange.
9698 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009699 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009700 if (ret)
9701 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009702 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009703 if (ret)
9704 goto out_fail;
9705
9706 BTRFS_I(old_inode)->dir_index = 0ULL;
9707 BTRFS_I(new_inode)->dir_index = 0ULL;
9708
9709 /* Reference for the source. */
9710 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9711 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009712 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009713 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009714 btrfs_pin_log_trans(root);
9715 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 ret = btrfs_insert_inode_ref(trans, dest,
9717 new_dentry->d_name.name,
9718 new_dentry->d_name.len,
9719 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009720 btrfs_ino(BTRFS_I(new_dir)),
9721 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009722 if (ret)
9723 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009724 }
9725
9726 /* And now for the dest. */
9727 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9728 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009729 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009730 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009731 btrfs_pin_log_trans(dest);
9732 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009733 ret = btrfs_insert_inode_ref(trans, root,
9734 old_dentry->d_name.name,
9735 old_dentry->d_name.len,
9736 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009737 btrfs_ino(BTRFS_I(old_dir)),
9738 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009739 if (ret)
9740 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009741 }
9742
9743 /* Update inode version and ctime/mtime. */
9744 inode_inc_iversion(old_dir);
9745 inode_inc_iversion(new_dir);
9746 inode_inc_iversion(old_inode);
9747 inode_inc_iversion(new_inode);
9748 old_dir->i_ctime = old_dir->i_mtime = ctime;
9749 new_dir->i_ctime = new_dir->i_mtime = ctime;
9750 old_inode->i_ctime = ctime;
9751 new_inode->i_ctime = ctime;
9752
9753 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009754 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9755 BTRFS_I(old_inode), 1);
9756 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9757 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009758 }
9759
9760 /* src is a subvolume */
9761 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9762 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9763 ret = btrfs_unlink_subvol(trans, root, old_dir,
9764 root_objectid,
9765 old_dentry->d_name.name,
9766 old_dentry->d_name.len);
9767 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009768 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9769 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009770 old_dentry->d_name.name,
9771 old_dentry->d_name.len);
9772 if (!ret)
9773 ret = btrfs_update_inode(trans, root, old_inode);
9774 }
9775 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009776 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009777 goto out_fail;
9778 }
9779
9780 /* dest is a subvolume */
9781 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9782 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9783 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9784 root_objectid,
9785 new_dentry->d_name.name,
9786 new_dentry->d_name.len);
9787 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009788 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9789 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009790 new_dentry->d_name.name,
9791 new_dentry->d_name.len);
9792 if (!ret)
9793 ret = btrfs_update_inode(trans, dest, new_inode);
9794 }
9795 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009796 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009797 goto out_fail;
9798 }
9799
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009800 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009801 new_dentry->d_name.name,
9802 new_dentry->d_name.len, 0, old_idx);
9803 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009804 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009805 goto out_fail;
9806 }
9807
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009808 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009809 old_dentry->d_name.name,
9810 old_dentry->d_name.len, 0, new_idx);
9811 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009812 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009813 goto out_fail;
9814 }
9815
9816 if (old_inode->i_nlink == 1)
9817 BTRFS_I(old_inode)->dir_index = old_idx;
9818 if (new_inode->i_nlink == 1)
9819 BTRFS_I(new_inode)->dir_index = new_idx;
9820
Filipe Manana86e8aa02016-05-05 02:02:27 +01009821 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009822 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009823 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9824 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009825 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009826 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009827 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009828 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009829 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009830 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9831 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009832 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009833 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009834 }
9835out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009836 /*
9837 * If we have pinned a log and an error happened, we unpin tasks
9838 * trying to sync the log and force them to fallback to a transaction
9839 * commit if the log currently contains any of the inodes involved in
9840 * this rename operation (to ensure we do not persist a log with an
9841 * inconsistent state for any of these inodes or leading to any
9842 * inconsistencies when replayed). If the transaction was aborted, the
9843 * abortion reason is propagated to userspace when attempting to commit
9844 * the transaction. If the log does not contain any of these inodes, we
9845 * allow the tasks to sync it.
9846 */
9847 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009848 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9849 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9850 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009851 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009852 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009853 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009854
9855 if (root_log_pinned) {
9856 btrfs_end_log_trans(root);
9857 root_log_pinned = false;
9858 }
9859 if (dest_log_pinned) {
9860 btrfs_end_log_trans(dest);
9861 dest_log_pinned = false;
9862 }
9863 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009864 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009865out_notrans:
9866 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009867 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009868 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009869 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009870
9871 return ret;
9872}
9873
9874static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9875 struct btrfs_root *root,
9876 struct inode *dir,
9877 struct dentry *dentry)
9878{
9879 int ret;
9880 struct inode *inode;
9881 u64 objectid;
9882 u64 index;
9883
9884 ret = btrfs_find_free_ino(root, &objectid);
9885 if (ret)
9886 return ret;
9887
9888 inode = btrfs_new_inode(trans, root, dir,
9889 dentry->d_name.name,
9890 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009891 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009892 objectid,
9893 S_IFCHR | WHITEOUT_MODE,
9894 &index);
9895
9896 if (IS_ERR(inode)) {
9897 ret = PTR_ERR(inode);
9898 return ret;
9899 }
9900
9901 inode->i_op = &btrfs_special_inode_operations;
9902 init_special_inode(inode, inode->i_mode,
9903 WHITEOUT_DEV);
9904
9905 ret = btrfs_init_inode_security(trans, inode, dir,
9906 &dentry->d_name);
9907 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009908 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009909
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009910 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9911 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009912 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009913 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009914
9915 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009916out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009917 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009918 if (ret)
9919 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009920 iput(inode);
9921
Filipe Mananac9901612016-05-05 01:41:57 +01009922 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009923}
9924
Chris Mason39279cc2007-06-12 06:35:45 -04009925static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009926 struct inode *new_dir, struct dentry *new_dentry,
9927 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009928{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009929 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009930 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009931 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009932 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9933 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009934 struct inode *new_inode = d_inode(new_dentry);
9935 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009936 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009937 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009938 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009939 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009940 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009941
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009942 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009943 return -EPERM;
9944
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009945 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009946 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009947 return -EXDEV;
9948
Li Zefan33345d012011-04-20 10:31:50 +08009949 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009950 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009951 return -ENOTEMPTY;
9952
Chris Mason39279cc2007-06-12 06:35:45 -04009953 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009954 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009955 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009956
9957
9958 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009959 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009960 new_dentry->d_name.name,
9961 new_dentry->d_name.len);
9962
9963 if (ret) {
9964 if (ret == -EEXIST) {
9965 /* we shouldn't get
9966 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309967 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009968 return ret;
9969 }
9970 } else {
9971 /* maybe -EOVERFLOW */
9972 return ret;
9973 }
9974 }
9975 ret = 0;
9976
Chris Mason5a3f23d2009-03-31 13:27:11 -04009977 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009978 * we're using rename to replace one file with another. Start IO on it
9979 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009980 */
Chris Mason8d875f92014-08-12 10:47:42 -07009981 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009982 filemap_flush(old_inode->i_mapping);
9983
Yan, Zheng76dda932009-09-21 16:00:26 -04009984 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009985 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009986 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009987 /*
9988 * We want to reserve the absolute worst case amount of items. So if
9989 * both inodes are subvols and we need to unlink them then that would
9990 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009991 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009992 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9993 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009994 * If our rename has the whiteout flag, we need more 5 units for the
9995 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9996 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009997 */
Filipe Manana5062af32016-05-05 10:26:26 +01009998 trans_num_items = 11;
9999 if (flags & RENAME_WHITEOUT)
10000 trans_num_items += 5;
10001 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010002 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010003 ret = PTR_ERR(trans);
10004 goto out_notrans;
10005 }
Chris Mason5f39d392007-10-15 16:14:19 -040010006
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010007 if (dest != root)
10008 btrfs_record_root_in_trans(trans, dest);
10009
Nikolay Borisov877574e2017-02-20 13:50:33 +020010010 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010011 if (ret)
10012 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -040010013
Miao Xie67de1172013-12-26 13:07:06 +080010014 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +080010015 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010016 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010017 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010018 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +010010019 btrfs_pin_log_trans(root);
10020 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -040010021 ret = btrfs_insert_inode_ref(trans, dest,
10022 new_dentry->d_name.name,
10023 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +080010024 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010025 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010026 if (ret)
10027 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010028 }
Chris Mason5a3f23d2009-03-31 13:27:11 -040010029
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010030 inode_inc_iversion(old_dir);
10031 inode_inc_iversion(new_dir);
10032 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010033 old_dir->i_ctime = old_dir->i_mtime =
10034 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010035 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -040010036
Chris Mason12fcfd22009-03-24 10:24:20 -040010037 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +010010038 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10039 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -040010040
Li Zefan33345d012011-04-20 10:31:50 +080010041 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010042 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
10043 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
10044 old_dentry->d_name.name,
10045 old_dentry->d_name.len);
10046 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010047 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10048 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +000010049 old_dentry->d_name.name,
10050 old_dentry->d_name.len);
10051 if (!ret)
10052 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010053 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010054 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010055 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010056 goto out_fail;
10057 }
Chris Mason39279cc2007-06-12 06:35:45 -040010058
10059 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010060 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010061 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010062 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010063 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10064 root_objectid = BTRFS_I(new_inode)->location.objectid;
10065 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10066 root_objectid,
10067 new_dentry->d_name.name,
10068 new_dentry->d_name.len);
10069 BUG_ON(new_inode->i_nlink == 0);
10070 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010071 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10072 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010073 new_dentry->d_name.name,
10074 new_dentry->d_name.len);
10075 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010076 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010077 ret = btrfs_orphan_add(trans,
10078 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010079 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010080 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010081 goto out_fail;
10082 }
Chris Mason39279cc2007-06-12 06:35:45 -040010083 }
Josef Bacikaec74772008-07-24 12:12:38 -040010084
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010085 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010086 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010087 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010088 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010089 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010090 goto out_fail;
10091 }
Chris Mason39279cc2007-06-12 06:35:45 -040010092
Miao Xie67de1172013-12-26 13:07:06 +080010093 if (old_inode->i_nlink == 1)
10094 BTRFS_I(old_inode)->dir_index = index;
10095
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010096 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010097 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010098
David Sterbaf85b7372017-01-20 14:54:07 +010010099 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10100 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010101 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010102 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010103 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010104
10105 if (flags & RENAME_WHITEOUT) {
10106 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10107 old_dentry);
10108
10109 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010110 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010111 goto out_fail;
10112 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010113 }
Chris Mason39279cc2007-06-12 06:35:45 -040010114out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010115 /*
10116 * If we have pinned the log and an error happened, we unpin tasks
10117 * trying to sync the log and force them to fallback to a transaction
10118 * commit if the log currently contains any of the inodes involved in
10119 * this rename operation (to ensure we do not persist a log with an
10120 * inconsistent state for any of these inodes or leading to any
10121 * inconsistencies when replayed). If the transaction was aborted, the
10122 * abortion reason is propagated to userspace when attempting to commit
10123 * the transaction. If the log does not contain any of these inodes, we
10124 * allow the tasks to sync it.
10125 */
10126 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010127 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10128 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10129 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010130 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010131 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010132 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010133
10134 btrfs_end_log_trans(root);
10135 log_pinned = false;
10136 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010137 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010138out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010139 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010140 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010141
Chris Mason39279cc2007-06-12 06:35:45 -040010142 return ret;
10143}
10144
Miklos Szeredi80ace852014-07-23 15:15:32 +020010145static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10146 struct inode *new_dir, struct dentry *new_dentry,
10147 unsigned int flags)
10148{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010149 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010150 return -EINVAL;
10151
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010152 if (flags & RENAME_EXCHANGE)
10153 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10154 new_dentry);
10155
10156 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010157}
10158
Miao Xie8ccf6f192012-10-25 09:28:04 +000010159static void btrfs_run_delalloc_work(struct btrfs_work *work)
10160{
10161 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010162 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010163
10164 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10165 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010166 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010167 filemap_flush(inode->i_mapping);
10168 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10169 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010170 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010171
10172 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010173 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010174 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010175 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010176 complete(&delalloc_work->completion);
10177}
10178
10179struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010180 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010181{
10182 struct btrfs_delalloc_work *work;
10183
David Sterba100d5702015-12-08 14:39:32 +010010184 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010185 if (!work)
10186 return NULL;
10187
10188 init_completion(&work->completion);
10189 INIT_LIST_HEAD(&work->list);
10190 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010191 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010192 WARN_ON_ONCE(!inode);
10193 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10194 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010195
10196 return work;
10197}
10198
10199void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10200{
10201 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010202 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010203}
10204
Chris Masond352ac62008-09-29 15:18:18 -040010205/*
10206 * some fairly slow code that needs optimization. This walks the list
10207 * of all the inodes with pending delalloc and forces them to disk.
10208 */
Miao Xie6c255e62014-03-06 13:55:01 +080010209static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10210 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010211{
Chris Masonea8c2812008-08-04 23:17:27 -040010212 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010213 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010214 struct btrfs_delalloc_work *work, *next;
10215 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010216 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010217 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010218
Miao Xie8ccf6f192012-10-25 09:28:04 +000010219 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010220 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010221
Miao Xie573bfb72014-03-06 13:55:03 +080010222 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010223 spin_lock(&root->delalloc_lock);
10224 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010225 while (!list_empty(&splice)) {
10226 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010227 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010228
Miao Xieeb73c1b2013-05-15 07:48:22 +000010229 list_move_tail(&binode->delalloc_inodes,
10230 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010231 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010232 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010233 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010234 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010235 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010236 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010237
David Sterba651d4942015-11-27 19:24:16 +010010238 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010239 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010240 if (delay_iput)
10241 btrfs_add_delayed_iput(inode);
10242 else
10243 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010244 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010245 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010246 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010247 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010248 btrfs_queue_work(root->fs_info->flush_workers,
10249 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010250 ret++;
10251 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010252 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010253 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010254 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010255 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010256 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010257
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010258out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010259 list_for_each_entry_safe(work, next, &works, list) {
10260 list_del_init(&work->list);
10261 btrfs_wait_and_free_delalloc_work(work);
10262 }
10263
Miao Xieeb73c1b2013-05-15 07:48:22 +000010264 if (!list_empty_careful(&splice)) {
10265 spin_lock(&root->delalloc_lock);
10266 list_splice_tail(&splice, &root->delalloc_inodes);
10267 spin_unlock(&root->delalloc_lock);
10268 }
Miao Xie573bfb72014-03-06 13:55:03 +080010269 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010270 return ret;
10271}
10272
10273int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10274{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010275 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010276 int ret;
10277
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010278 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010279 return -EROFS;
10280
Miao Xie6c255e62014-03-06 13:55:01 +080010281 ret = __start_delalloc_inodes(root, delay_iput, -1);
10282 if (ret > 0)
10283 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010284 return ret;
10285}
10286
Miao Xie6c255e62014-03-06 13:55:01 +080010287int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10288 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010289{
10290 struct btrfs_root *root;
10291 struct list_head splice;
10292 int ret;
10293
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010294 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010295 return -EROFS;
10296
10297 INIT_LIST_HEAD(&splice);
10298
Miao Xie573bfb72014-03-06 13:55:03 +080010299 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010300 spin_lock(&fs_info->delalloc_root_lock);
10301 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010302 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010303 root = list_first_entry(&splice, struct btrfs_root,
10304 delalloc_root);
10305 root = btrfs_grab_fs_root(root);
10306 BUG_ON(!root);
10307 list_move_tail(&root->delalloc_root,
10308 &fs_info->delalloc_roots);
10309 spin_unlock(&fs_info->delalloc_root_lock);
10310
Miao Xie6c255e62014-03-06 13:55:01 +080010311 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010312 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010313 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010314 goto out;
10315
Miao Xie6c255e62014-03-06 13:55:01 +080010316 if (nr != -1) {
10317 nr -= ret;
10318 WARN_ON(nr < 0);
10319 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010320 spin_lock(&fs_info->delalloc_root_lock);
10321 }
10322 spin_unlock(&fs_info->delalloc_root_lock);
10323
Miao Xie6c255e62014-03-06 13:55:01 +080010324 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010325out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010326 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010327 spin_lock(&fs_info->delalloc_root_lock);
10328 list_splice_tail(&splice, &fs_info->delalloc_roots);
10329 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010330 }
Miao Xie573bfb72014-03-06 13:55:03 +080010331 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010332 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010333}
10334
Chris Mason39279cc2007-06-12 06:35:45 -040010335static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10336 const char *symname)
10337{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010338 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010339 struct btrfs_trans_handle *trans;
10340 struct btrfs_root *root = BTRFS_I(dir)->root;
10341 struct btrfs_path *path;
10342 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010343 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010344 int err;
10345 int drop_inode = 0;
10346 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010347 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010348 int name_len;
10349 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010350 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010351 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010352 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010353
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010354 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010355 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010356 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010357
Josef Bacik9ed74f22009-09-11 16:12:44 -040010358 /*
10359 * 2 items for inode item and ref
10360 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010361 * 1 item for updating parent inode item
10362 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010363 * 1 item for xattr if selinux is on
10364 */
Filipe Manana9269d122015-12-31 18:16:29 +000010365 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010366 if (IS_ERR(trans))
10367 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010368
Li Zefan581bb052011-04-20 10:06:11 +080010369 err = btrfs_find_free_ino(root, &objectid);
10370 if (err)
10371 goto out_unlock;
10372
Josef Bacikaec74772008-07-24 12:12:38 -040010373 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010374 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10375 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010376 if (IS_ERR(inode)) {
10377 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010378 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010379 }
Chris Mason39279cc2007-06-12 06:35:45 -040010380
Casey Schauflerad19db72011-12-15 10:09:07 -050010381 /*
10382 * If the active LSM wants to access the inode during
10383 * d_instantiate it needs these. Smack checks to see
10384 * if the filesystem supports xattrs by looking at the
10385 * ops vector.
10386 */
10387 inode->i_fop = &btrfs_file_operations;
10388 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010389 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010390 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10391
10392 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10393 if (err)
10394 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010395
Chris Mason39279cc2007-06-12 06:35:45 -040010396 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010397 if (!path) {
10398 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010399 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010400 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010401 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010402 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010403 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010404 datasize = btrfs_file_extent_calc_inline_size(name_len);
10405 err = btrfs_insert_empty_item(trans, root, path, &key,
10406 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010407 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010408 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010409 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010410 }
Chris Mason5f39d392007-10-15 16:14:19 -040010411 leaf = path->nodes[0];
10412 ei = btrfs_item_ptr(leaf, path->slots[0],
10413 struct btrfs_file_extent_item);
10414 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10415 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010416 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010417 btrfs_set_file_extent_encryption(leaf, ei, 0);
10418 btrfs_set_file_extent_compression(leaf, ei, 0);
10419 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10420 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10421
Chris Mason39279cc2007-06-12 06:35:45 -040010422 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010423 write_extent_buffer(leaf, symname, ptr, name_len);
10424 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010425 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010426
Chris Mason39279cc2007-06-12 06:35:45 -040010427 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010428 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010429 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010430 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010431 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010432 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010433 /*
10434 * Last step, add directory indexes for our symlink inode. This is the
10435 * last step to avoid extra cleanup of these indexes if an error happens
10436 * elsewhere above.
10437 */
10438 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010439 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10440 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010441 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010442 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010443 goto out_unlock_inode;
10444 }
10445
10446 unlock_new_inode(inode);
10447 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010448
10449out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010450 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010451 if (drop_inode) {
10452 inode_dec_link_count(inode);
10453 iput(inode);
10454 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010455 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010456 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010457
10458out_unlock_inode:
10459 drop_inode = 1;
10460 unlock_new_inode(inode);
10461 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010462}
Chris Mason16432982008-04-10 10:23:21 -040010463
Josef Bacik0af3d002010-06-21 14:48:16 -040010464static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10465 u64 start, u64 num_bytes, u64 min_size,
10466 loff_t actual_len, u64 *alloc_hint,
10467 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010468{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010469 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010470 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10471 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010472 struct btrfs_root *root = BTRFS_I(inode)->root;
10473 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010474 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010475 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010476 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010477 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010478 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010479 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010480 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010481
Josef Bacik0af3d002010-06-21 14:48:16 -040010482 if (trans)
10483 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010484 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010485 if (own_trans) {
10486 trans = btrfs_start_transaction(root, 3);
10487 if (IS_ERR(trans)) {
10488 ret = PTR_ERR(trans);
10489 break;
10490 }
Yan Zhengd899e052008-10-30 14:25:28 -040010491 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010492
Byongho Leeee221842015-12-15 01:42:10 +090010493 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010494 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010495 /*
10496 * If we are severely fragmented we could end up with really
10497 * small allocations, so if the allocator is returning small
10498 * chunks lets make its job easier by only searching for those
10499 * sized chunks.
10500 */
10501 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010502 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10503 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010504 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010505 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010506 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010507 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010508 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010509 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010510
Josef Bacik0b670dc2015-09-23 17:11:16 -040010511 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010512 ret = insert_reserved_file_extent(trans, inode,
10513 cur_offset, ins.objectid,
10514 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010515 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010516 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010517 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010518 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010519 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010520 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010521 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010522 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010523 break;
10524 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010525
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010526 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010527 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010528
Josef Bacik5dc562c2012-08-17 13:14:17 -040010529 em = alloc_extent_map();
10530 if (!em) {
10531 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10532 &BTRFS_I(inode)->runtime_flags);
10533 goto next;
10534 }
10535
10536 em->start = cur_offset;
10537 em->orig_start = cur_offset;
10538 em->len = ins.offset;
10539 em->block_start = ins.objectid;
10540 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010541 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010542 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010543 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010544 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10545 em->generation = trans->transid;
10546
10547 while (1) {
10548 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010549 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010550 write_unlock(&em_tree->lock);
10551 if (ret != -EEXIST)
10552 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010553 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010554 cur_offset + ins.offset - 1,
10555 0);
10556 }
10557 free_extent_map(em);
10558next:
Yan Zhengd899e052008-10-30 14:25:28 -040010559 num_bytes -= ins.offset;
10560 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010561 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010562
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010563 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010564 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010565 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010566 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010567 (actual_len > inode->i_size) &&
10568 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010569 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010570 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010571 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010572 i_size = cur_offset;
10573 i_size_write(inode, i_size);
10574 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010575 }
10576
Yan Zhengd899e052008-10-30 14:25:28 -040010577 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010578
10579 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010580 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010581 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010582 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010583 break;
10584 }
Yan Zhengd899e052008-10-30 14:25:28 -040010585
Josef Bacik0af3d002010-06-21 14:48:16 -040010586 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010587 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010588 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010589 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010590 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010591 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010592 return ret;
10593}
10594
Josef Bacik0af3d002010-06-21 14:48:16 -040010595int btrfs_prealloc_file_range(struct inode *inode, int mode,
10596 u64 start, u64 num_bytes, u64 min_size,
10597 loff_t actual_len, u64 *alloc_hint)
10598{
10599 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10600 min_size, actual_len, alloc_hint,
10601 NULL);
10602}
10603
10604int btrfs_prealloc_file_range_trans(struct inode *inode,
10605 struct btrfs_trans_handle *trans, int mode,
10606 u64 start, u64 num_bytes, u64 min_size,
10607 loff_t actual_len, u64 *alloc_hint)
10608{
10609 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10610 min_size, actual_len, alloc_hint, trans);
10611}
10612
Chris Masone6dcd2d2008-07-17 12:53:50 -040010613static int btrfs_set_page_dirty(struct page *page)
10614{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010615 return __set_page_dirty_nobuffers(page);
10616}
10617
Al Viro10556cb2011-06-20 19:28:19 -040010618static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010619{
Li Zefanb83cc962010-12-20 16:04:08 +080010620 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010621 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010622
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010623 if (mask & MAY_WRITE &&
10624 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10625 if (btrfs_root_readonly(root))
10626 return -EROFS;
10627 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10628 return -EACCES;
10629 }
Al Viro2830ba72011-06-20 19:16:29 -040010630 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010631}
Chris Mason39279cc2007-06-12 06:35:45 -040010632
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010633static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10634{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010635 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010636 struct btrfs_trans_handle *trans;
10637 struct btrfs_root *root = BTRFS_I(dir)->root;
10638 struct inode *inode = NULL;
10639 u64 objectid;
10640 u64 index;
10641 int ret = 0;
10642
10643 /*
10644 * 5 units required for adding orphan entry
10645 */
10646 trans = btrfs_start_transaction(root, 5);
10647 if (IS_ERR(trans))
10648 return PTR_ERR(trans);
10649
10650 ret = btrfs_find_free_ino(root, &objectid);
10651 if (ret)
10652 goto out;
10653
10654 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010655 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010656 if (IS_ERR(inode)) {
10657 ret = PTR_ERR(inode);
10658 inode = NULL;
10659 goto out;
10660 }
10661
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010662 inode->i_fop = &btrfs_file_operations;
10663 inode->i_op = &btrfs_file_inode_operations;
10664
10665 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010666 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10667
Chris Masonb0d5d102014-09-08 13:08:51 -070010668 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10669 if (ret)
10670 goto out_inode;
10671
10672 ret = btrfs_update_inode(trans, root, inode);
10673 if (ret)
10674 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010675 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010676 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010677 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010678
Filipe Manana5762b5c2014-08-01 00:10:32 +010010679 /*
10680 * We set number of links to 0 in btrfs_new_inode(), and here we set
10681 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10682 * through:
10683 *
10684 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10685 */
10686 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010687 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010688 d_tmpfile(dentry, inode);
10689 mark_inode_dirty(inode);
10690
10691out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010692 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010693 if (ret)
10694 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010695 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010696 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010697
10698out_inode:
10699 unlock_new_inode(inode);
10700 goto out;
10701
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010702}
10703
David Sterba20a7db82017-02-17 16:24:29 +010010704__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010705static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010706{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010707 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010708}
10709
Josef Bacikc6100a42017-05-05 11:57:13 -040010710static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10711{
10712 struct inode *inode = private_data;
10713 return btrfs_sb(inode->i_sb);
10714}
10715
10716static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10717 u64 start, u64 end)
10718{
10719 struct inode *inode = private_data;
10720 u64 isize;
10721
10722 isize = i_size_read(inode);
10723 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10724 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10725 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10726 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10727 }
10728}
10729
10730void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10731{
10732 struct inode *inode = private_data;
10733 unsigned long index = start >> PAGE_SHIFT;
10734 unsigned long end_index = end >> PAGE_SHIFT;
10735 struct page *page;
10736
10737 while (index <= end_index) {
10738 page = find_get_page(inode->i_mapping, index);
10739 ASSERT(page); /* Pages should be in the extent_io_tree */
10740 set_page_writeback(page);
10741 put_page(page);
10742 index++;
10743 }
10744}
10745
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010746static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010747 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010748 .lookup = btrfs_lookup,
10749 .create = btrfs_create,
10750 .unlink = btrfs_unlink,
10751 .link = btrfs_link,
10752 .mkdir = btrfs_mkdir,
10753 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010754 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010755 .symlink = btrfs_symlink,
10756 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010757 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010758 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010759 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010760 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010761 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010762 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010763 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010764};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010765static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010766 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010767 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010768 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010769};
Yan, Zheng76dda932009-09-21 16:00:26 -040010770
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010771static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010772 .llseek = generic_file_llseek,
10773 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010774 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010775 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010776 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010777#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010778 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010779#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010780 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010781 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010782};
10783
David Sterba20e55062015-11-19 11:42:28 +010010784static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010785 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010786 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010787 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010788 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010789 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010790 .tree_fs_info = iotree_fs_info,
10791 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010792
10793 /* optional callbacks */
10794 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010795 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010796 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010797 .set_bit_hook = btrfs_set_bit_hook,
10798 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010799 .merge_extent_hook = btrfs_merge_extent_hook,
10800 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010801 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010802};
10803
Chris Mason35054392009-01-21 13:11:13 -050010804/*
10805 * btrfs doesn't support the bmap operation because swapfiles
10806 * use bmap to make a mapping of extents in the file. They assume
10807 * these extents won't change over the life of the file and they
10808 * use the bmap result to do IO directly to the drive.
10809 *
10810 * the btrfs bmap call would return logical addresses that aren't
10811 * suitable for IO and they also will change frequently as COW
10812 * operations happen. So, swapfile + btrfs == corruption.
10813 *
10814 * For now we're avoiding this by dropping bmap.
10815 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010816static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010817 .readpage = btrfs_readpage,
10818 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010819 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010820 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010821 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010822 .invalidatepage = btrfs_invalidatepage,
10823 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010824 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010825 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010826};
10827
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010828static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010829 .readpage = btrfs_readpage,
10830 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010831 .invalidatepage = btrfs_invalidatepage,
10832 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010833};
10834
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010835static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010836 .getattr = btrfs_getattr,
10837 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010838 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010839 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010840 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010841 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010842 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010843 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010844};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010845static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010846 .getattr = btrfs_getattr,
10847 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010848 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010849 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010850 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010851 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010852 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010853};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010854static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010855 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010856 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010857 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010858 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010859 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010860 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010861};
Yan, Zheng76dda932009-09-21 16:00:26 -040010862
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010863const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010864 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010865 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010866};