blob: 034d7333b14dda57829f642c9923e31d7d95faa7 [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.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300539 *
540 * Note that the remaining part is redirtied, the start pointer
541 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400542 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300543 if (!redirty) {
544 extent_range_clear_dirty_for_io(inode, start, end);
545 redirty = 1;
546 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200547
548 /* Compression level is applied here and only here */
549 ret = btrfs_compress_pages(
550 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800551 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100552 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100553 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800554 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100555 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400556
557 if (!ret) {
558 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300559 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100560 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400561 char *kaddr;
562
563 /* zero the tail end of the last page, we might be
564 * sending it down to disk
565 */
566 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800567 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400568 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300569 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800570 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400571 }
572 will_compress = 1;
573 }
574 }
Li Zefan560f7d72011-09-08 10:22:01 +0800575cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400576 if (start == 0) {
577 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300578 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400579 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500580 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400581 */
Josef Bacik00361582013-08-14 14:02:47 -0400582 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800583 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400584 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500585 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400586 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000587 total_compressed,
588 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400589 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100590 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400591 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400592 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
593 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100594 unsigned long page_error_op;
595
Filipe Mananae6eb4312014-10-10 10:45:12 +0100596 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400597
Chris Mason771ed682008-11-06 22:02:51 -0500598 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100599 * inline extent creation worked or returned error,
600 * we don't need to create any more async work items.
601 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400602 *
603 * We use DO_ACCOUNTING here because we need the
604 * delalloc_release_metadata to be done _after_ we drop
605 * our outstanding extent for clearing delalloc for this
606 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500607 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800608 extent_clear_unlock_delalloc(inode, start, end, end,
609 NULL, clear_flags,
610 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400611 PAGE_CLEAR_DIRTY |
612 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100613 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400614 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400615 goto free_pages_out;
616 }
617 }
618
619 if (will_compress) {
620 /*
621 * we aren't doing an inline extent round the compressed size
622 * up to a block size boundary so the allocator does sane
623 * things
624 */
Qu Wenruofda28322013-02-26 08:10:22 +0000625 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400626
627 /*
628 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300629 * win, compare the page count read with the blocks on disk,
630 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400631 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300632 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300633 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700634 *num_added += 1;
635
636 /*
637 * The async work queues will take care of doing actual
638 * allocation on disk for these compressed pages, and
639 * will submit them to the elevator.
640 */
Timofey Titovets11708622017-10-03 18:06:01 +0300641 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100642 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700643 compress_type);
644
Timofey Titovets11708622017-10-03 18:06:01 +0300645 if (start + total_in < end) {
646 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700647 pages = NULL;
648 cond_resched();
649 goto again;
650 }
651 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400652 }
653 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700654 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400655 /*
656 * the compression code ran but failed to make things smaller,
657 * free any pages it allocated and our page pointer array
658 */
David Sterba4d3a8002017-02-14 19:04:07 +0100659 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400660 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300661 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400662 }
663 kfree(pages);
664 pages = NULL;
665 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100666 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400667
668 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400669 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200670 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500671 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500672 }
Chris Masonc8b97812008-10-29 14:49:59 -0400673 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500674cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700675 /*
676 * No compression, but we still need to write the pages in the file
677 * we've been given so far. redirty the locked page if it corresponds
678 * to our extent and set things up for the async work queue to run
679 * cow_file_range to do the normal delalloc dance.
680 */
681 if (page_offset(locked_page) >= start &&
682 page_offset(locked_page) <= end)
683 __set_page_dirty_nobuffers(locked_page);
684 /* unlocked later on in the async handlers */
685
686 if (redirty)
687 extent_range_redirty_for_io(inode, start, end);
688 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
689 BTRFS_COMPRESS_NONE);
690 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500691
Filipe Mananac44f6492014-10-09 21:15:44 +0100692 return;
Chris Mason771ed682008-11-06 22:02:51 -0500693
694free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100695 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500696 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300697 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500698 }
Chris Masond3977122009-01-05 21:25:51 -0500699 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500700}
Chris Mason771ed682008-11-06 22:02:51 -0500701
Filipe Manana40ae8372014-10-06 22:14:24 +0100702static void free_async_extent_pages(struct async_extent *async_extent)
703{
704 int i;
705
706 if (!async_extent->pages)
707 return;
708
709 for (i = 0; i < async_extent->nr_pages; i++) {
710 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300711 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100712 }
713 kfree(async_extent->pages);
714 async_extent->nr_pages = 0;
715 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500716}
717
718/*
719 * phase two of compressed writeback. This is the ordered portion
720 * of the code, which only gets called in the order the work was
721 * queued. We walk all the async extents created by compress_file_range
722 * and send them down to the disk.
723 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100724static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500725 struct async_cow *async_cow)
726{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400727 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500728 struct async_extent *async_extent;
729 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500730 struct btrfs_key ins;
731 struct extent_map *em;
732 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500733 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500734 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500735
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500736again:
Chris Masond3977122009-01-05 21:25:51 -0500737 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500738 async_extent = list_entry(async_cow->extents.next,
739 struct async_extent, list);
740 list_del(&async_extent->list);
741
742 io_tree = &BTRFS_I(inode)->io_tree;
743
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500744retry:
Chris Mason771ed682008-11-06 22:02:51 -0500745 /* did the compression code fall back to uncompressed IO? */
746 if (!async_extent->pages) {
747 int page_started = 0;
748 unsigned long nr_written = 0;
749
750 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000751 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100752 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500753
754 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500755 ret = cow_file_range(inode, async_cow->locked_page,
756 async_extent->start,
757 async_extent->start +
758 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800759 async_extent->start +
760 async_extent->ram_size - 1,
761 &page_started, &nr_written, 0,
762 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500763
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100764 /* JDM XXX */
765
Chris Mason771ed682008-11-06 22:02:51 -0500766 /*
767 * if page_started, cow_file_range inserted an
768 * inline extent and took care of all the unlocking
769 * and IO for us. Otherwise, we need to submit
770 * all those pages down to the drive.
771 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500772 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500773 extent_write_locked_range(io_tree,
774 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500775 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500776 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500777 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500778 else if (ret)
779 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500780 kfree(async_extent);
781 cond_resched();
782 continue;
783 }
784
785 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100786 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500787
Wang Xiaoguang18513092016-07-25 15:51:40 +0800788 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500789 async_extent->compressed_size,
790 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800791 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500792 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100793 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500794
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400795 if (ret == -ENOSPC) {
796 unlock_extent(io_tree, async_extent->start,
797 async_extent->start +
798 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800799
800 /*
801 * we need to redirty the pages if we decide to
802 * fallback to uncompressed IO, otherwise we
803 * will not submit these pages down to lower
804 * layers.
805 */
806 extent_range_redirty_for_io(inode,
807 async_extent->start,
808 async_extent->start +
809 async_extent->ram_size - 1);
810
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100811 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400812 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500813 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500814 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000815 /*
816 * here we're doing allocation and writeback of the
817 * compressed pages
818 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800819 em = create_io_em(inode, async_extent->start,
820 async_extent->ram_size, /* len */
821 async_extent->start, /* orig_start */
822 ins.objectid, /* block_start */
823 ins.offset, /* block_len */
824 ins.offset, /* orig_block_len */
825 async_extent->ram_size, /* ram_bytes */
826 async_extent->compress_type,
827 BTRFS_ORDERED_COMPRESSED);
828 if (IS_ERR(em))
829 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500830 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800831 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500832
Li Zefan261507a02010-12-17 14:21:50 +0800833 ret = btrfs_add_ordered_extent_compress(inode,
834 async_extent->start,
835 ins.objectid,
836 async_extent->ram_size,
837 ins.offset,
838 BTRFS_ORDERED_COMPRESSED,
839 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100840 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200841 btrfs_drop_extent_cache(BTRFS_I(inode),
842 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100843 async_extent->start +
844 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500845 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100846 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400847 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500848
Chris Mason771ed682008-11-06 22:02:51 -0500849 /*
850 * clear dirty, set writeback and unlock the pages.
851 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400852 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400853 async_extent->start +
854 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800855 async_extent->start +
856 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400857 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
858 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400859 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200860 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500861 async_extent->start,
862 async_extent->ram_size,
863 ins.objectid,
864 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600865 async_extent->nr_pages,
866 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100867 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
868 struct page *p = async_extent->pages[0];
869 const u64 start = async_extent->start;
870 const u64 end = start + async_extent->ram_size - 1;
871
872 p->mapping = inode->i_mapping;
873 tree->ops->writepage_end_io_hook(p, start, end,
874 NULL, 0);
875 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800876 extent_clear_unlock_delalloc(inode, start, end, end,
877 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100878 PAGE_END_WRITEBACK |
879 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100880 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100881 }
Chris Mason771ed682008-11-06 22:02:51 -0500882 alloc_hint = ins.objectid + ins.offset;
883 kfree(async_extent);
884 cond_resched();
885 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100886 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500887out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400888 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400889 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100890out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400891 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500892 async_extent->start +
893 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800894 async_extent->start +
895 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400896 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100897 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400898 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
899 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100900 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
901 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100902 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100903 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500904 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500905}
906
Josef Bacik4b46fce2010-05-23 11:00:55 -0400907static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
908 u64 num_bytes)
909{
910 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
911 struct extent_map *em;
912 u64 alloc_hint = 0;
913
914 read_lock(&em_tree->lock);
915 em = search_extent_mapping(em_tree, start, num_bytes);
916 if (em) {
917 /*
918 * if block start isn't an actual block number then find the
919 * first block in this inode and use that as a hint. If that
920 * block is also bogus then just don't worry about it.
921 */
922 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
923 free_extent_map(em);
924 em = search_extent_mapping(em_tree, 0, 0);
925 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
926 alloc_hint = em->block_start;
927 if (em)
928 free_extent_map(em);
929 } else {
930 alloc_hint = em->block_start;
931 free_extent_map(em);
932 }
933 }
934 read_unlock(&em_tree->lock);
935
936 return alloc_hint;
937}
938
Chris Mason771ed682008-11-06 22:02:51 -0500939/*
940 * when extent_io.c finds a delayed allocation range in the file,
941 * the call backs end up in this code. The basic idea is to
942 * allocate extents on disk for the range, and create ordered data structs
943 * in ram to track those extents.
944 *
945 * locked_page is the page that writepage had locked already. We use
946 * it to make sure we don't do extra locks or unlocks.
947 *
948 * *page_started is set to one if we unlock locked_page and do everything
949 * required to start IO on it. It may be clean and already done with
950 * IO when we return.
951 */
Josef Bacik00361582013-08-14 14:02:47 -0400952static noinline int cow_file_range(struct inode *inode,
953 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800954 u64 start, u64 end, u64 delalloc_end,
955 int *page_started, unsigned long *nr_written,
956 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500957{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400958 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400959 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500960 u64 alloc_hint = 0;
961 u64 num_bytes;
962 unsigned long ram_size;
963 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000964 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400965 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500966 struct btrfs_key ins;
967 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000968 unsigned clear_bits;
969 unsigned long page_ops;
970 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500971 int ret = 0;
972
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200973 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400974 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500975 ret = -EINVAL;
976 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400977 }
Chris Mason771ed682008-11-06 22:02:51 -0500978
Qu Wenruofda28322013-02-26 08:10:22 +0000979 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500980 num_bytes = max(blocksize, num_bytes);
981 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500982
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200983 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400984
Chris Mason771ed682008-11-06 22:02:51 -0500985 if (start == 0) {
986 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800987 ret = cow_file_range_inline(root, inode, start, end, 0,
988 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500989 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400990 /*
991 * We use DO_ACCOUNTING here because we need the
992 * delalloc_release_metadata to be run _after_ we drop
993 * our outstanding extent for clearing delalloc for this
994 * range.
995 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800996 extent_clear_unlock_delalloc(inode, start, end,
997 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400998 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400999 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1000 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001001 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1002 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001003 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001004 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001005 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001006 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001007 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001008 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001009 }
1010 }
Chris Masonc8b97812008-10-29 14:49:59 -04001011
1012 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001013 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001014
Josef Bacik4b46fce2010-05-23 11:00:55 -04001015 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001016 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1017 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001018
Chris Masond3977122009-01-05 21:25:51 -05001019 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +00001020 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001021 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001022 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001023 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001024 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001025 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001026 cur_alloc_size = ins.offset;
1027 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001028
Chris Mason771ed682008-11-06 22:02:51 -05001029 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001030 em = create_io_em(inode, start, ins.offset, /* len */
1031 start, /* orig_start */
1032 ins.objectid, /* block_start */
1033 ins.offset, /* block_len */
1034 ins.offset, /* orig_block_len */
1035 ram_size, /* ram_bytes */
1036 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001037 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001038 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001039 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001040 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001041
Chris Masone6dcd2d2008-07-17 12:53:50 -04001042 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001043 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001044 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001045 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001046
Yan Zheng17d217f2008-12-12 10:03:38 -05001047 if (root->root_key.objectid ==
1048 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1049 ret = btrfs_reloc_clone_csums(inode, start,
1050 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001051 /*
1052 * Only drop cache here, and process as normal.
1053 *
1054 * We must not allow extent_clear_unlock_delalloc()
1055 * at out_unlock label to free meta of this ordered
1056 * extent, as its meta should be freed by
1057 * btrfs_finish_ordered_io().
1058 *
1059 * So we must continue until @start is increased to
1060 * skip current ordered extent.
1061 */
Josef Bacik00361582013-08-14 14:02:47 -04001062 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001063 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1064 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001065 }
1066
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001067 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001068
Chris Masonc8b97812008-10-29 14:49:59 -04001069 /* we're not doing compressed IO, don't unlock the first
1070 * page (which the caller expects to stay locked), don't
1071 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001072 *
1073 * Do set the Private2 bit so we know this page was properly
1074 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001075 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001076 page_ops = unlock ? PAGE_UNLOCK : 0;
1077 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001078
Josef Bacikc2790a22013-07-29 11:20:47 -04001079 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001080 start + ram_size - 1,
1081 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001082 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001083 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001084 if (disk_num_bytes < cur_alloc_size)
1085 disk_num_bytes = 0;
1086 else
1087 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001088 num_bytes -= cur_alloc_size;
1089 alloc_hint = ins.objectid + ins.offset;
1090 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001091 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001092
1093 /*
1094 * btrfs_reloc_clone_csums() error, since start is increased
1095 * extent_clear_unlock_delalloc() at out_unlock label won't
1096 * free metadata of current ordered extent, we're OK to exit.
1097 */
1098 if (ret)
1099 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001100 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001101out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001102 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001103
Filipe Mananad9f85962014-08-25 10:43:00 +01001104out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001105 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001106out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001107 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001108 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001109out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001110 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1111 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001112 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1113 PAGE_END_WRITEBACK;
1114 /*
1115 * If we reserved an extent for our delalloc range (or a subrange) and
1116 * failed to create the respective ordered extent, then it means that
1117 * when we reserved the extent we decremented the extent's size from
1118 * the data space_info's bytes_may_use counter and incremented the
1119 * space_info's bytes_reserved counter by the same amount. We must make
1120 * sure extent_clear_unlock_delalloc() does not try to decrement again
1121 * the data space_info's bytes_may_use counter, therefore we do not pass
1122 * it the flag EXTENT_CLEAR_DATA_RESV.
1123 */
1124 if (extent_reserved) {
1125 extent_clear_unlock_delalloc(inode, start,
1126 start + cur_alloc_size,
1127 start + cur_alloc_size,
1128 locked_page,
1129 clear_bits,
1130 page_ops);
1131 start += cur_alloc_size;
1132 if (start >= end)
1133 goto out;
1134 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001135 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1136 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001137 clear_bits | EXTENT_CLEAR_DATA_RESV,
1138 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001139 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001140}
Chris Masonc8b97812008-10-29 14:49:59 -04001141
Chris Mason771ed682008-11-06 22:02:51 -05001142/*
1143 * work queue call back to started compression on a file and pages
1144 */
1145static noinline void async_cow_start(struct btrfs_work *work)
1146{
1147 struct async_cow *async_cow;
1148 int num_added = 0;
1149 async_cow = container_of(work, struct async_cow, work);
1150
1151 compress_file_range(async_cow->inode, async_cow->locked_page,
1152 async_cow->start, async_cow->end, async_cow,
1153 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001154 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001155 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001156 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001157 }
Chris Mason771ed682008-11-06 22:02:51 -05001158}
1159
1160/*
1161 * work queue call back to submit previously compressed pages
1162 */
1163static noinline void async_cow_submit(struct btrfs_work *work)
1164{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001165 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001166 struct async_cow *async_cow;
1167 struct btrfs_root *root;
1168 unsigned long nr_pages;
1169
1170 async_cow = container_of(work, struct async_cow, work);
1171
1172 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001173 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001174 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1175 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001176
David Sterbaee863952015-02-16 19:41:40 +01001177 /*
1178 * atomic_sub_return implies a barrier for waitqueue_active
1179 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001180 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001181 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001182 waitqueue_active(&fs_info->async_submit_wait))
1183 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001184
Chris Masond3977122009-01-05 21:25:51 -05001185 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001186 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001187}
Chris Masonc8b97812008-10-29 14:49:59 -04001188
Chris Mason771ed682008-11-06 22:02:51 -05001189static noinline void async_cow_free(struct btrfs_work *work)
1190{
1191 struct async_cow *async_cow;
1192 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001193 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001194 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001195 kfree(async_cow);
1196}
1197
1198static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1199 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001200 unsigned long *nr_written,
1201 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001202{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001203 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001204 struct async_cow *async_cow;
1205 struct btrfs_root *root = BTRFS_I(inode)->root;
1206 unsigned long nr_pages;
1207 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001208
Chris Masona3429ab2009-10-08 12:30:20 -04001209 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001210 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001211 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001212 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001213 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001214 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001215 async_cow->root = root;
1216 async_cow->locked_page = locked_page;
1217 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001218 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001219
Wang Shilongf79707b2014-07-17 11:44:09 +08001220 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001221 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001222 cur_end = end;
1223 else
Byongho Leeee221842015-12-15 01:42:10 +09001224 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001225
1226 async_cow->end = cur_end;
1227 INIT_LIST_HEAD(&async_cow->extents);
1228
Liu Bo9e0af232014-08-15 23:36:53 +08001229 btrfs_init_work(&async_cow->work,
1230 btrfs_delalloc_helper,
1231 async_cow_start, async_cow_submit,
1232 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001233
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001234 nr_pages = (cur_end - start + PAGE_SIZE) >>
1235 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001236 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001237
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001238 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001239
Chris Mason771ed682008-11-06 22:02:51 -05001240 *nr_written += nr_pages;
1241 start = cur_end + 1;
1242 }
1243 *page_started = 1;
1244 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001245}
1246
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001247static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001248 u64 bytenr, u64 num_bytes)
1249{
1250 int ret;
1251 struct btrfs_ordered_sum *sums;
1252 LIST_HEAD(list);
1253
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001254 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001255 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001256 if (ret == 0 && list_empty(&list))
1257 return 0;
1258
1259 while (!list_empty(&list)) {
1260 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1261 list_del(&sums->list);
1262 kfree(sums);
1263 }
1264 return 1;
1265}
1266
Chris Masond352ac62008-09-29 15:18:18 -04001267/*
1268 * when nowcow writeback call back. This checks for snapshots or COW copies
1269 * of the extents that exist in the file, and COWs the file as required.
1270 *
1271 * If no cow copies or snapshots exist, we write directly to the existing
1272 * blocks on disk
1273 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001274static noinline int run_delalloc_nocow(struct inode *inode,
1275 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001276 u64 start, u64 end, int *page_started, int force,
1277 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001278{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001279 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001280 struct btrfs_root *root = BTRFS_I(inode)->root;
1281 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001282 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001284 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001285 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 u64 cow_start;
1287 u64 cur_offset;
1288 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001289 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 u64 disk_bytenr;
1291 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001292 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001293 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001295 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001296 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 int nocow;
1298 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001299 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001300 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001301
1302 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001303 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001304 extent_clear_unlock_delalloc(inode, start, end, end,
1305 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001306 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001307 EXTENT_DO_ACCOUNTING |
1308 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001309 PAGE_CLEAR_DIRTY |
1310 PAGE_SET_WRITEBACK |
1311 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001312 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001313 }
Li Zefan82d59022011-04-20 10:33:24 +08001314
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001315 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001316
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 cow_start = (u64)-1;
1318 cur_offset = start;
1319 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001320 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001322 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001323 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001324 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1325 leaf = path->nodes[0];
1326 btrfs_item_key_to_cpu(leaf, &found_key,
1327 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001328 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 found_key.type == BTRFS_EXTENT_DATA_KEY)
1330 path->slots[0]--;
1331 }
1332 check_prev = 0;
1333next_slot:
1334 leaf = path->nodes[0];
1335 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1336 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001337 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001338 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 if (ret > 0)
1340 break;
1341 leaf = path->nodes[0];
1342 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001343
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 nocow = 0;
1345 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001346 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001348
Filipe Manana1d512cb2015-11-09 00:33:58 +00001349 if (found_key.objectid > ino)
1350 break;
1351 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1352 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1353 path->slots[0]++;
1354 goto next_slot;
1355 }
1356 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 found_key.offset > end)
1358 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001359
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 if (found_key.offset > cur_offset) {
1361 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001362 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 goto out_check;
1364 }
Chris Masonc31f8832008-01-08 15:46:31 -05001365
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 fi = btrfs_item_ptr(leaf, path->slots[0],
1367 struct btrfs_file_extent_item);
1368 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001369
Josef Bacikcc95bef2013-04-04 14:31:27 -04001370 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001371 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1372 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001374 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001375 extent_end = found_key.offset +
1376 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001377 disk_num_bytes =
1378 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001379 if (extent_end <= start) {
1380 path->slots[0]++;
1381 goto next_slot;
1382 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001383 if (disk_bytenr == 0)
1384 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001385 if (btrfs_file_extent_compression(leaf, fi) ||
1386 btrfs_file_extent_encryption(leaf, fi) ||
1387 btrfs_file_extent_other_encoding(leaf, fi))
1388 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001389 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1390 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001391 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001392 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001393 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001394 found_key.offset -
1395 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001396 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001397 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001398 disk_bytenr += cur_offset - found_key.offset;
1399 num_bytes = min(end + 1, extent_end) - cur_offset;
1400 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001401 * if there are pending snapshots for this root,
1402 * we fall into common COW way.
1403 */
1404 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001405 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001406 if (!err)
1407 goto out_check;
1408 }
1409 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001410 * force cow if csum exists in the range.
1411 * this ensure that csum for a given extent are
1412 * either valid or do not exist.
1413 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001414 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001415 num_bytes)) {
1416 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001417 btrfs_end_write_no_snapshotting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001418 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001419 }
1420 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1421 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001422 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001423 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001424 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001425 nocow = 1;
1426 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1427 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001428 btrfs_file_extent_inline_len(leaf,
1429 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001430 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001431 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001432 } else {
1433 BUG_ON(1);
1434 }
1435out_check:
1436 if (extent_end <= start) {
1437 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001438 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001439 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001440 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001441 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001442 goto next_slot;
1443 }
1444 if (!nocow) {
1445 if (cow_start == (u64)-1)
1446 cow_start = cur_offset;
1447 cur_offset = extent_end;
1448 if (cur_offset > end)
1449 break;
1450 path->slots[0]++;
1451 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001452 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001453
David Sterbab3b4aa72011-04-21 01:20:15 +02001454 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001455 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001456 ret = cow_file_range(inode, locked_page,
1457 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001458 end, page_started, nr_written, 1,
1459 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001460 if (ret) {
1461 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001462 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001463 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001464 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001465 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001466 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001467 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001468 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001469 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001470
Yan Zhengd899e052008-10-30 14:25:28 -04001471 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001472 u64 orig_start = found_key.offset - extent_offset;
1473
1474 em = create_io_em(inode, cur_offset, num_bytes,
1475 orig_start,
1476 disk_bytenr, /* block_start */
1477 num_bytes, /* block_len */
1478 disk_num_bytes, /* orig_block_len */
1479 ram_bytes, BTRFS_COMPRESS_NONE,
1480 BTRFS_ORDERED_PREALLOC);
1481 if (IS_ERR(em)) {
1482 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001483 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001484 if (nocow)
1485 btrfs_dec_nocow_writers(fs_info,
1486 disk_bytenr);
1487 ret = PTR_ERR(em);
1488 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001489 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001490 free_extent_map(em);
1491 }
1492
1493 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001494 type = BTRFS_ORDERED_PREALLOC;
1495 } else {
1496 type = BTRFS_ORDERED_NOCOW;
1497 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001498
1499 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001500 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001501 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001502 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001503 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001504
Yan, Zhengefa56462010-05-16 10:49:59 -04001505 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001506 BTRFS_DATA_RELOC_TREE_OBJECTID)
1507 /*
1508 * Error handled later, as we must prevent
1509 * extent_clear_unlock_delalloc() in error handler
1510 * from freeing metadata of created ordered extent.
1511 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001512 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1513 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001514
Josef Bacikc2790a22013-07-29 11:20:47 -04001515 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001516 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001517 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001518 EXTENT_DELALLOC |
1519 EXTENT_CLEAR_DATA_RESV,
1520 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1521
Wang Shilonge9894fd2014-03-27 11:12:25 +08001522 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001523 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001524 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001525
1526 /*
1527 * btrfs_reloc_clone_csums() error, now we're OK to call error
1528 * handler, as metadata for created ordered extent will only
1529 * be freed by btrfs_finish_ordered_io().
1530 */
1531 if (ret)
1532 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001533 if (cur_offset > end)
1534 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001535 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001536 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001537
Josef Bacik17ca04a2012-05-31 15:58:55 -04001538 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001539 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001540 cur_offset = end;
1541 }
1542
Yan Zheng80ff3852008-10-30 14:20:02 -04001543 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001544 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1545 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001546 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001547 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001548 }
1549
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001550error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001551 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001552 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001553 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001554 EXTENT_DELALLOC | EXTENT_DEFRAG |
1555 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1556 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001557 PAGE_SET_WRITEBACK |
1558 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001559 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001560 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001561}
1562
Wang Shilong47059d92014-07-03 18:22:07 +08001563static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1564{
1565
1566 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1567 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1568 return 0;
1569
1570 /*
1571 * @defrag_bytes is a hint value, no spinlock held here,
1572 * if is not zero, it means the file is defragging.
1573 * Force cow if given extent needs to be defragged.
1574 */
1575 if (BTRFS_I(inode)->defrag_bytes &&
1576 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1577 EXTENT_DEFRAG, 0, NULL))
1578 return 1;
1579
1580 return 0;
1581}
1582
Chris Masond352ac62008-09-29 15:18:18 -04001583/*
1584 * extent_io.c call back to do delayed allocation processing
1585 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001586static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001587 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001588 unsigned long *nr_written,
1589 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001590{
Josef Bacikc6100a42017-05-05 11:57:13 -04001591 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001592 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001593 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001594 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001595
Wang Shilong47059d92014-07-03 18:22:07 +08001596 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001597 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001598 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001599 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001600 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001601 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001602 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001603 ret = cow_file_range(inode, locked_page, start, end, end,
1604 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001605 } else {
1606 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1607 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001608 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001609 page_started, nr_written,
1610 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001611 }
Qu Wenruo524272602017-03-08 10:25:52 +08001612 if (ret)
1613 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001614 return ret;
1615}
1616
Josef Bacikc6100a42017-05-05 11:57:13 -04001617static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001618 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001619{
Josef Bacikc6100a42017-05-05 11:57:13 -04001620 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 u64 size;
1622
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001623 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001624 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001625 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001626
Josef Bacikdcab6a32015-02-11 15:08:59 -05001627 size = orig->end - orig->start + 1;
1628 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001629 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001630 u64 new_size;
1631
1632 /*
Josef Bacikba117212015-03-13 15:01:24 -04001633 * See the explanation in btrfs_merge_extent_hook, the same
1634 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001635 */
1636 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001637 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001638 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001639 num_extents += count_max_extents(new_size);
1640 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001641 return;
1642 }
1643
Josef Bacik9e0baf62011-07-15 15:16:44 +00001644 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001645 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001646 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001647}
1648
1649/*
1650 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1651 * extents so we can keep track of new extents that are just merged onto old
1652 * extents, such as when we are doing sequential writes, so we can properly
1653 * account for the metadata space we'll need.
1654 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001655static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001656 struct extent_state *new,
1657 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001658{
Josef Bacikc6100a42017-05-05 11:57:13 -04001659 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001660 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001661 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001662
Josef Bacik9ed74f22009-09-11 16:12:44 -04001663 /* not delalloc, ignore it */
1664 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001665 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001666
Josef Bacik8461a3d2015-03-13 15:12:08 -04001667 if (new->start > other->start)
1668 new_size = new->end - other->start + 1;
1669 else
1670 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001671
1672 /* we're not bigger than the max, unreserve the space and go */
1673 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1674 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001675 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001676 spin_unlock(&BTRFS_I(inode)->lock);
1677 return;
1678 }
1679
1680 /*
Josef Bacikba117212015-03-13 15:01:24 -04001681 * We have to add up either side to figure out how many extents were
1682 * accounted for before we merged into one big extent. If the number of
1683 * extents we accounted for is <= the amount we need for the new range
1684 * then we can return, otherwise drop. Think of it like this
1685 *
1686 * [ 4k][MAX_SIZE]
1687 *
1688 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1689 * need 2 outstanding extents, on one side we have 1 and the other side
1690 * we have 1 so they are == and we can return. But in this case
1691 *
1692 * [MAX_SIZE+4k][MAX_SIZE+4k]
1693 *
1694 * Each range on their own accounts for 2 extents, but merged together
1695 * they are only 3 extents worth of accounting, so we need to drop in
1696 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001697 */
Josef Bacikba117212015-03-13 15:01:24 -04001698 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001699 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001700 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001701 num_extents += count_max_extents(old_size);
1702 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001703 return;
1704
Josef Bacik9e0baf62011-07-15 15:16:44 +00001705 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001706 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001707 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001708}
1709
Miao Xieeb73c1b2013-05-15 07:48:22 +00001710static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1711 struct inode *inode)
1712{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001713 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1714
Miao Xieeb73c1b2013-05-15 07:48:22 +00001715 spin_lock(&root->delalloc_lock);
1716 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1717 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1718 &root->delalloc_inodes);
1719 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1720 &BTRFS_I(inode)->runtime_flags);
1721 root->nr_delalloc_inodes++;
1722 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001723 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724 BUG_ON(!list_empty(&root->delalloc_root));
1725 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001726 &fs_info->delalloc_roots);
1727 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001728 }
1729 }
1730 spin_unlock(&root->delalloc_lock);
1731}
1732
1733static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001734 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001735{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001736 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001737
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001739 if (!list_empty(&inode->delalloc_inodes)) {
1740 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001741 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001742 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001743 root->nr_delalloc_inodes--;
1744 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001745 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001746 BUG_ON(list_empty(&root->delalloc_root));
1747 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001748 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001749 }
1750 }
1751 spin_unlock(&root->delalloc_lock);
1752}
1753
Chris Masond352ac62008-09-29 15:18:18 -04001754/*
1755 * extent_io.c set_bit_hook, used to track delayed allocation
1756 * bytes in this file, and to maintain the list of inodes that
1757 * have pending delalloc work to be done.
1758 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001759static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001760 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001761{
Josef Bacikc6100a42017-05-05 11:57:13 -04001762 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001763
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001764 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1765
Wang Shilong47059d92014-07-03 18:22:07 +08001766 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1767 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001768 /*
1769 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001770 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001771 * bit, which is only set or cleared with irqs on
1772 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001773 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001774 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001775 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001776 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001777 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001778
Josef Bacik8b62f872017-10-19 14:15:55 -04001779 spin_lock(&BTRFS_I(inode)->lock);
1780 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1781 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001782
Josef Bacik6a3891c2015-03-16 17:38:52 -04001783 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001784 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001785 return;
1786
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001787 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1788 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001789 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001790 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001791 if (*bits & EXTENT_DEFRAG)
1792 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001793 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001794 &BTRFS_I(inode)->runtime_flags))
1795 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001796 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001797 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001798
1799 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1800 (*bits & EXTENT_DELALLOC_NEW)) {
1801 spin_lock(&BTRFS_I(inode)->lock);
1802 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1803 state->start;
1804 spin_unlock(&BTRFS_I(inode)->lock);
1805 }
Chris Mason291d6732008-01-29 15:55:23 -05001806}
1807
Chris Masond352ac62008-09-29 15:18:18 -04001808/*
1809 * extent_io.c clear_bit_hook, see set_bit_hook for why
1810 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001811static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001812 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001813 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001814{
Josef Bacikc6100a42017-05-05 11:57:13 -04001815 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001816 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001817 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001818 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001819
Filipe Manana4a4b9642017-07-27 19:52:55 +01001820 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1821 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001822 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001823 spin_unlock(&inode->lock);
1824 }
Wang Shilong47059d92014-07-03 18:22:07 +08001825
Chris Mason75eff682008-12-15 15:54:40 -05001826 /*
1827 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001828 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001829 * bit, which is only set or cleared with irqs on
1830 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001831 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001832 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001833 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001834
Josef Bacik8b62f872017-10-19 14:15:55 -04001835 spin_lock(&inode->lock);
1836 btrfs_mod_outstanding_extents(inode, -num_extents);
1837 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001838
Josef Bacikb6d08f02013-09-27 14:57:43 -04001839 /*
1840 * We don't reserve metadata space for space cache inodes so we
1841 * don't need to call dellalloc_release_metadata if there is an
1842 * error.
1843 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001844 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001845 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001846 btrfs_delalloc_release_metadata(inode, len);
1847
Josef Bacik6a3891c2015-03-16 17:38:52 -04001848 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001849 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001850 return;
1851
Filipe Mananaa315e682017-03-06 23:04:20 +00001852 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1853 do_list && !(state->state & EXTENT_NORESERVE) &&
1854 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001855 btrfs_free_reserved_data_space_noquota(
1856 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001857 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001858
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001859 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1860 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001861 spin_lock(&inode->lock);
1862 inode->delalloc_bytes -= len;
1863 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001864 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001865 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001866 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001867 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001868 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001869
1870 if ((state->state & EXTENT_DELALLOC_NEW) &&
1871 (*bits & EXTENT_DELALLOC_NEW)) {
1872 spin_lock(&inode->lock);
1873 ASSERT(inode->new_delalloc_bytes >= len);
1874 inode->new_delalloc_bytes -= len;
1875 spin_unlock(&inode->lock);
1876 }
Chris Mason291d6732008-01-29 15:55:23 -05001877}
1878
Chris Masond352ac62008-09-29 15:18:18 -04001879/*
1880 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1881 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001882 *
1883 * return 1 if page cannot be merged to bio
1884 * return 0 if page can be merged to bio
1885 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001886 */
Mike Christie81a75f672016-06-05 14:31:54 -05001887int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001888 size_t size, struct bio *bio,
1889 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001890{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001891 struct inode *inode = page->mapping->host;
1892 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001893 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001894 u64 length = 0;
1895 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001896 int ret;
1897
Chris Mason771ed682008-11-06 22:02:51 -05001898 if (bio_flags & EXTENT_BIO_COMPRESSED)
1899 return 0;
1900
Kent Overstreet4f024f32013-10-11 15:44:27 -07001901 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001902 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001903 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1904 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001905 if (ret < 0)
1906 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001907 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001908 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001909 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001910}
1911
Chris Masond352ac62008-09-29 15:18:18 -04001912/*
1913 * in order to insert checksums into the metadata in large chunks,
1914 * we wait until bio submission time. All the pages in the bio are
1915 * checksummed and sums are attached onto the ordered extent record.
1916 *
1917 * At IO completion time the cums attached on the ordered extent record
1918 * are inserted into the btree
1919 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001920static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001921 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001922 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001923{
Josef Bacikc6100a42017-05-05 11:57:13 -04001924 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001925 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001926
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001927 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001928 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001929 return 0;
1930}
Chris Masone0156402008-04-16 11:15:20 -04001931
Chris Mason4a69a412008-11-06 22:03:00 -05001932/*
1933 * in order to insert checksums into the metadata in large chunks,
1934 * we wait until bio submission time. All the pages in the bio are
1935 * checksummed and sums are attached onto the ordered extent record.
1936 *
1937 * At IO completion time the cums attached on the ordered extent record
1938 * are inserted into the btree
1939 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001940static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001941 int mirror_num, unsigned long bio_flags,
1942 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001943{
Josef Bacikc6100a42017-05-05 11:57:13 -04001944 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001945 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001946 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001947
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001948 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001949 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001950 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001951 bio_endio(bio);
1952 }
Stefan Behrens61891922012-11-05 18:51:52 +01001953 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001954}
1955
Chris Masond352ac62008-09-29 15:18:18 -04001956/*
Chris Masoncad321a2008-12-17 14:51:42 -05001957 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001958 * on write, or reading the csums from the tree before a read.
1959 *
1960 * Rules about async/sync submit,
1961 * a) read: sync submit
1962 *
1963 * b) write without checksum: sync submit
1964 *
1965 * c) write with checksum:
1966 * c-1) if bio is issued by fsync: sync submit
1967 * (sync_writers != 0)
1968 *
1969 * c-2) if root is reloc root: sync submit
1970 * (only in case of buffered IO)
1971 *
1972 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001973 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001974static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001975 int mirror_num, unsigned long bio_flags,
1976 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001977{
Josef Bacikc6100a42017-05-05 11:57:13 -04001978 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001979 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001980 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301981 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001982 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001983 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001984 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001985
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001986 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001987
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001988 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301989 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001990
Mike Christie37226b22016-06-05 14:31:52 -05001991 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001992 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001993 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001994 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001995
Chris Masond20f7042008-12-08 16:58:54 -05001996 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001997 ret = btrfs_submit_compressed_read(inode, bio,
1998 mirror_num,
1999 bio_flags);
2000 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002001 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002002 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002003 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002004 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002005 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002006 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002007 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002008 /* csum items have already been cloned */
2009 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2010 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002011 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002012 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2013 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002014 __btrfs_submit_bio_start,
2015 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002016 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002017 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002018 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002019 if (ret)
2020 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002021 }
2022
Chris Mason0b86a832008-03-24 15:01:56 -04002023mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002024 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002025
2026out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002027 if (ret) {
2028 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002029 bio_endio(bio);
2030 }
Stefan Behrens61891922012-11-05 18:51:52 +01002031 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002032}
Chris Mason6885f302008-02-20 16:11:05 -05002033
Chris Masond352ac62008-09-29 15:18:18 -04002034/*
2035 * given a list of ordered sums record them in the inode. This happens
2036 * at IO completion time based on sums calculated at bio submission time.
2037 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002038static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002039 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002040{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002041 struct btrfs_ordered_sum *sum;
2042
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002043 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002044 trans->adding_csums = true;
Chris Masond20f7042008-12-08 16:58:54 -05002045 btrfs_csum_file_blocks(trans,
2046 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002047 trans->adding_csums = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002048 }
2049 return 0;
2050}
2051
Josef Bacik2ac55d42010-02-03 19:33:23 +00002052int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002053 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002054 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002055{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002056 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002057 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002058 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002059}
2060
Chris Masond352ac62008-09-29 15:18:18 -04002061/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002062struct btrfs_writepage_fixup {
2063 struct page *page;
2064 struct btrfs_work work;
2065};
2066
Christoph Hellwigb2950862008-12-02 09:54:17 -05002067static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002068{
2069 struct btrfs_writepage_fixup *fixup;
2070 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002071 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002072 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002073 struct page *page;
2074 struct inode *inode;
2075 u64 page_start;
2076 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002077 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002078
2079 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2080 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002081again:
Chris Mason247e7432008-07-17 12:53:51 -04002082 lock_page(page);
2083 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2084 ClearPageChecked(page);
2085 goto out_page;
2086 }
2087
2088 inode = page->mapping->host;
2089 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002090 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002091
David Sterbaff13db42015-12-03 14:30:40 +01002092 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002093 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002094
2095 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002096 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002097 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002098
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002099 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002100 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002101 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002102 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2103 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002104 unlock_page(page);
2105 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002106 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002107 goto again;
2108 }
Chris Mason247e7432008-07-17 12:53:51 -04002109
Qu Wenruo364ecf32017-02-27 15:10:38 +08002110 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002111 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002112 if (ret) {
2113 mapping_set_error(page->mapping, ret);
2114 end_extent_writepage(page, ret, page_start, page_end);
2115 ClearPageChecked(page);
2116 goto out;
2117 }
2118
Filipe Mananae3b8a482017-11-04 00:16:59 +00002119 btrfs_set_extent_delalloc(inode, page_start, page_end, 0, &cached_state,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002120 0);
Chris Mason247e7432008-07-17 12:53:51 -04002121 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002122 set_page_dirty(page);
Josef Bacik8b62f872017-10-19 14:15:55 -04002123 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002124out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002125 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2126 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002127out_page:
2128 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002129 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002130 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002131 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002132}
2133
2134/*
2135 * There are a few paths in the higher layers of the kernel that directly
2136 * set the page dirty bit without asking the filesystem if it is a
2137 * good idea. This causes problems because we want to make sure COW
2138 * properly happens and the data=ordered rules are followed.
2139 *
Chris Masonc8b97812008-10-29 14:49:59 -04002140 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002141 * hasn't been properly setup for IO. We kick off an async process
2142 * to fix it up. The async helper will wait for ordered extents, set
2143 * the delalloc bit and make it safe to write the page.
2144 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002145static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002146{
2147 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002148 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002149 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002150
Chris Mason8b62b722009-09-02 16:53:46 -04002151 /* this page is properly in the ordered list */
2152 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002153 return 0;
2154
2155 if (PageChecked(page))
2156 return -EAGAIN;
2157
2158 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2159 if (!fixup)
2160 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002161
Chris Mason247e7432008-07-17 12:53:51 -04002162 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002163 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002164 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2165 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002166 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002167 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002168 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002169}
2170
Yan Zhengd899e052008-10-30 14:25:28 -04002171static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2172 struct inode *inode, u64 file_pos,
2173 u64 disk_bytenr, u64 disk_num_bytes,
2174 u64 num_bytes, u64 ram_bytes,
2175 u8 compression, u8 encryption,
2176 u16 other_encoding, int extent_type)
2177{
2178 struct btrfs_root *root = BTRFS_I(inode)->root;
2179 struct btrfs_file_extent_item *fi;
2180 struct btrfs_path *path;
2181 struct extent_buffer *leaf;
2182 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002183 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002184 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002185 int ret;
2186
2187 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002188 if (!path)
2189 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002190
Chris Masona1ed8352009-09-11 12:27:37 -04002191 /*
2192 * we may be replacing one extent in the tree with another.
2193 * The new extent is pinned in the extent map, and we don't want
2194 * to drop it from the cache until it is completely in the btree.
2195 *
2196 * So, tell btrfs_drop_extents to leave this extent in the cache.
2197 * the caller is expected to unpin it and allow it to be merged
2198 * with the others.
2199 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002200 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2201 file_pos + num_bytes, NULL, 0,
2202 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002203 if (ret)
2204 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002205
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002206 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002207 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002208 ins.offset = file_pos;
2209 ins.type = BTRFS_EXTENT_DATA_KEY;
2210
2211 path->leave_spinning = 1;
2212 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2213 sizeof(*fi));
2214 if (ret)
2215 goto out;
2216 }
Yan Zhengd899e052008-10-30 14:25:28 -04002217 leaf = path->nodes[0];
2218 fi = btrfs_item_ptr(leaf, path->slots[0],
2219 struct btrfs_file_extent_item);
2220 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2221 btrfs_set_file_extent_type(leaf, fi, extent_type);
2222 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2223 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2224 btrfs_set_file_extent_offset(leaf, fi, 0);
2225 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2226 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2227 btrfs_set_file_extent_compression(leaf, fi, compression);
2228 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2229 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002230
Yan Zhengd899e052008-10-30 14:25:28 -04002231 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002232 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002233
2234 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002235
2236 ins.objectid = disk_bytenr;
2237 ins.offset = disk_num_bytes;
2238 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002239
Qu Wenruo297d7502015-09-08 17:08:37 +08002240 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002241 * Release the reserved range from inode dirty range map, as it is
2242 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002243 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002244 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2245 if (ret < 0)
2246 goto out;
2247 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002248 ret = btrfs_alloc_reserved_file_extent(trans, root,
2249 btrfs_ino(BTRFS_I(inode)),
2250 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002251out:
Yan Zhengd899e052008-10-30 14:25:28 -04002252 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002253
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002254 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002255}
2256
Liu Bo38c227d2013-01-29 03:18:40 +00002257/* snapshot-aware defrag */
2258struct sa_defrag_extent_backref {
2259 struct rb_node node;
2260 struct old_sa_defrag_extent *old;
2261 u64 root_id;
2262 u64 inum;
2263 u64 file_pos;
2264 u64 extent_offset;
2265 u64 num_bytes;
2266 u64 generation;
2267};
2268
2269struct old_sa_defrag_extent {
2270 struct list_head list;
2271 struct new_sa_defrag_extent *new;
2272
2273 u64 extent_offset;
2274 u64 bytenr;
2275 u64 offset;
2276 u64 len;
2277 int count;
2278};
2279
2280struct new_sa_defrag_extent {
2281 struct rb_root root;
2282 struct list_head head;
2283 struct btrfs_path *path;
2284 struct inode *inode;
2285 u64 file_pos;
2286 u64 len;
2287 u64 bytenr;
2288 u64 disk_len;
2289 u8 compress_type;
2290};
2291
2292static int backref_comp(struct sa_defrag_extent_backref *b1,
2293 struct sa_defrag_extent_backref *b2)
2294{
2295 if (b1->root_id < b2->root_id)
2296 return -1;
2297 else if (b1->root_id > b2->root_id)
2298 return 1;
2299
2300 if (b1->inum < b2->inum)
2301 return -1;
2302 else if (b1->inum > b2->inum)
2303 return 1;
2304
2305 if (b1->file_pos < b2->file_pos)
2306 return -1;
2307 else if (b1->file_pos > b2->file_pos)
2308 return 1;
2309
2310 /*
2311 * [------------------------------] ===> (a range of space)
2312 * |<--->| |<---->| =============> (fs/file tree A)
2313 * |<---------------------------->| ===> (fs/file tree B)
2314 *
2315 * A range of space can refer to two file extents in one tree while
2316 * refer to only one file extent in another tree.
2317 *
2318 * So we may process a disk offset more than one time(two extents in A)
2319 * and locate at the same extent(one extent in B), then insert two same
2320 * backrefs(both refer to the extent in B).
2321 */
2322 return 0;
2323}
2324
2325static void backref_insert(struct rb_root *root,
2326 struct sa_defrag_extent_backref *backref)
2327{
2328 struct rb_node **p = &root->rb_node;
2329 struct rb_node *parent = NULL;
2330 struct sa_defrag_extent_backref *entry;
2331 int ret;
2332
2333 while (*p) {
2334 parent = *p;
2335 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2336
2337 ret = backref_comp(backref, entry);
2338 if (ret < 0)
2339 p = &(*p)->rb_left;
2340 else
2341 p = &(*p)->rb_right;
2342 }
2343
2344 rb_link_node(&backref->node, parent, p);
2345 rb_insert_color(&backref->node, root);
2346}
2347
2348/*
2349 * Note the backref might has changed, and in this case we just return 0.
2350 */
2351static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2352 void *ctx)
2353{
2354 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002355 struct old_sa_defrag_extent *old = ctx;
2356 struct new_sa_defrag_extent *new = old->new;
2357 struct btrfs_path *path = new->path;
2358 struct btrfs_key key;
2359 struct btrfs_root *root;
2360 struct sa_defrag_extent_backref *backref;
2361 struct extent_buffer *leaf;
2362 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002363 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002364 int slot;
2365 int ret;
2366 u64 extent_offset;
2367 u64 num_bytes;
2368
2369 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002370 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002371 return 0;
2372
2373 key.objectid = root_id;
2374 key.type = BTRFS_ROOT_ITEM_KEY;
2375 key.offset = (u64)-1;
2376
Liu Bo38c227d2013-01-29 03:18:40 +00002377 root = btrfs_read_fs_root_no_name(fs_info, &key);
2378 if (IS_ERR(root)) {
2379 if (PTR_ERR(root) == -ENOENT)
2380 return 0;
2381 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002382 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002383 inum, offset, root_id);
2384 return PTR_ERR(root);
2385 }
2386
2387 key.objectid = inum;
2388 key.type = BTRFS_EXTENT_DATA_KEY;
2389 if (offset > (u64)-1 << 32)
2390 key.offset = 0;
2391 else
2392 key.offset = offset;
2393
2394 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302395 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002396 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002397 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002398
2399 while (1) {
2400 cond_resched();
2401
2402 leaf = path->nodes[0];
2403 slot = path->slots[0];
2404
2405 if (slot >= btrfs_header_nritems(leaf)) {
2406 ret = btrfs_next_leaf(root, path);
2407 if (ret < 0) {
2408 goto out;
2409 } else if (ret > 0) {
2410 ret = 0;
2411 goto out;
2412 }
2413 continue;
2414 }
2415
2416 path->slots[0]++;
2417
2418 btrfs_item_key_to_cpu(leaf, &key, slot);
2419
2420 if (key.objectid > inum)
2421 goto out;
2422
2423 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2424 continue;
2425
2426 extent = btrfs_item_ptr(leaf, slot,
2427 struct btrfs_file_extent_item);
2428
2429 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2430 continue;
2431
Liu Boe68afa42013-07-01 22:13:26 +08002432 /*
2433 * 'offset' refers to the exact key.offset,
2434 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2435 * (key.offset - extent_offset).
2436 */
2437 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002438 continue;
2439
Liu Boe68afa42013-07-01 22:13:26 +08002440 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002441 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002442
Liu Bo38c227d2013-01-29 03:18:40 +00002443 if (extent_offset >= old->extent_offset + old->offset +
2444 old->len || extent_offset + num_bytes <=
2445 old->extent_offset + old->offset)
2446 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002447 break;
2448 }
2449
2450 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2451 if (!backref) {
2452 ret = -ENOENT;
2453 goto out;
2454 }
2455
2456 backref->root_id = root_id;
2457 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002458 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002459 backref->num_bytes = num_bytes;
2460 backref->extent_offset = extent_offset;
2461 backref->generation = btrfs_file_extent_generation(leaf, extent);
2462 backref->old = old;
2463 backref_insert(&new->root, backref);
2464 old->count++;
2465out:
2466 btrfs_release_path(path);
2467 WARN_ON(ret);
2468 return ret;
2469}
2470
2471static noinline bool record_extent_backrefs(struct btrfs_path *path,
2472 struct new_sa_defrag_extent *new)
2473{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002474 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002475 struct old_sa_defrag_extent *old, *tmp;
2476 int ret;
2477
2478 new->path = path;
2479
2480 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002481 ret = iterate_inodes_from_logical(old->bytenr +
2482 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002483 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002484 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002485 if (ret < 0 && ret != -ENOENT)
2486 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002487
2488 /* no backref to be processed for this extent */
2489 if (!old->count) {
2490 list_del(&old->list);
2491 kfree(old);
2492 }
2493 }
2494
2495 if (list_empty(&new->head))
2496 return false;
2497
2498 return true;
2499}
2500
2501static int relink_is_mergable(struct extent_buffer *leaf,
2502 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002503 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002504{
Liu Bo116e0022013-08-02 16:30:40 +08002505 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002506 return 0;
2507
2508 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2509 return 0;
2510
Liu Bo116e0022013-08-02 16:30:40 +08002511 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2512 return 0;
2513
2514 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002515 btrfs_file_extent_other_encoding(leaf, fi))
2516 return 0;
2517
2518 return 1;
2519}
2520
2521/*
2522 * Note the backref might has changed, and in this case we just return 0.
2523 */
2524static noinline int relink_extent_backref(struct btrfs_path *path,
2525 struct sa_defrag_extent_backref *prev,
2526 struct sa_defrag_extent_backref *backref)
2527{
2528 struct btrfs_file_extent_item *extent;
2529 struct btrfs_file_extent_item *item;
2530 struct btrfs_ordered_extent *ordered;
2531 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002532 struct btrfs_root *root;
2533 struct btrfs_key key;
2534 struct extent_buffer *leaf;
2535 struct old_sa_defrag_extent *old = backref->old;
2536 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002537 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002538 struct inode *inode;
2539 struct extent_state *cached = NULL;
2540 int ret = 0;
2541 u64 start;
2542 u64 len;
2543 u64 lock_start;
2544 u64 lock_end;
2545 bool merge = false;
2546 int index;
2547
2548 if (prev && prev->root_id == backref->root_id &&
2549 prev->inum == backref->inum &&
2550 prev->file_pos + prev->num_bytes == backref->file_pos)
2551 merge = true;
2552
2553 /* step 1: get root */
2554 key.objectid = backref->root_id;
2555 key.type = BTRFS_ROOT_ITEM_KEY;
2556 key.offset = (u64)-1;
2557
Liu Bo38c227d2013-01-29 03:18:40 +00002558 index = srcu_read_lock(&fs_info->subvol_srcu);
2559
2560 root = btrfs_read_fs_root_no_name(fs_info, &key);
2561 if (IS_ERR(root)) {
2562 srcu_read_unlock(&fs_info->subvol_srcu, index);
2563 if (PTR_ERR(root) == -ENOENT)
2564 return 0;
2565 return PTR_ERR(root);
2566 }
Liu Bo38c227d2013-01-29 03:18:40 +00002567
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002568 if (btrfs_root_readonly(root)) {
2569 srcu_read_unlock(&fs_info->subvol_srcu, index);
2570 return 0;
2571 }
2572
Liu Bo38c227d2013-01-29 03:18:40 +00002573 /* step 2: get inode */
2574 key.objectid = backref->inum;
2575 key.type = BTRFS_INODE_ITEM_KEY;
2576 key.offset = 0;
2577
2578 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2579 if (IS_ERR(inode)) {
2580 srcu_read_unlock(&fs_info->subvol_srcu, index);
2581 return 0;
2582 }
2583
2584 srcu_read_unlock(&fs_info->subvol_srcu, index);
2585
2586 /* step 3: relink backref */
2587 lock_start = backref->file_pos;
2588 lock_end = backref->file_pos + backref->num_bytes - 1;
2589 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002590 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002591
2592 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2593 if (ordered) {
2594 btrfs_put_ordered_extent(ordered);
2595 goto out_unlock;
2596 }
2597
2598 trans = btrfs_join_transaction(root);
2599 if (IS_ERR(trans)) {
2600 ret = PTR_ERR(trans);
2601 goto out_unlock;
2602 }
2603
2604 key.objectid = backref->inum;
2605 key.type = BTRFS_EXTENT_DATA_KEY;
2606 key.offset = backref->file_pos;
2607
2608 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2609 if (ret < 0) {
2610 goto out_free_path;
2611 } else if (ret > 0) {
2612 ret = 0;
2613 goto out_free_path;
2614 }
2615
2616 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2617 struct btrfs_file_extent_item);
2618
2619 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2620 backref->generation)
2621 goto out_free_path;
2622
2623 btrfs_release_path(path);
2624
2625 start = backref->file_pos;
2626 if (backref->extent_offset < old->extent_offset + old->offset)
2627 start += old->extent_offset + old->offset -
2628 backref->extent_offset;
2629
2630 len = min(backref->extent_offset + backref->num_bytes,
2631 old->extent_offset + old->offset + old->len);
2632 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2633
2634 ret = btrfs_drop_extents(trans, root, inode, start,
2635 start + len, 1);
2636 if (ret)
2637 goto out_free_path;
2638again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002639 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002640 key.type = BTRFS_EXTENT_DATA_KEY;
2641 key.offset = start;
2642
Liu Boa09a0a72013-03-11 09:20:58 +00002643 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002644 if (merge) {
2645 struct btrfs_file_extent_item *fi;
2646 u64 extent_len;
2647 struct btrfs_key found_key;
2648
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002649 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002650 if (ret < 0)
2651 goto out_free_path;
2652
2653 path->slots[0]--;
2654 leaf = path->nodes[0];
2655 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2656
2657 fi = btrfs_item_ptr(leaf, path->slots[0],
2658 struct btrfs_file_extent_item);
2659 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2660
Liu Bo116e0022013-08-02 16:30:40 +08002661 if (extent_len + found_key.offset == start &&
2662 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002663 btrfs_set_file_extent_num_bytes(leaf, fi,
2664 extent_len + len);
2665 btrfs_mark_buffer_dirty(leaf);
2666 inode_add_bytes(inode, len);
2667
2668 ret = 1;
2669 goto out_free_path;
2670 } else {
2671 merge = false;
2672 btrfs_release_path(path);
2673 goto again;
2674 }
2675 }
2676
2677 ret = btrfs_insert_empty_item(trans, root, path, &key,
2678 sizeof(*extent));
2679 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002680 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002681 goto out_free_path;
2682 }
2683
2684 leaf = path->nodes[0];
2685 item = btrfs_item_ptr(leaf, path->slots[0],
2686 struct btrfs_file_extent_item);
2687 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2688 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2689 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2690 btrfs_set_file_extent_num_bytes(leaf, item, len);
2691 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2692 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2693 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2694 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2695 btrfs_set_file_extent_encryption(leaf, item, 0);
2696 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2697
2698 btrfs_mark_buffer_dirty(leaf);
2699 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002700 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002701
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002702 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002703 new->disk_len, 0,
2704 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002705 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002706 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002707 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002708 goto out_free_path;
2709 }
2710
2711 ret = 1;
2712out_free_path:
2713 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002714 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002715 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002716out_unlock:
2717 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2718 &cached, GFP_NOFS);
2719 iput(inode);
2720 return ret;
2721}
2722
Liu Bo6f519562013-10-29 10:45:05 +08002723static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2724{
2725 struct old_sa_defrag_extent *old, *tmp;
2726
2727 if (!new)
2728 return;
2729
2730 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002731 kfree(old);
2732 }
2733 kfree(new);
2734}
2735
Liu Bo38c227d2013-01-29 03:18:40 +00002736static void relink_file_extents(struct new_sa_defrag_extent *new)
2737{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002738 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002739 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002740 struct sa_defrag_extent_backref *backref;
2741 struct sa_defrag_extent_backref *prev = NULL;
2742 struct inode *inode;
2743 struct btrfs_root *root;
2744 struct rb_node *node;
2745 int ret;
2746
2747 inode = new->inode;
2748 root = BTRFS_I(inode)->root;
2749
2750 path = btrfs_alloc_path();
2751 if (!path)
2752 return;
2753
2754 if (!record_extent_backrefs(path, new)) {
2755 btrfs_free_path(path);
2756 goto out;
2757 }
2758 btrfs_release_path(path);
2759
2760 while (1) {
2761 node = rb_first(&new->root);
2762 if (!node)
2763 break;
2764 rb_erase(node, &new->root);
2765
2766 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2767
2768 ret = relink_extent_backref(path, prev, backref);
2769 WARN_ON(ret < 0);
2770
2771 kfree(prev);
2772
2773 if (ret == 1)
2774 prev = backref;
2775 else
2776 prev = NULL;
2777 cond_resched();
2778 }
2779 kfree(prev);
2780
2781 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002782out:
Liu Bo6f519562013-10-29 10:45:05 +08002783 free_sa_defrag_extent(new);
2784
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002785 atomic_dec(&fs_info->defrag_running);
2786 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002787}
2788
2789static struct new_sa_defrag_extent *
2790record_old_file_extents(struct inode *inode,
2791 struct btrfs_ordered_extent *ordered)
2792{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002793 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002794 struct btrfs_root *root = BTRFS_I(inode)->root;
2795 struct btrfs_path *path;
2796 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002797 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002798 struct new_sa_defrag_extent *new;
2799 int ret;
2800
2801 new = kmalloc(sizeof(*new), GFP_NOFS);
2802 if (!new)
2803 return NULL;
2804
2805 new->inode = inode;
2806 new->file_pos = ordered->file_offset;
2807 new->len = ordered->len;
2808 new->bytenr = ordered->start;
2809 new->disk_len = ordered->disk_len;
2810 new->compress_type = ordered->compress_type;
2811 new->root = RB_ROOT;
2812 INIT_LIST_HEAD(&new->head);
2813
2814 path = btrfs_alloc_path();
2815 if (!path)
2816 goto out_kfree;
2817
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002818 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002819 key.type = BTRFS_EXTENT_DATA_KEY;
2820 key.offset = new->file_pos;
2821
2822 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2823 if (ret < 0)
2824 goto out_free_path;
2825 if (ret > 0 && path->slots[0] > 0)
2826 path->slots[0]--;
2827
2828 /* find out all the old extents for the file range */
2829 while (1) {
2830 struct btrfs_file_extent_item *extent;
2831 struct extent_buffer *l;
2832 int slot;
2833 u64 num_bytes;
2834 u64 offset;
2835 u64 end;
2836 u64 disk_bytenr;
2837 u64 extent_offset;
2838
2839 l = path->nodes[0];
2840 slot = path->slots[0];
2841
2842 if (slot >= btrfs_header_nritems(l)) {
2843 ret = btrfs_next_leaf(root, path);
2844 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002845 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002846 else if (ret > 0)
2847 break;
2848 continue;
2849 }
2850
2851 btrfs_item_key_to_cpu(l, &key, slot);
2852
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002853 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002854 break;
2855 if (key.type != BTRFS_EXTENT_DATA_KEY)
2856 break;
2857 if (key.offset >= new->file_pos + new->len)
2858 break;
2859
2860 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2861
2862 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2863 if (key.offset + num_bytes < new->file_pos)
2864 goto next;
2865
2866 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2867 if (!disk_bytenr)
2868 goto next;
2869
2870 extent_offset = btrfs_file_extent_offset(l, extent);
2871
2872 old = kmalloc(sizeof(*old), GFP_NOFS);
2873 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002874 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002875
2876 offset = max(new->file_pos, key.offset);
2877 end = min(new->file_pos + new->len, key.offset + num_bytes);
2878
2879 old->bytenr = disk_bytenr;
2880 old->extent_offset = extent_offset;
2881 old->offset = offset - key.offset;
2882 old->len = end - offset;
2883 old->new = new;
2884 old->count = 0;
2885 list_add_tail(&old->list, &new->head);
2886next:
2887 path->slots[0]++;
2888 cond_resched();
2889 }
2890
2891 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002892 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002893
2894 return new;
2895
Liu Bo38c227d2013-01-29 03:18:40 +00002896out_free_path:
2897 btrfs_free_path(path);
2898out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002899 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002900 return NULL;
2901}
2902
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002903static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002904 u64 start, u64 len)
2905{
2906 struct btrfs_block_group_cache *cache;
2907
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002908 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002909 ASSERT(cache);
2910
2911 spin_lock(&cache->lock);
2912 cache->delalloc_bytes -= len;
2913 spin_unlock(&cache->lock);
2914
2915 btrfs_put_block_group(cache);
2916}
2917
Chris Masond352ac62008-09-29 15:18:18 -04002918/* as ordered data IO finishes, this gets called so we can finish
2919 * an ordered extent if the range of bytes in the file it covers are
2920 * fully written.
2921 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002922static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002923{
Josef Bacik5fd02042012-05-02 14:00:54 -04002924 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002925 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002926 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002927 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002928 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002929 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002930 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002931 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002932 int ret = 0;
2933 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002934 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002935 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002936 bool range_locked = false;
2937 bool clear_new_delalloc_bytes = false;
2938
2939 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2940 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2941 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2942 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002943
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002944 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002945
Josef Bacik5fd02042012-05-02 14:00:54 -04002946 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2947 ret = -EIO;
2948 goto out;
2949 }
2950
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002951 btrfs_free_io_failure_record(BTRFS_I(inode),
2952 ordered_extent->file_offset,
2953 ordered_extent->file_offset +
2954 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002955
Josef Bacik77cef2e2013-08-29 13:57:21 -04002956 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2957 truncated = true;
2958 logical_len = ordered_extent->truncated_len;
2959 /* Truncated the entire extent, don't bother adding */
2960 if (!logical_len)
2961 goto out;
2962 }
2963
Yan, Zhengc2167752009-11-12 09:34:21 +00002964 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002965 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002966
2967 /*
2968 * For mwrite(mmap + memset to write) case, we still reserve
2969 * space for NOCOW range.
2970 * As NOCOW won't cause a new delayed ref, just free the space
2971 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002972 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002973 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002974 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2975 if (nolock)
2976 trans = btrfs_join_transaction_nolock(root);
2977 else
2978 trans = btrfs_join_transaction(root);
2979 if (IS_ERR(trans)) {
2980 ret = PTR_ERR(trans);
2981 trans = NULL;
2982 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002983 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002984 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002985 ret = btrfs_update_inode_fallback(trans, root, inode);
2986 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002987 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002988 goto out;
2989 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002990
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002991 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002992 lock_extent_bits(io_tree, ordered_extent->file_offset,
2993 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002994 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002995
Liu Bo38c227d2013-01-29 03:18:40 +00002996 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2997 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002998 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002999 if (ret) {
3000 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003001 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003002 /* the inode is shared */
3003 new = record_old_file_extents(inode, ordered_extent);
3004
3005 clear_extent_bit(io_tree, ordered_extent->file_offset,
3006 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003007 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003008 }
3009
Josef Bacik0cb59c92010-07-02 12:14:14 -04003010 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003011 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003012 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003013 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003014 if (IS_ERR(trans)) {
3015 ret = PTR_ERR(trans);
3016 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003017 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003018 }
Chris Masona79b7d42014-05-22 16:18:52 -07003019
Josef Bacik69fe2d72017-10-19 14:15:57 -04003020 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003021
Chris Masonc8b97812008-10-29 14:49:59 -04003022 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003023 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003024 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003025 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003026 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3027 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003028 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003029 ordered_extent->file_offset,
3030 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003031 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003032 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003033 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003034 ret = insert_reserved_file_extent(trans, inode,
3035 ordered_extent->file_offset,
3036 ordered_extent->start,
3037 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003038 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003039 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003040 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003041 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003042 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003043 ordered_extent->start,
3044 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003045 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003046 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3047 ordered_extent->file_offset, ordered_extent->len,
3048 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003049 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003050 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003051 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003052 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003053
David Sterbadf9f6282017-02-10 19:35:37 +01003054 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003055
Josef Bacik6c760c02012-11-09 10:53:21 -05003056 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3057 ret = btrfs_update_inode_fallback(trans, root, inode);
3058 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003059 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003060 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003061 }
3062 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003063out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003064 if (range_locked || clear_new_delalloc_bytes) {
3065 unsigned int clear_bits = 0;
3066
3067 if (range_locked)
3068 clear_bits |= EXTENT_LOCKED;
3069 if (clear_new_delalloc_bytes)
3070 clear_bits |= EXTENT_DELALLOC_NEW;
3071 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3072 ordered_extent->file_offset,
3073 ordered_extent->file_offset +
3074 ordered_extent->len - 1,
3075 clear_bits,
3076 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003077 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003078 }
3079
Miao Xiea698d0752012-09-20 01:51:59 -06003080 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003081 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003082
Josef Bacik77cef2e2013-08-29 13:57:21 -04003083 if (ret || truncated) {
3084 u64 start, end;
3085
3086 if (truncated)
3087 start = ordered_extent->file_offset + logical_len;
3088 else
3089 start = ordered_extent->file_offset;
3090 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003091 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003092
3093 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003094 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003095
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003096 /*
3097 * If the ordered extent had an IOERR or something else went
3098 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003099 * back to the allocator. We only free the extent in the
3100 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003101 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003102 if ((ret || !logical_len) &&
3103 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003104 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003105 btrfs_free_reserved_extent(fs_info,
3106 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003107 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003108 }
3109
3110
Josef Bacik5fd02042012-05-02 14:00:54 -04003111 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003112 * This needs to be done to make sure anybody waiting knows we are done
3113 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003114 */
3115 btrfs_remove_ordered_extent(inode, ordered_extent);
3116
Liu Bo38c227d2013-01-29 03:18:40 +00003117 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003118 if (new) {
3119 if (ret) {
3120 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003121 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003122 } else {
3123 relink_file_extents(new);
3124 }
3125 }
Liu Bo38c227d2013-01-29 03:18:40 +00003126
Chris Masone6dcd2d2008-07-17 12:53:50 -04003127 /* once for us */
3128 btrfs_put_ordered_extent(ordered_extent);
3129 /* once for the tree */
3130 btrfs_put_ordered_extent(ordered_extent);
3131
Josef Bacik5fd02042012-05-02 14:00:54 -04003132 return ret;
3133}
3134
3135static void finish_ordered_fn(struct btrfs_work *work)
3136{
3137 struct btrfs_ordered_extent *ordered_extent;
3138 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3139 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003140}
3141
David Sterbac3988d62017-02-17 15:18:32 +01003142static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003143 struct extent_state *state, int uptodate)
3144{
Josef Bacik5fd02042012-05-02 14:00:54 -04003145 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003146 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003147 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003148 struct btrfs_workqueue *wq;
3149 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003150
liubo1abe9b82011-03-24 11:18:59 +00003151 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3152
Chris Mason8b62b722009-09-02 16:53:46 -04003153 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003154 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3155 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003156 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003157
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003158 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003159 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003160 func = btrfs_freespace_write_helper;
3161 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003162 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003163 func = btrfs_endio_write_helper;
3164 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003165
Liu Bo9e0af232014-08-15 23:36:53 +08003166 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3167 NULL);
3168 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003169}
3170
Miao Xiedc380ae2014-09-12 18:43:55 +08003171static int __readpage_endio_check(struct inode *inode,
3172 struct btrfs_io_bio *io_bio,
3173 int icsum, struct page *page,
3174 int pgoff, u64 start, size_t len)
3175{
3176 char *kaddr;
3177 u32 csum_expected;
3178 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003179
3180 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3181
3182 kaddr = kmap_atomic(page);
3183 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003184 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003185 if (csum != csum_expected)
3186 goto zeroit;
3187
3188 kunmap_atomic(kaddr);
3189 return 0;
3190zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003191 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003192 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003193 memset(kaddr + pgoff, 1, len);
3194 flush_dcache_page(page);
3195 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003196 return -EIO;
3197}
3198
Chris Masond352ac62008-09-29 15:18:18 -04003199/*
Chris Masond352ac62008-09-29 15:18:18 -04003200 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003201 * if there's a match, we allow the bio to finish. If not, the code in
3202 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003203 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003204static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3205 u64 phy_offset, struct page *page,
3206 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003207{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003208 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003209 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003210 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003211 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003212
Chris Masond20f7042008-12-08 16:58:54 -05003213 if (PageChecked(page)) {
3214 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003215 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003216 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003217
3218 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003219 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003220
Yan Zheng17d217f2008-12-12 10:03:38 -05003221 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003222 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003223 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003224 return 0;
3225 }
3226
Miao Xiefacc8a222013-07-25 19:22:34 +08003227 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003228 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3229 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003230}
Chris Masonb888db22007-08-27 16:49:44 -04003231
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003232void btrfs_add_delayed_iput(struct inode *inode)
3233{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003234 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003235 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003236
3237 if (atomic_add_unless(&inode->i_count, -1, 1))
3238 return;
3239
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003240 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003241 if (binode->delayed_iput_count == 0) {
3242 ASSERT(list_empty(&binode->delayed_iput));
3243 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3244 } else {
3245 binode->delayed_iput_count++;
3246 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003247 spin_unlock(&fs_info->delayed_iput_lock);
3248}
3249
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003250void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003251{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003252
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003253 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003254 while (!list_empty(&fs_info->delayed_iputs)) {
3255 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003256
David Sterba8089fe62015-11-19 14:15:51 +01003257 inode = list_first_entry(&fs_info->delayed_iputs,
3258 struct btrfs_inode, delayed_iput);
3259 if (inode->delayed_iput_count) {
3260 inode->delayed_iput_count--;
3261 list_move_tail(&inode->delayed_iput,
3262 &fs_info->delayed_iputs);
3263 } else {
3264 list_del_init(&inode->delayed_iput);
3265 }
3266 spin_unlock(&fs_info->delayed_iput_lock);
3267 iput(&inode->vfs_inode);
3268 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003269 }
David Sterba8089fe62015-11-19 14:15:51 +01003270 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003271}
3272
Yan, Zhengd68fc572010-05-16 10:49:58 -04003273/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003274 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003275 * files in the subvolume, it removes orphan item and frees block_rsv
3276 * structure.
3277 */
3278void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3279 struct btrfs_root *root)
3280{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003281 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003282 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003283 int ret;
3284
Josef Bacik8a35d952012-05-23 14:26:42 -04003285 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003286 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3287 return;
3288
Josef Bacik90290e12011-12-02 15:44:12 -05003289 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003290 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003291 spin_unlock(&root->orphan_lock);
3292 return;
3293 }
3294
3295 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3296 spin_unlock(&root->orphan_lock);
3297 return;
3298 }
3299
3300 block_rsv = root->orphan_block_rsv;
3301 root->orphan_block_rsv = NULL;
3302 spin_unlock(&root->orphan_lock);
3303
Miao Xie27cdeb72014-04-02 19:51:05 +08003304 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003305 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003306 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003307 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003308 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003309 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003310 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003311 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3312 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003313 }
3314
Josef Bacik90290e12011-12-02 15:44:12 -05003315 if (block_rsv) {
3316 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003317 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003318 }
3319}
3320
3321/*
Josef Bacik7b128762008-07-24 12:17:14 -04003322 * This creates an orphan entry for the given inode in case something goes
3323 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003324 *
3325 * NOTE: caller of this function should reserve 5 units of metadata for
3326 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003327 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003328int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3329 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003330{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003331 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3332 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003333 struct btrfs_block_rsv *block_rsv = NULL;
3334 int reserve = 0;
3335 int insert = 0;
3336 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003337
Yan, Zhengd68fc572010-05-16 10:49:58 -04003338 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003339 block_rsv = btrfs_alloc_block_rsv(fs_info,
3340 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003341 if (!block_rsv)
3342 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003343 }
3344
Yan, Zhengd68fc572010-05-16 10:49:58 -04003345 spin_lock(&root->orphan_lock);
3346 if (!root->orphan_block_rsv) {
3347 root->orphan_block_rsv = block_rsv;
3348 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003349 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003350 block_rsv = NULL;
3351 }
Josef Bacik7b128762008-07-24 12:17:14 -04003352
Josef Bacik8a35d952012-05-23 14:26:42 -04003353 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003354 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003355#if 0
3356 /*
3357 * For proper ENOSPC handling, we should do orphan
3358 * cleanup when mounting. But this introduces backward
3359 * compatibility issue.
3360 */
3361 if (!xchg(&root->orphan_item_inserted, 1))
3362 insert = 2;
3363 else
3364 insert = 1;
3365#endif
3366 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003367 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003368 }
Josef Bacik7b128762008-07-24 12:17:14 -04003369
Josef Bacik72ac3c02012-05-23 14:13:11 -04003370 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003371 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003372 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003373 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003374
Yan, Zhengd68fc572010-05-16 10:49:58 -04003375 /* grab metadata reservation from transaction handle */
3376 if (reserve) {
3377 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003378 ASSERT(!ret);
3379 if (ret) {
3380 atomic_dec(&root->orphan_inodes);
3381 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003382 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003383 if (insert)
3384 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003385 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003386 return ret;
3387 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003388 }
3389
3390 /* insert an orphan item to track this unlinked/truncated file */
3391 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003392 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003393 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003394 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003395 if (reserve) {
3396 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003397 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003398 btrfs_orphan_release_metadata(inode);
3399 }
3400 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003401 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003402 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003403 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003404 return ret;
3405 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003406 }
3407 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003408 }
3409
3410 /* insert an orphan item to track subvolume contains orphan files */
3411 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003412 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003413 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003414 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003415 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003416 return ret;
3417 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003418 }
3419 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003420}
3421
3422/*
3423 * We have done the truncate/delete so we can go ahead and remove the orphan
3424 * item for this particular inode.
3425 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003426static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003427 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003428{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003429 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003430 int delete_item = 0;
3431 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003432 int ret = 0;
3433
Yan, Zhengd68fc572010-05-16 10:49:58 -04003434 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003435 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003436 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003437 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003438
Josef Bacik72ac3c02012-05-23 14:13:11 -04003439 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003440 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003441 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003442 spin_unlock(&root->orphan_lock);
3443
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003444 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003445 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003446 if (trans)
3447 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003448 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003449 }
Josef Bacik7b128762008-07-24 12:17:14 -04003450
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003451 if (release_rsv)
3452 btrfs_orphan_release_metadata(inode);
3453
Josef Bacik4ef31a42013-08-13 14:10:08 -04003454 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003455}
3456
3457/*
3458 * this cleans up any orphans that may be left on the list from the last use
3459 * of this root.
3460 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003461int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003462{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003463 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003464 struct btrfs_path *path;
3465 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003466 struct btrfs_key key, found_key;
3467 struct btrfs_trans_handle *trans;
3468 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003469 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003470 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3471
Yan, Zhengd68fc572010-05-16 10:49:58 -04003472 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003473 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003474
3475 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003476 if (!path) {
3477 ret = -ENOMEM;
3478 goto out;
3479 }
David Sterbae4058b52015-11-27 16:31:35 +01003480 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003481
3482 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003483 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003484 key.offset = (u64)-1;
3485
Josef Bacik7b128762008-07-24 12:17:14 -04003486 while (1) {
3487 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003488 if (ret < 0)
3489 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003490
3491 /*
3492 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003493 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003494 * find the key and see if we have stuff that matches
3495 */
3496 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003497 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003498 if (path->slots[0] == 0)
3499 break;
3500 path->slots[0]--;
3501 }
3502
3503 /* pull out the item */
3504 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003505 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3506
3507 /* make sure the item matches what we want */
3508 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3509 break;
David Sterba962a2982014-06-04 18:41:45 +02003510 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003511 break;
3512
3513 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003514 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003515
3516 /*
3517 * this is where we are basically btrfs_lookup, without the
3518 * crossing root thing. we store the inode number in the
3519 * offset of the orphan item.
3520 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003521
3522 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003523 btrfs_err(fs_info,
3524 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003525 ret = -EINVAL;
3526 goto out;
3527 }
3528
3529 last_objectid = found_key.offset;
3530
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003531 found_key.objectid = found_key.offset;
3532 found_key.type = BTRFS_INODE_ITEM_KEY;
3533 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003534 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303535 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003536 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003537 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003538
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003539 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003540 struct btrfs_root *dead_root;
3541 struct btrfs_fs_info *fs_info = root->fs_info;
3542 int is_dead_root = 0;
3543
3544 /*
3545 * this is an orphan in the tree root. Currently these
3546 * could come from 2 sources:
3547 * a) a snapshot deletion in progress
3548 * b) a free space cache inode
3549 * We need to distinguish those two, as the snapshot
3550 * orphan must not get deleted.
3551 * find_dead_roots already ran before us, so if this
3552 * is a snapshot deletion, we should find the root
3553 * in the dead_roots list
3554 */
3555 spin_lock(&fs_info->trans_lock);
3556 list_for_each_entry(dead_root, &fs_info->dead_roots,
3557 root_list) {
3558 if (dead_root->root_key.objectid ==
3559 found_key.objectid) {
3560 is_dead_root = 1;
3561 break;
3562 }
3563 }
3564 spin_unlock(&fs_info->trans_lock);
3565 if (is_dead_root) {
3566 /* prevent this orphan from being found again */
3567 key.offset = found_key.objectid - 1;
3568 continue;
3569 }
3570 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003571 /*
3572 * Inode is already gone but the orphan item is still there,
3573 * kill the orphan item.
3574 */
Filipe Manana67710892016-06-06 11:51:25 +01003575 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003576 trans = btrfs_start_transaction(root, 1);
3577 if (IS_ERR(trans)) {
3578 ret = PTR_ERR(trans);
3579 goto out;
3580 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003581 btrfs_debug(fs_info, "auto deleting %Lu",
3582 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003583 ret = btrfs_del_orphan_item(trans, root,
3584 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003585 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003586 if (ret)
3587 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003588 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003589 }
Josef Bacik7b128762008-07-24 12:17:14 -04003590
Josef Bacik7b128762008-07-24 12:17:14 -04003591 /*
3592 * add this inode to the orphan list so btrfs_orphan_del does
3593 * the proper thing when we hit it
3594 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003595 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3596 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003597 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003598
Josef Bacik7b128762008-07-24 12:17:14 -04003599 /* if we have links, this was a truncate, lets do that */
3600 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303601 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003602 iput(inode);
3603 continue;
3604 }
Josef Bacik7b128762008-07-24 12:17:14 -04003605 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003606
3607 /* 1 for the orphan item deletion. */
3608 trans = btrfs_start_transaction(root, 1);
3609 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003610 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003611 ret = PTR_ERR(trans);
3612 goto out;
3613 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003614 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003615 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003616 if (ret) {
3617 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003618 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003619 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003620
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003621 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003622 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003623 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003624 } else {
3625 nr_unlink++;
3626 }
3627
3628 /* this will do delete_inode and everything for us */
3629 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003630 if (ret)
3631 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003632 }
Miao Xie3254c872011-11-10 20:45:05 -05003633 /* release the path since we're done with it */
3634 btrfs_release_path(path);
3635
Yan, Zhengd68fc572010-05-16 10:49:58 -04003636 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3637
3638 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003639 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003640 (u64)-1);
3641
Miao Xie27cdeb72014-04-02 19:51:05 +08003642 if (root->orphan_block_rsv ||
3643 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003644 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003645 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003646 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003647 }
Josef Bacik7b128762008-07-24 12:17:14 -04003648
3649 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003650 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003651 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003652 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003653
3654out:
3655 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003656 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003657 btrfs_free_path(path);
3658 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003659}
3660
Chris Masond352ac62008-09-29 15:18:18 -04003661/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003662 * very simple check to peek ahead in the leaf looking for xattrs. If we
3663 * don't find any xattrs, we know there can't be any acls.
3664 *
3665 * slot is the slot the inode is in, objectid is the objectid of the inode
3666 */
3667static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003668 int slot, u64 objectid,
3669 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003670{
3671 u32 nritems = btrfs_header_nritems(leaf);
3672 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003673 static u64 xattr_access = 0;
3674 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003675 int scanned = 0;
3676
Josef Bacikf23b5a52013-06-19 10:16:26 -04003677 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003678 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3679 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3680 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3681 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003682 }
3683
Chris Mason46a53cc2009-04-27 11:47:50 -04003684 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003685 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003686 while (slot < nritems) {
3687 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3688
3689 /* we found a different objectid, there must not be acls */
3690 if (found_key.objectid != objectid)
3691 return 0;
3692
3693 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003694 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003695 if (*first_xattr_slot == -1)
3696 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003697 if (found_key.offset == xattr_access ||
3698 found_key.offset == xattr_default)
3699 return 1;
3700 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003701
3702 /*
3703 * we found a key greater than an xattr key, there can't
3704 * be any acls later on
3705 */
3706 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3707 return 0;
3708
3709 slot++;
3710 scanned++;
3711
3712 /*
3713 * it goes inode, inode backrefs, xattrs, extents,
3714 * so if there are a ton of hard links to an inode there can
3715 * be a lot of backrefs. Don't waste time searching too hard,
3716 * this is just an optimization
3717 */
3718 if (scanned >= 8)
3719 break;
3720 }
3721 /* we hit the end of the leaf before we found an xattr or
3722 * something larger than an xattr. We have to assume the inode
3723 * has acls
3724 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003725 if (*first_xattr_slot == -1)
3726 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003727 return 1;
3728}
3729
3730/*
Chris Masond352ac62008-09-29 15:18:18 -04003731 * read an inode from the btree into the in-memory inode
3732 */
Filipe Manana67710892016-06-06 11:51:25 +01003733static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003734{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003735 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003736 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003737 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003738 struct btrfs_inode_item *inode_item;
3739 struct btrfs_root *root = BTRFS_I(inode)->root;
3740 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003741 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003742 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003743 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003744 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003745 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003746 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003747
3748 ret = btrfs_fill_inode(inode, &rdev);
3749 if (!ret)
3750 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003751
3752 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003753 if (!path) {
3754 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003755 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003756 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003757
Chris Mason39279cc2007-06-12 06:35:45 -04003758 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003759
Chris Mason39279cc2007-06-12 06:35:45 -04003760 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003761 if (ret) {
3762 if (ret > 0)
3763 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003764 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003765 }
Chris Mason39279cc2007-06-12 06:35:45 -04003766
Chris Mason5f39d392007-10-15 16:14:19 -04003767 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003768
3769 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003770 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003771
Chris Mason5f39d392007-10-15 16:14:19 -04003772 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3773 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003774 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003775 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003776 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3777 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003778 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003779
David Sterbaa937b972014-12-12 17:39:12 +01003780 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3781 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003782
David Sterbaa937b972014-12-12 17:39:12 +01003783 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3784 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003785
David Sterbaa937b972014-12-12 17:39:12 +01003786 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3787 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003788
chandan r9cc97d62012-07-04 12:48:07 +05303789 BTRFS_I(inode)->i_otime.tv_sec =
3790 btrfs_timespec_sec(leaf, &inode_item->otime);
3791 BTRFS_I(inode)->i_otime.tv_nsec =
3792 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003793
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003794 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003795 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003796 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3797
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003798 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003799 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003800 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003801 rdev = btrfs_inode_rdev(leaf, inode_item);
3802
Josef Bacikaec74772008-07-24 12:12:38 -04003803 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003804 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003805
3806cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003807 /*
3808 * If we were modified in the current generation and evicted from memory
3809 * and then re-read we need to do a full sync since we don't have any
3810 * idea about which extents were modified before we were evicted from
3811 * cache.
3812 *
3813 * This is required for both inode re-read from disk and delayed inode
3814 * in delayed_nodes_tree.
3815 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003816 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003817 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3818 &BTRFS_I(inode)->runtime_flags);
3819
Filipe Mananabde6c242015-07-24 00:00:19 +01003820 /*
3821 * We don't persist the id of the transaction where an unlink operation
3822 * against the inode was last made. So here we assume the inode might
3823 * have been evicted, and therefore the exact value of last_unlink_trans
3824 * lost, and set it to last_trans to avoid metadata inconsistencies
3825 * between the inode and its parent if the inode is fsync'ed and the log
3826 * replayed. For example, in the scenario:
3827 *
3828 * touch mydir/foo
3829 * ln mydir/foo mydir/bar
3830 * sync
3831 * unlink mydir/bar
3832 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3833 * xfs_io -c fsync mydir/foo
3834 * <power failure>
3835 * mount fs, triggers fsync log replay
3836 *
3837 * We must make sure that when we fsync our inode foo we also log its
3838 * parent inode, otherwise after log replay the parent still has the
3839 * dentry with the "bar" name but our inode foo has a link count of 1
3840 * and doesn't have an inode ref with the name "bar" anymore.
3841 *
3842 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003843 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003844 * transaction commits on fsync if our inode is a directory, or if our
3845 * inode is not a directory, logging its parent unnecessarily.
3846 */
3847 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3848
Miao Xie67de1172013-12-26 13:07:06 +08003849 path->slots[0]++;
3850 if (inode->i_nlink != 1 ||
3851 path->slots[0] >= btrfs_header_nritems(leaf))
3852 goto cache_acl;
3853
3854 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003855 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003856 goto cache_acl;
3857
3858 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3859 if (location.type == BTRFS_INODE_REF_KEY) {
3860 struct btrfs_inode_ref *ref;
3861
3862 ref = (struct btrfs_inode_ref *)ptr;
3863 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3864 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3865 struct btrfs_inode_extref *extref;
3866
3867 extref = (struct btrfs_inode_extref *)ptr;
3868 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3869 extref);
3870 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003871cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003872 /*
3873 * try to precache a NULL acl entry for files that don't have
3874 * any xattrs or acls
3875 */
Li Zefan33345d012011-04-20 10:31:50 +08003876 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003877 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003878 if (first_xattr_slot != -1) {
3879 path->slots[0] = first_xattr_slot;
3880 ret = btrfs_load_inode_props(inode, path);
3881 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003882 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003883 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003884 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003885 root->root_key.objectid, ret);
3886 }
3887 btrfs_free_path(path);
3888
Al Viro72c04902009-06-24 16:58:48 -04003889 if (!maybe_acls)
3890 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003891
Chris Mason39279cc2007-06-12 06:35:45 -04003892 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003893 case S_IFREG:
3894 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003895 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003896 inode->i_fop = &btrfs_file_operations;
3897 inode->i_op = &btrfs_file_inode_operations;
3898 break;
3899 case S_IFDIR:
3900 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003901 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003902 break;
3903 case S_IFLNK:
3904 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003905 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003906 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3907 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003908 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003909 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003910 init_special_inode(inode, inode->i_mode, rdev);
3911 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003912 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003913
3914 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003915 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003916
3917make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003918 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003919 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003920 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003921}
3922
Chris Masond352ac62008-09-29 15:18:18 -04003923/*
3924 * given a leaf and an inode, copy the inode fields into the leaf
3925 */
Chris Masone02119d2008-09-05 16:13:11 -04003926static void fill_inode_item(struct btrfs_trans_handle *trans,
3927 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003928 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003929 struct inode *inode)
3930{
Liu Bo51fab692012-12-27 09:01:21 +00003931 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003932
Liu Bo51fab692012-12-27 09:01:21 +00003933 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003934
Liu Bo51fab692012-12-27 09:01:21 +00003935 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3936 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3937 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3938 &token);
3939 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3940 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003941
David Sterbaa937b972014-12-12 17:39:12 +01003942 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003943 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003944 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003945 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003946
David Sterbaa937b972014-12-12 17:39:12 +01003947 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003948 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003949 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003950 inode->i_mtime.tv_nsec, &token);
3951
David Sterbaa937b972014-12-12 17:39:12 +01003952 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003953 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003954 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003955 inode->i_ctime.tv_nsec, &token);
3956
chandan r9cc97d62012-07-04 12:48:07 +05303957 btrfs_set_token_timespec_sec(leaf, &item->otime,
3958 BTRFS_I(inode)->i_otime.tv_sec, &token);
3959 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3960 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3961
Liu Bo51fab692012-12-27 09:01:21 +00003962 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3963 &token);
3964 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3965 &token);
3966 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3967 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3968 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3969 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3970 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003971}
3972
Chris Masond352ac62008-09-29 15:18:18 -04003973/*
3974 * copy everything in the in-memory inode into the btree.
3975 */
Chris Mason21151332011-11-10 20:39:08 -05003976static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003977 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003978{
3979 struct btrfs_inode_item *inode_item;
3980 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003981 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003982 int ret;
3983
3984 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003985 if (!path)
3986 return -ENOMEM;
3987
Chris Masonb9473432009-03-13 11:00:37 -04003988 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003989 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3990 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003991 if (ret) {
3992 if (ret > 0)
3993 ret = -ENOENT;
3994 goto failed;
3995 }
3996
Chris Mason5f39d392007-10-15 16:14:19 -04003997 leaf = path->nodes[0];
3998 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003999 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004000
Chris Masone02119d2008-09-05 16:13:11 -04004001 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004002 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004003 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004004 ret = 0;
4005failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004006 btrfs_free_path(path);
4007 return ret;
4008}
4009
Chris Masond352ac62008-09-29 15:18:18 -04004010/*
Chris Mason21151332011-11-10 20:39:08 -05004011 * copy everything in the in-memory inode into the btree.
4012 */
4013noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4014 struct btrfs_root *root, struct inode *inode)
4015{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004016 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004017 int ret;
4018
4019 /*
4020 * If the inode is a free space inode, we can deadlock during commit
4021 * if we put it into the delayed code.
4022 *
4023 * The data relocation inode should also be directly updated
4024 * without delay
4025 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004026 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004027 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004028 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004029 btrfs_update_root_times(trans, root);
4030
Chris Mason21151332011-11-10 20:39:08 -05004031 ret = btrfs_delayed_update_inode(trans, root, inode);
4032 if (!ret)
4033 btrfs_set_inode_last_trans(trans, inode);
4034 return ret;
4035 }
4036
4037 return btrfs_update_inode_item(trans, root, inode);
4038}
4039
Josef Bacikbe6aef62012-10-22 15:43:12 -04004040noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4041 struct btrfs_root *root,
4042 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004043{
4044 int ret;
4045
4046 ret = btrfs_update_inode(trans, root, inode);
4047 if (ret == -ENOSPC)
4048 return btrfs_update_inode_item(trans, root, inode);
4049 return ret;
4050}
4051
4052/*
Chris Masond352ac62008-09-29 15:18:18 -04004053 * unlink helper that gets used here in inode.c and in the tree logging
4054 * recovery code. It remove a link in a directory with a given name, and
4055 * also drops the back refs in the inode to the directory
4056 */
Al Viro92986792011-03-04 17:14:37 +00004057static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4058 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004059 struct btrfs_inode *dir,
4060 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004061 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004062{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004063 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004064 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004065 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004066 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004067 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004068 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004069 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004070 u64 ino = btrfs_ino(inode);
4071 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004072
4073 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004074 if (!path) {
4075 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004076 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004077 }
4078
Chris Masonb9473432009-03-13 11:00:37 -04004079 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004080 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004081 name, name_len, -1);
4082 if (IS_ERR(di)) {
4083 ret = PTR_ERR(di);
4084 goto err;
4085 }
4086 if (!di) {
4087 ret = -ENOENT;
4088 goto err;
4089 }
Chris Mason5f39d392007-10-15 16:14:19 -04004090 leaf = path->nodes[0];
4091 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004092 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004093 if (ret)
4094 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004095 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004096
Miao Xie67de1172013-12-26 13:07:06 +08004097 /*
4098 * If we don't have dir index, we have to get it by looking up
4099 * the inode ref, since we get the inode ref, remove it directly,
4100 * it is unnecessary to do delayed deletion.
4101 *
4102 * But if we have dir index, needn't search inode ref to get it.
4103 * Since the inode ref is close to the inode item, it is better
4104 * that we delay to delete it, and just do this deletion when
4105 * we update the inode item.
4106 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004107 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004108 ret = btrfs_delayed_delete_inode_ref(inode);
4109 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004110 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004111 goto skip_backref;
4112 }
4113 }
4114
Li Zefan33345d012011-04-20 10:31:50 +08004115 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4116 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004117 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004118 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004119 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004120 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004121 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004122 goto err;
4123 }
Miao Xie67de1172013-12-26 13:07:06 +08004124skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004125 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004126 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004127 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004128 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004129 }
Chris Mason39279cc2007-06-12 06:35:45 -04004130
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004131 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4132 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004133 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004134 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004135 goto err;
4136 }
Chris Masone02119d2008-09-05 16:13:11 -04004137
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004138 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4139 index);
Chris Mason6418c962010-10-30 07:34:24 -04004140 if (ret == -ENOENT)
4141 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004142 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004143 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004144err:
4145 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004146 if (ret)
4147 goto out;
4148
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004149 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004150 inode_inc_iversion(&inode->vfs_inode);
4151 inode_inc_iversion(&dir->vfs_inode);
4152 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4153 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4154 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004155out:
Chris Mason39279cc2007-06-12 06:35:45 -04004156 return ret;
4157}
4158
Al Viro92986792011-03-04 17:14:37 +00004159int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4160 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004161 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004162 const char *name, int name_len)
4163{
4164 int ret;
4165 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4166 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004167 drop_nlink(&inode->vfs_inode);
4168 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004169 }
4170 return ret;
4171}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004172
4173/*
4174 * helper to start transaction for unlink and rmdir.
4175 *
Josef Bacikd52be812013-05-29 14:54:47 -04004176 * unlink and rmdir are special in btrfs, they do not always free space, so
4177 * if we cannot make our reservations the normal way try and see if there is
4178 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4179 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004180 */
Josef Bacikd52be812013-05-29 14:54:47 -04004181static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004182{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004183 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004184
Josef Bacike70bea52011-10-11 14:18:24 -04004185 /*
4186 * 1 for the possible orphan item
4187 * 1 for the dir item
4188 * 1 for the dir index
4189 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004190 * 1 for the inode
4191 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004192 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004193}
4194
Chris Mason39279cc2007-06-12 06:35:45 -04004195static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4196{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004197 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004198 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004199 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004200 int ret;
4201
Josef Bacikd52be812013-05-29 14:54:47 -04004202 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004203 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004204 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004205
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004206 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4207 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004208
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004209 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4210 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4211 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004212 if (ret)
4213 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004214
Yan, Zhenga22285a2010-05-16 10:48:46 -04004215 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004216 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004217 if (ret)
4218 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004219 }
Josef Bacik7b128762008-07-24 12:17:14 -04004220
Tsutomu Itohb5324022011-07-19 07:27:20 +00004221out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004222 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004223 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004224 return ret;
4225}
4226
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004227int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4228 struct btrfs_root *root,
4229 struct inode *dir, u64 objectid,
4230 const char *name, int name_len)
4231{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004232 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004233 struct btrfs_path *path;
4234 struct extent_buffer *leaf;
4235 struct btrfs_dir_item *di;
4236 struct btrfs_key key;
4237 u64 index;
4238 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004239 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004240
4241 path = btrfs_alloc_path();
4242 if (!path)
4243 return -ENOMEM;
4244
Li Zefan33345d012011-04-20 10:31:50 +08004245 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004246 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004247 if (IS_ERR_OR_NULL(di)) {
4248 if (!di)
4249 ret = -ENOENT;
4250 else
4251 ret = PTR_ERR(di);
4252 goto out;
4253 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004254
4255 leaf = path->nodes[0];
4256 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4257 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4258 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004259 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004260 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004261 goto out;
4262 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004263 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004264
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004265 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4266 root->root_key.objectid, dir_ino,
4267 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004268 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004269 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004270 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004271 goto out;
4272 }
Li Zefan33345d012011-04-20 10:31:50 +08004273 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004274 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004275 if (IS_ERR_OR_NULL(di)) {
4276 if (!di)
4277 ret = -ENOENT;
4278 else
4279 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004280 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004281 goto out;
4282 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004283
4284 leaf = path->nodes[0];
4285 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004286 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004287 index = key.offset;
4288 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004289 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004290
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004291 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004292 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004293 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004294 goto out;
4295 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004296
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004297 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004298 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004299 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004300 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004301 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004302 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004303out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004304 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004305 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004306}
4307
Chris Mason39279cc2007-06-12 06:35:45 -04004308static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4309{
David Howells2b0143b2015-03-17 22:25:59 +00004310 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004311 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004312 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004313 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004314 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004315
David Sterbab3ae2442012-09-13 16:04:34 -06004316 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004317 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004318 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004319 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004320
Josef Bacikd52be812013-05-29 14:54:47 -04004321 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004322 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004323 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004324
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004325 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004326 err = btrfs_unlink_subvol(trans, root, dir,
4327 BTRFS_I(inode)->location.objectid,
4328 dentry->d_name.name,
4329 dentry->d_name.len);
4330 goto out;
4331 }
4332
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004333 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004334 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004335 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004336
Filipe Manana44f714d2016-06-06 16:11:13 +01004337 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4338
Chris Mason39279cc2007-06-12 06:35:45 -04004339 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004340 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4341 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4342 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004343 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004344 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004345 /*
4346 * Propagate the last_unlink_trans value of the deleted dir to
4347 * its parent directory. This is to prevent an unrecoverable
4348 * log tree in the case we do something like this:
4349 * 1) create dir foo
4350 * 2) create snapshot under dir foo
4351 * 3) delete the snapshot
4352 * 4) rmdir foo
4353 * 5) mkdir foo
4354 * 6) fsync foo or some file inside foo
4355 */
4356 if (last_unlink_trans >= trans->transid)
4357 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4358 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004359out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004360 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004361 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004362
Chris Mason39279cc2007-06-12 06:35:45 -04004363 return err;
4364}
4365
Chris Mason28f75a02015-02-04 06:59:29 -08004366static int truncate_space_check(struct btrfs_trans_handle *trans,
4367 struct btrfs_root *root,
4368 u64 bytes_deleted)
4369{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004370 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004371 int ret;
4372
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004373 /*
4374 * This is only used to apply pressure to the enospc system, we don't
4375 * intend to use this reservation at all.
4376 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004377 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004378 bytes_deleted *= fs_info->nodesize;
4379 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004380 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004381 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004382 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004383 trans->transid,
4384 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004385 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004386 }
Chris Mason28f75a02015-02-04 06:59:29 -08004387 return ret;
4388
4389}
4390
Josef Bacikddfae632017-10-19 14:16:02 -04004391/*
4392 * Return this if we need to call truncate_block for the last bit of the
4393 * truncate.
4394 */
4395#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004396
Chris Mason323ac952008-10-01 19:05:46 -04004397/*
Chris Mason39279cc2007-06-12 06:35:45 -04004398 * this can truncate away extent items, csum items and directory items.
4399 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004400 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004401 *
4402 * csum items that cross the new i_size are truncated to the new size
4403 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004404 *
4405 * min_type is the minimum key type to truncate down to. If set to 0, this
4406 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004407 */
Yan, Zheng80825102009-11-12 09:35:36 +00004408int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4409 struct btrfs_root *root,
4410 struct inode *inode,
4411 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004412{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004413 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004414 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004415 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004416 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004417 struct btrfs_key key;
4418 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004419 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004420 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004421 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004422 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004423 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004424 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004425 int found_extent;
4426 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004427 int pending_del_nr = 0;
4428 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004429 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004430 int ret;
4431 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004432 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004433 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004434 bool be_nice = false;
4435 bool should_throttle = false;
4436 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004437
4438 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004439
Chris Mason28ed1342014-12-17 09:41:04 -08004440 /*
4441 * for non-free space inodes and ref cows, we want to back off from
4442 * time to time
4443 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004444 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004445 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004446 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004447
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004448 path = btrfs_alloc_path();
4449 if (!path)
4450 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004451 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004452
Josef Bacik5dc562c2012-08-17 13:14:17 -04004453 /*
4454 * We want to drop from the next block forward in case this new size is
4455 * not block aligned since we will be keeping the last block of the
4456 * extent just the way it is.
4457 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004458 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004459 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004460 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004461 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004462 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004463
Miao Xie16cdcec2011-04-22 18:12:22 +08004464 /*
4465 * This function is also used to drop the items in the log tree before
4466 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4467 * it is used to drop the loged items. So we shouldn't kill the delayed
4468 * items.
4469 */
4470 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004471 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004472
Li Zefan33345d012011-04-20 10:31:50 +08004473 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004474 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004475 key.type = (u8)-1;
4476
Chris Mason85e21ba2008-01-29 15:11:36 -05004477search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004478 /*
4479 * with a 16K leaf size and 128MB extents, you can actually queue
4480 * up a huge file in a single leaf. Most of the time that
4481 * bytes_deleted is > 0, it will be huge by the time we get here
4482 */
Byongho Leeee221842015-12-15 01:42:10 +09004483 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004484 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004485 err = -EAGAIN;
4486 goto error;
4487 }
4488 }
4489
4490
Chris Masonb9473432009-03-13 11:00:37 -04004491 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004492 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004493 if (ret < 0) {
4494 err = ret;
4495 goto out;
4496 }
Chris Masond3977122009-01-05 21:25:51 -05004497
Chris Mason85e21ba2008-01-29 15:11:36 -05004498 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004499 /* there are no items in the tree for us to truncate, we're
4500 * done
4501 */
Yan, Zheng80825102009-11-12 09:35:36 +00004502 if (path->slots[0] == 0)
4503 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004504 path->slots[0]--;
4505 }
4506
Chris Masond3977122009-01-05 21:25:51 -05004507 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004508 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004509 leaf = path->nodes[0];
4510 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004511 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004512
Li Zefan33345d012011-04-20 10:31:50 +08004513 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004514 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004515
Chris Mason85e21ba2008-01-29 15:11:36 -05004516 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004517 break;
4518
Chris Mason5f39d392007-10-15 16:14:19 -04004519 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004520 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004521 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004522 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004523 extent_type = btrfs_file_extent_type(leaf, fi);
4524 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004525 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004526 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004527
4528 trace_btrfs_truncate_show_fi_regular(
4529 BTRFS_I(inode), leaf, fi,
4530 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004531 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004532 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004533 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004534
4535 trace_btrfs_truncate_show_fi_inline(
4536 BTRFS_I(inode), leaf, fi, path->slots[0],
4537 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004538 }
Yan008630c2007-11-07 13:31:09 -05004539 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004540 }
Yan, Zheng80825102009-11-12 09:35:36 +00004541 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004542 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004543 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004544 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004545 break;
4546 if (found_key.offset >= new_size)
4547 del_item = 1;
4548 else
4549 del_item = 0;
4550 }
Chris Mason39279cc2007-06-12 06:35:45 -04004551 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004552 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004553 if (found_type != BTRFS_EXTENT_DATA_KEY)
4554 goto delete;
4555
4556 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004557 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004558 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004559 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004560 u64 orig_num_bytes =
4561 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004562 extent_num_bytes = ALIGN(new_size -
4563 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004564 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004565 btrfs_set_file_extent_num_bytes(leaf, fi,
4566 extent_num_bytes);
4567 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004568 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004569 if (test_bit(BTRFS_ROOT_REF_COWS,
4570 &root->state) &&
4571 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004572 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004573 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004574 } else {
Chris Masondb945352007-10-15 16:15:53 -04004575 extent_num_bytes =
4576 btrfs_file_extent_disk_num_bytes(leaf,
4577 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004578 extent_offset = found_key.offset -
4579 btrfs_file_extent_offset(leaf, fi);
4580
Chris Mason39279cc2007-06-12 06:35:45 -04004581 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004582 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004583 if (extent_start != 0) {
4584 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004585 if (test_bit(BTRFS_ROOT_REF_COWS,
4586 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004587 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004588 }
4589 }
Chris Mason90692182008-02-08 13:49:28 -05004590 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004591 /*
4592 * we can't truncate inline items that have had
4593 * special encodings
4594 */
4595 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004596 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004597 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4598 btrfs_file_extent_compression(leaf, fi) == 0) {
4599 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004600
Josef Bacikddfae632017-10-19 14:16:02 -04004601 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4602 size = btrfs_file_extent_calc_inline_size(size);
4603 btrfs_truncate_item(root->fs_info, path, size, 1);
4604 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004605 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004606 * We have to bail so the last_size is set to
4607 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004608 */
Josef Bacikddfae632017-10-19 14:16:02 -04004609 err = NEED_TRUNCATE_BLOCK;
4610 break;
Chris Mason90692182008-02-08 13:49:28 -05004611 }
Josef Bacikddfae632017-10-19 14:16:02 -04004612
4613 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4614 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004615 }
Chris Mason179e29e2007-11-01 11:28:41 -04004616delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004617 if (del_item)
4618 last_size = found_key.offset;
4619 else
4620 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004621 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004622 if (!pending_del_nr) {
4623 /* no pending yet, add ourselves */
4624 pending_del_slot = path->slots[0];
4625 pending_del_nr = 1;
4626 } else if (pending_del_nr &&
4627 path->slots[0] + 1 == pending_del_slot) {
4628 /* hop on the pending chunk */
4629 pending_del_nr++;
4630 pending_del_slot = path->slots[0];
4631 } else {
Chris Masond3977122009-01-05 21:25:51 -05004632 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004633 }
Chris Mason39279cc2007-06-12 06:35:45 -04004634 } else {
4635 break;
4636 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004637 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004638
Miao Xie27cdeb72014-04-02 19:51:05 +08004639 if (found_extent &&
4640 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004641 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004642 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004643 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004644 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004645 extent_num_bytes, 0,
4646 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004647 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004648 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004649 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4650 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004651 trans->delayed_ref_updates * 2,
4652 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004653 if (be_nice) {
4654 if (truncate_space_check(trans, root,
4655 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004656 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004657 }
4658 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004659 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004660 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004661 }
Chris Mason39279cc2007-06-12 06:35:45 -04004662 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004663
Yan, Zheng80825102009-11-12 09:35:36 +00004664 if (found_type == BTRFS_INODE_ITEM_KEY)
4665 break;
4666
4667 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004668 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004669 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004670 if (pending_del_nr) {
4671 ret = btrfs_del_items(trans, root, path,
4672 pending_del_slot,
4673 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004674 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004675 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004676 goto error;
4677 }
Yan, Zheng80825102009-11-12 09:35:36 +00004678 pending_del_nr = 0;
4679 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004680 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004681 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004682 unsigned long updates = trans->delayed_ref_updates;
4683 if (updates) {
4684 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004685 ret = btrfs_run_delayed_refs(trans,
4686 fs_info,
4687 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004688 if (ret && !err)
4689 err = ret;
4690 }
4691 }
Chris Mason28f75a02015-02-04 06:59:29 -08004692 /*
4693 * if we failed to refill our space rsv, bail out
4694 * and let the transaction restart
4695 */
4696 if (should_end) {
4697 err = -EAGAIN;
4698 goto error;
4699 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004700 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004701 } else {
4702 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004703 }
Chris Mason39279cc2007-06-12 06:35:45 -04004704 }
Yan, Zheng80825102009-11-12 09:35:36 +00004705out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004706 if (pending_del_nr) {
4707 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4708 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004709 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004710 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004711 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004712error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004713 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4714 ASSERT(last_size >= new_size);
4715 if (!err && last_size > new_size)
4716 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004717 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004718 }
Chris Mason28ed1342014-12-17 09:41:04 -08004719
Chris Mason39279cc2007-06-12 06:35:45 -04004720 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004721
Byongho Leeee221842015-12-15 01:42:10 +09004722 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004723 unsigned long updates = trans->delayed_ref_updates;
4724 if (updates) {
4725 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004726 ret = btrfs_run_delayed_refs(trans, fs_info,
4727 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004728 if (ret && !err)
4729 err = ret;
4730 }
4731 }
Yan, Zheng80825102009-11-12 09:35:36 +00004732 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004733}
4734
Chris Masona52d9a82007-08-27 16:49:44 -04004735/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304736 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004737 * @inode - inode that we're zeroing
4738 * @from - the offset to start zeroing
4739 * @len - the length to zero, 0 to zero the entire range respective to the
4740 * offset
4741 * @front - zero up to the offset instead of from the offset on
4742 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304743 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004744 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004745 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304746int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004747 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004748{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004749 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004750 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004751 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4752 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004753 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004754 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004755 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004756 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004757 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304758 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004759 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004760 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004761 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304762 u64 block_start;
4763 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004764
Josef Bacik2aaa6652012-08-29 14:27:18 -04004765 if ((offset & (blocksize - 1)) == 0 &&
4766 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004767 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304768
Josef Bacik8b62f872017-10-19 14:15:55 -04004769 block_start = round_down(from, blocksize);
4770 block_end = block_start + blocksize - 1;
4771
Qu Wenruo364ecf32017-02-27 15:10:38 +08004772 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004773 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004774 if (ret)
4775 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004776
Chris Mason211c17f2008-05-15 09:13:45 -04004777again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004778 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004779 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004780 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004781 block_start, blocksize);
4782 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004783 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004784 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004785 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004786
Chris Masona52d9a82007-08-27 16:49:44 -04004787 if (!PageUptodate(page)) {
4788 ret = btrfs_readpage(NULL, page);
4789 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004790 if (page->mapping != mapping) {
4791 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004792 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004793 goto again;
4794 }
Chris Masona52d9a82007-08-27 16:49:44 -04004795 if (!PageUptodate(page)) {
4796 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004797 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004798 }
4799 }
Chris Mason211c17f2008-05-15 09:13:45 -04004800 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004801
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304802 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004803 set_page_extent_mapped(page);
4804
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304805 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004806 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304807 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004808 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004809 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004810 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004811 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004812 btrfs_put_ordered_extent(ordered);
4813 goto again;
4814 }
4815
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304816 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004817 EXTENT_DIRTY | EXTENT_DELALLOC |
4818 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004819 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004820
Filipe Mananae3b8a482017-11-04 00:16:59 +00004821 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004822 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004823 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304824 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004825 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004826 goto out_unlock;
4827 }
4828
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304829 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004830 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304831 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004832 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004833 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304834 memset(kaddr + (block_start - page_offset(page)),
4835 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004836 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304837 memset(kaddr + (block_start - page_offset(page)) + offset,
4838 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004839 flush_dcache_page(page);
4840 kunmap(page);
4841 }
Chris Mason247e7432008-07-17 12:53:51 -04004842 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004843 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304844 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004845 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004846
Chris Mason89642222008-07-24 09:41:53 -04004847out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004848 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004849 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304850 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004851 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004852 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004853 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004854out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004855 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004856 return ret;
4857}
4858
Josef Bacik16e75492013-10-22 12:18:51 -04004859static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4860 u64 offset, u64 len)
4861{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004862 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004863 struct btrfs_trans_handle *trans;
4864 int ret;
4865
4866 /*
4867 * Still need to make sure the inode looks like it's been updated so
4868 * that any holes get logged if we fsync.
4869 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004870 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4871 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004872 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4873 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4874 return 0;
4875 }
4876
4877 /*
4878 * 1 - for the one we're dropping
4879 * 1 - for the one we're adding
4880 * 1 - for updating the inode.
4881 */
4882 trans = btrfs_start_transaction(root, 3);
4883 if (IS_ERR(trans))
4884 return PTR_ERR(trans);
4885
4886 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4887 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004888 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004889 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004890 return ret;
4891 }
4892
David Sterbaf85b7372017-01-20 14:54:07 +01004893 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4894 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004895 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004896 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004897 else
4898 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004899 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004900 return ret;
4901}
4902
Josef Bacik695a0d02011-03-04 15:46:53 -05004903/*
4904 * This function puts in dummy file extents for the area we're creating a hole
4905 * for. So if we are truncating this file to a larger size we need to insert
4906 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4907 * the range between oldsize and size
4908 */
Josef Bacika41ad392011-01-31 15:30:16 -05004909int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004910{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004911 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004912 struct btrfs_root *root = BTRFS_I(inode)->root;
4913 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004914 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004915 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004916 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004917 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4918 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004919 u64 last_byte;
4920 u64 cur_offset;
4921 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004922 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004923
Josef Bacika71754f2013-06-17 17:14:39 -04004924 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304925 * If our size started in the middle of a block we need to zero out the
4926 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004927 * expose stale data.
4928 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304929 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004930 if (err)
4931 return err;
4932
Yan Zheng9036c102008-10-30 14:19:41 -04004933 if (size <= hole_start)
4934 return 0;
4935
Yan Zheng9036c102008-10-30 14:19:41 -04004936 while (1) {
4937 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004938
David Sterbaff13db42015-12-03 14:30:40 +01004939 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004940 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004941 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004942 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004943 if (!ordered)
4944 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004945 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4946 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004947 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004948 btrfs_put_ordered_extent(ordered);
4949 }
4950
Yan Zheng9036c102008-10-30 14:19:41 -04004951 cur_offset = hole_start;
4952 while (1) {
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02004953 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004954 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004955 if (IS_ERR(em)) {
4956 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004957 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004958 break;
4959 }
Yan Zheng9036c102008-10-30 14:19:41 -04004960 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004961 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004962 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004963 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004964 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004965
Josef Bacik16e75492013-10-22 12:18:51 -04004966 err = maybe_insert_hole(root, inode, cur_offset,
4967 hole_size);
4968 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004969 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004970 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004971 cur_offset + hole_size - 1, 0);
4972 hole_em = alloc_extent_map();
4973 if (!hole_em) {
4974 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4975 &BTRFS_I(inode)->runtime_flags);
4976 goto next;
4977 }
4978 hole_em->start = cur_offset;
4979 hole_em->len = hole_size;
4980 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004981
Josef Bacik5dc562c2012-08-17 13:14:17 -04004982 hole_em->block_start = EXTENT_MAP_HOLE;
4983 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004984 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004985 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004986 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004987 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004988 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004989
4990 while (1) {
4991 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004992 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004993 write_unlock(&em_tree->lock);
4994 if (err != -EEXIST)
4995 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004996 btrfs_drop_extent_cache(BTRFS_I(inode),
4997 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004998 cur_offset +
4999 hole_size - 1, 0);
5000 }
5001 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005002 }
Josef Bacik16e75492013-10-22 12:18:51 -04005003next:
Yan Zheng9036c102008-10-30 14:19:41 -04005004 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005005 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005006 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005007 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005008 break;
5009 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005010 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005011 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
5012 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04005013 return err;
5014}
5015
Eric Sandeen3972f262013-01-12 02:57:22 +00005016static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005017{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005018 struct btrfs_root *root = BTRFS_I(inode)->root;
5019 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005020 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005021 loff_t newsize = attr->ia_size;
5022 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005023 int ret;
5024
Eric Sandeen3972f262013-01-12 02:57:22 +00005025 /*
5026 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5027 * special case where we need to update the times despite not having
5028 * these flags set. For all other operations the VFS set these flags
5029 * explicitly if it wants a timestamp update.
5030 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005031 if (newsize != oldsize) {
5032 inode_inc_iversion(inode);
5033 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5034 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005035 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005036 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005037
Josef Bacika41ad392011-01-31 15:30:16 -05005038 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005039 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005040 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005041 * This is to ensure the snapshot captures a fully consistent
5042 * state of this file - if the snapshot captures this expanding
5043 * truncation, it must capture all writes that happened before
5044 * this truncation.
5045 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005046 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005047 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005048 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005049 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005050 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005051 }
Yan, Zheng80825102009-11-12 09:35:36 +00005052
Miao Xief4a2f4c2011-12-14 20:12:01 -05005053 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005054 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005055 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005056 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005057 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005058
5059 i_size_write(inode, newsize);
5060 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305061 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005062 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005063 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005064 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005065 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005066
Josef Bacika41ad392011-01-31 15:30:16 -05005067 /*
5068 * We're truncating a file that used to have good data down to
5069 * zero. Make sure it gets into the ordered flush list so that
5070 * any new writes get down to disk quickly.
5071 */
5072 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005073 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5074 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005075
Josef Bacikf3fe8202013-01-07 17:03:21 -05005076 /*
5077 * 1 for the orphan item we're going to add
5078 * 1 for the orphan item deletion.
5079 */
5080 trans = btrfs_start_transaction(root, 2);
5081 if (IS_ERR(trans))
5082 return PTR_ERR(trans);
5083
5084 /*
5085 * We need to do this in case we fail at _any_ point during the
5086 * actual truncate. Once we do the truncate_setsize we could
5087 * invalidate pages which forces any outstanding ordered io to
5088 * be instantly completed which will give us extents that need
5089 * to be truncated. If we fail to get an orphan inode down we
5090 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005091 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005092 * will be consistent.
5093 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005094 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005095 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005096 if (ret)
5097 return ret;
5098
Josef Bacika41ad392011-01-31 15:30:16 -05005099 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5100 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005101
5102 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005103 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005104 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005105 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005106
Josef Bacika41ad392011-01-31 15:30:16 -05005107 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005108 if (ret && inode->i_nlink) {
5109 int err;
5110
Liu Bo19fd2df2016-12-01 13:01:02 -08005111 /* To get a stable disk_i_size */
5112 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5113 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005114 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005115 return err;
5116 }
5117
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005118 /*
5119 * failed to truncate, disk_i_size is only adjusted down
5120 * as we remove extents, so it should represent the true
5121 * size of the inode, so reset the in memory size and
5122 * delete our orphan entry.
5123 */
5124 trans = btrfs_join_transaction(root);
5125 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005126 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005127 return ret;
5128 }
5129 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005130 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005131 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005132 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005133 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005134 }
Yan, Zheng80825102009-11-12 09:35:36 +00005135 }
5136
Josef Bacika41ad392011-01-31 15:30:16 -05005137 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005138}
5139
Chris Mason39279cc2007-06-12 06:35:45 -04005140static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5141{
David Howells2b0143b2015-03-17 22:25:59 +00005142 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005143 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005144 int err;
5145
Li Zefanb83cc962010-12-20 16:04:08 +08005146 if (btrfs_root_readonly(root))
5147 return -EROFS;
5148
Jan Kara31051c82016-05-26 16:55:18 +02005149 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005150 if (err)
5151 return err;
5152
Chris Mason5a3f23d2009-03-31 13:27:11 -04005153 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005154 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005155 if (err)
5156 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005157 }
Yan Zheng9036c102008-10-30 14:19:41 -04005158
Christoph Hellwig10257742010-06-04 11:30:02 +02005159 if (attr->ia_valid) {
5160 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005161 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005162 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005163
Josef Bacik22c44fe2011-11-30 10:45:38 -05005164 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005165 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005166 }
5167
Chris Mason39279cc2007-06-12 06:35:45 -04005168 return err;
5169}
Chris Mason61295eb2008-01-14 16:24:38 -05005170
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005171/*
5172 * While truncating the inode pages during eviction, we get the VFS calling
5173 * btrfs_invalidatepage() against each page of the inode. This is slow because
5174 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5175 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5176 * extent_state structures over and over, wasting lots of time.
5177 *
5178 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5179 * those expensive operations on a per page basis and do only the ordered io
5180 * finishing, while we release here the extent_map and extent_state structures,
5181 * without the excessive merging and splitting.
5182 */
5183static void evict_inode_truncate_pages(struct inode *inode)
5184{
5185 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5186 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5187 struct rb_node *node;
5188
5189 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005190 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005191
5192 write_lock(&map_tree->lock);
5193 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5194 struct extent_map *em;
5195
5196 node = rb_first(&map_tree->map);
5197 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005198 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5199 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005200 remove_extent_mapping(map_tree, em);
5201 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005202 if (need_resched()) {
5203 write_unlock(&map_tree->lock);
5204 cond_resched();
5205 write_lock(&map_tree->lock);
5206 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005207 }
5208 write_unlock(&map_tree->lock);
5209
Filipe Manana6ca07092015-05-26 00:55:42 +01005210 /*
5211 * Keep looping until we have no more ranges in the io tree.
5212 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005213 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5214 * still in progress (unlocked the pages in the bio but did not yet
5215 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005216 * ranges can still be locked and eviction started because before
5217 * submitting those bios, which are executed by a separate task (work
5218 * queue kthread), inode references (inode->i_count) were not taken
5219 * (which would be dropped in the end io callback of each bio).
5220 * Therefore here we effectively end up waiting for those bios and
5221 * anyone else holding locked ranges without having bumped the inode's
5222 * reference count - if we don't do it, when they access the inode's
5223 * io_tree to unlock a range it may be too late, leading to an
5224 * use-after-free issue.
5225 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005226 spin_lock(&io_tree->lock);
5227 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5228 struct extent_state *state;
5229 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005230 u64 start;
5231 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005232
5233 node = rb_first(&io_tree->state);
5234 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005235 start = state->start;
5236 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005237 spin_unlock(&io_tree->lock);
5238
David Sterbaff13db42015-12-03 14:30:40 +01005239 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005240
5241 /*
5242 * If still has DELALLOC flag, the extent didn't reach disk,
5243 * and its reserved space won't be freed by delayed_ref.
5244 * So we need to free its reserved space here.
5245 * (Refer to comment in btrfs_invalidatepage, case 2)
5246 *
5247 * Note, end is the bytenr of last byte, so we need + 1 here.
5248 */
5249 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005250 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005251
Filipe Manana6ca07092015-05-26 00:55:42 +01005252 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005253 EXTENT_LOCKED | EXTENT_DIRTY |
5254 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005255 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005256
Filipe Manana7064dd52014-08-08 02:47:05 +01005257 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005258 spin_lock(&io_tree->lock);
5259 }
5260 spin_unlock(&io_tree->lock);
5261}
5262
Al Virobd555972010-06-07 11:35:40 -04005263void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005264{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005265 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005266 struct btrfs_trans_handle *trans;
5267 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005268 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005269 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005270 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005271 int ret;
5272
liubo1abe9b82011-03-24 11:18:59 +00005273 trace_btrfs_inode_evict(inode);
5274
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005275 if (!root) {
5276 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5277 return;
5278 }
5279
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005280 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005281
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005282 evict_inode_truncate_pages(inode);
5283
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005284 if (inode->i_nlink &&
5285 ((btrfs_root_refs(&root->root_item) != 0 &&
5286 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005287 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005288 goto no_delete;
5289
Chris Mason39279cc2007-06-12 06:35:45 -04005290 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005291 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005292 goto no_delete;
5293 }
Al Virobd555972010-06-07 11:35:40 -04005294 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005295 if (!special_file(inode->i_mode))
5296 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005297
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005298 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005299
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005300 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005301 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005302 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005303 goto no_delete;
5304 }
5305
Yan, Zheng76dda932009-09-21 16:00:26 -04005306 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005307 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5308 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005309 goto no_delete;
5310 }
5311
Nikolay Borisovaa790212017-01-10 20:35:40 +02005312 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005313 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005314 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005315 goto no_delete;
5316 }
5317
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005318 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005319 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005320 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005321 goto no_delete;
5322 }
Josef Bacik4a338542011-08-29 11:01:31 -04005323 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005324 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005325 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005326
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005327 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005328
Josef Bacik4289a662011-08-05 13:22:24 -04005329 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005330 * This is a bit simpler than btrfs_truncate since we've already
5331 * reserved our space for our orphan item in the unlink, so we just
5332 * need to reserve some slack space in case we add bytes and update
5333 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005334 */
Yan, Zheng80825102009-11-12 09:35:36 +00005335 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005336 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5337 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005338
Josef Bacik726c35f2011-09-26 15:46:06 -04005339 /*
5340 * Try and steal from the global reserve since we will
5341 * likely not use this space anyway, we want to try as
5342 * hard as possible to get this to work.
5343 */
5344 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005345 steal_from_global++;
5346 else
5347 steal_from_global = 0;
5348 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005349
Josef Bacik3bce8762015-02-24 12:35:51 -08005350 /*
5351 * steal_from_global == 0: we reserved stuff, hooray!
5352 * steal_from_global == 1: we didn't reserve stuff, boo!
5353 * steal_from_global == 2: we've committed, still not a lot of
5354 * room but maybe we'll have room in the global reserve this
5355 * time.
5356 * steal_from_global == 3: abandon all hope!
5357 */
5358 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005359 btrfs_warn(fs_info,
5360 "Could not get space for a delete, will truncate on mount %d",
5361 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005362 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005363 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005364 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005365 }
5366
Miao Xie0e8c36a2012-12-19 06:59:51 +00005367 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005368 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005369 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005370 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005371 goto no_delete;
5372 }
5373
Josef Bacik3bce8762015-02-24 12:35:51 -08005374 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005375 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005376 * sure there is room to do it, if not we need to commit and try
5377 * again.
5378 */
5379 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005380 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005381 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005382 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005383 else
5384 ret = -ENOSPC;
5385 }
5386
5387 /*
5388 * Couldn't steal from the global reserve, we have too much
5389 * pending stuff built up, commit the transaction and try it
5390 * again.
5391 */
5392 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005393 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005394 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005395 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005396 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005397 goto no_delete;
5398 }
5399 continue;
5400 } else {
5401 steal_from_global = 0;
5402 }
5403
Josef Bacik4289a662011-08-05 13:22:24 -04005404 trans->block_rsv = rsv;
5405
Yan, Zhengd68fc572010-05-16 10:49:58 -04005406 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005407 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005408 break;
5409
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005410 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005411 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005412 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005413 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005414 }
5415
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005416 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005417
Josef Bacik4ef31a42013-08-13 14:10:08 -04005418 /*
5419 * Errors here aren't a big deal, it just means we leave orphan items
5420 * in the tree. They will be cleaned up on the next mount.
5421 */
Yan, Zheng80825102009-11-12 09:35:36 +00005422 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005423 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005424 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005425 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005426 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005427 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005428
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005429 trans->block_rsv = &fs_info->trans_block_rsv;
5430 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005431 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005432 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005433
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005434 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005435 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005436no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005437 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005438 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005439}
5440
5441/*
5442 * this returns the key found in the dir entry in the location pointer.
5443 * If no dir entries were found, location->objectid is 0.
5444 */
5445static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5446 struct btrfs_key *location)
5447{
5448 const char *name = dentry->d_name.name;
5449 int namelen = dentry->d_name.len;
5450 struct btrfs_dir_item *di;
5451 struct btrfs_path *path;
5452 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005453 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005454
5455 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005456 if (!path)
5457 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005458
David Sterbaf85b7372017-01-20 14:54:07 +01005459 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5460 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005461 if (IS_ERR(di))
5462 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005463
David Sterbac7040052011-04-19 18:00:01 +02005464 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005465 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005466
Chris Mason5f39d392007-10-15 16:14:19 -04005467 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005468 if (location->type != BTRFS_INODE_ITEM_KEY &&
5469 location->type != BTRFS_ROOT_ITEM_KEY) {
5470 btrfs_warn(root->fs_info,
5471"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5472 __func__, name, btrfs_ino(BTRFS_I(dir)),
5473 location->objectid, location->type, location->offset);
5474 goto out_err;
5475 }
Chris Mason39279cc2007-06-12 06:35:45 -04005476out:
Chris Mason39279cc2007-06-12 06:35:45 -04005477 btrfs_free_path(path);
5478 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005479out_err:
5480 location->objectid = 0;
5481 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005482}
5483
5484/*
5485 * when we hit a tree root in a directory, the btrfs part of the inode
5486 * needs to be changed to reflect the root directory of the tree root. This
5487 * is kind of like crossing a mount point.
5488 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005489static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005490 struct inode *dir,
5491 struct dentry *dentry,
5492 struct btrfs_key *location,
5493 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005494{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005495 struct btrfs_path *path;
5496 struct btrfs_root *new_root;
5497 struct btrfs_root_ref *ref;
5498 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005499 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005500 int ret;
5501 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005502
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005503 path = btrfs_alloc_path();
5504 if (!path) {
5505 err = -ENOMEM;
5506 goto out;
5507 }
Chris Mason39279cc2007-06-12 06:35:45 -04005508
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005509 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005510 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5511 key.type = BTRFS_ROOT_REF_KEY;
5512 key.offset = location->objectid;
5513
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005514 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005515 if (ret) {
5516 if (ret < 0)
5517 err = ret;
5518 goto out;
5519 }
Chris Mason39279cc2007-06-12 06:35:45 -04005520
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005521 leaf = path->nodes[0];
5522 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005523 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005524 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5525 goto out;
5526
5527 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5528 (unsigned long)(ref + 1),
5529 dentry->d_name.len);
5530 if (ret)
5531 goto out;
5532
David Sterbab3b4aa72011-04-21 01:20:15 +02005533 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005534
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005535 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005536 if (IS_ERR(new_root)) {
5537 err = PTR_ERR(new_root);
5538 goto out;
5539 }
5540
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005541 *sub_root = new_root;
5542 location->objectid = btrfs_root_dirid(&new_root->root_item);
5543 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005544 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005545 err = 0;
5546out:
5547 btrfs_free_path(path);
5548 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005549}
5550
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005551static void inode_tree_add(struct inode *inode)
5552{
5553 struct btrfs_root *root = BTRFS_I(inode)->root;
5554 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005555 struct rb_node **p;
5556 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005557 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005558 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005559
Al Viro1d3382cb2010-10-23 15:19:20 -04005560 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005561 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005562 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005563 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005564 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005565 while (*p) {
5566 parent = *p;
5567 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5568
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005569 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005570 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005571 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005572 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005573 else {
5574 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005575 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005576 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005577 RB_CLEAR_NODE(parent);
5578 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005579 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005580 }
5581 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005582 rb_link_node(new, parent, p);
5583 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005584 spin_unlock(&root->inode_lock);
5585}
5586
5587static void inode_tree_del(struct inode *inode)
5588{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005589 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005590 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005591 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005592
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005593 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005594 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005595 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005596 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005597 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005598 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005599 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005600
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005601 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005602 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005603 spin_lock(&root->inode_lock);
5604 empty = RB_EMPTY_ROOT(&root->inode_tree);
5605 spin_unlock(&root->inode_lock);
5606 if (empty)
5607 btrfs_add_dead_root(root);
5608 }
5609}
5610
Jeff Mahoney143bede2012-03-01 14:56:26 +01005611void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005612{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005613 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005614 struct rb_node *node;
5615 struct rb_node *prev;
5616 struct btrfs_inode *entry;
5617 struct inode *inode;
5618 u64 objectid = 0;
5619
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005620 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005621 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005622
5623 spin_lock(&root->inode_lock);
5624again:
5625 node = root->inode_tree.rb_node;
5626 prev = NULL;
5627 while (node) {
5628 prev = node;
5629 entry = rb_entry(node, struct btrfs_inode, rb_node);
5630
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005631 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005632 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005633 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005634 node = node->rb_right;
5635 else
5636 break;
5637 }
5638 if (!node) {
5639 while (prev) {
5640 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005641 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005642 node = prev;
5643 break;
5644 }
5645 prev = rb_next(prev);
5646 }
5647 }
5648 while (node) {
5649 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005650 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005651 inode = igrab(&entry->vfs_inode);
5652 if (inode) {
5653 spin_unlock(&root->inode_lock);
5654 if (atomic_read(&inode->i_count) > 1)
5655 d_prune_aliases(inode);
5656 /*
Al Viro45321ac2010-06-07 13:43:19 -04005657 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005658 * the inode cache when its usage count
5659 * hits zero.
5660 */
5661 iput(inode);
5662 cond_resched();
5663 spin_lock(&root->inode_lock);
5664 goto again;
5665 }
5666
5667 if (cond_resched_lock(&root->inode_lock))
5668 goto again;
5669
5670 node = rb_next(node);
5671 }
5672 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005673}
5674
Chris Masone02119d2008-09-05 16:13:11 -04005675static int btrfs_init_locked_inode(struct inode *inode, void *p)
5676{
5677 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005678 inode->i_ino = args->location->objectid;
5679 memcpy(&BTRFS_I(inode)->location, args->location,
5680 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005681 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005682 return 0;
5683}
5684
5685static int btrfs_find_actor(struct inode *inode, void *opaque)
5686{
5687 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005688 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005689 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005690}
5691
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005692static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005693 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005694 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005695{
5696 struct inode *inode;
5697 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005698 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005699
Chris Mason90d3e592014-01-09 17:28:00 -08005700 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005701 args.root = root;
5702
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005703 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005704 btrfs_init_locked_inode,
5705 (void *)&args);
5706 return inode;
5707}
5708
Balaji Rao1a54ef82008-07-21 02:01:04 +05305709/* Get an inode object given its location and corresponding root.
5710 * Returns in *is_new if the inode was read from disk
5711 */
5712struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005713 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305714{
5715 struct inode *inode;
5716
Chris Mason90d3e592014-01-09 17:28:00 -08005717 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305718 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005719 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305720
5721 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005722 int ret;
5723
5724 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005725 if (!is_bad_inode(inode)) {
5726 inode_tree_add(inode);
5727 unlock_new_inode(inode);
5728 if (new)
5729 *new = 1;
5730 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005731 unlock_new_inode(inode);
5732 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005733 ASSERT(ret < 0);
5734 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005735 }
5736 }
5737
Balaji Rao1a54ef82008-07-21 02:01:04 +05305738 return inode;
5739}
5740
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005741static struct inode *new_simple_dir(struct super_block *s,
5742 struct btrfs_key *key,
5743 struct btrfs_root *root)
5744{
5745 struct inode *inode = new_inode(s);
5746
5747 if (!inode)
5748 return ERR_PTR(-ENOMEM);
5749
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005750 BTRFS_I(inode)->root = root;
5751 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005752 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005753
5754 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005755 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005756 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005757 inode->i_fop = &simple_dir_operations;
5758 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005759 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305760 inode->i_atime = inode->i_mtime;
5761 inode->i_ctime = inode->i_mtime;
5762 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005763
5764 return inode;
5765}
5766
Chris Mason3de45862008-11-17 21:02:50 -05005767struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005768{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005769 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005770 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005771 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005772 struct btrfs_root *sub_root = root;
5773 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005774 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005775 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005776
5777 if (dentry->d_name.len > BTRFS_NAME_LEN)
5778 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005779
Jeff Layton39e3c952012-11-28 11:30:53 -05005780 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005781 if (ret < 0)
5782 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005783
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005784 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005785 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005786
5787 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005788 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005789 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005790 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005791
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005792 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005793 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005794 &location, &sub_root);
5795 if (ret < 0) {
5796 if (ret != -ENOENT)
5797 inode = ERR_PTR(ret);
5798 else
5799 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5800 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005801 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005802 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005803 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005804
Julia Lawall34d19ba2011-01-24 19:55:19 +00005805 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005806 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005807 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005808 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005809 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005810 if (ret) {
5811 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005812 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005813 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005814 }
5815
Chris Mason3de45862008-11-17 21:02:50 -05005816 return inode;
5817}
5818
Nick Pigginfe15ce42011-01-07 17:49:23 +11005819static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005820{
5821 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005822 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005823
Li Zefan848cce02012-02-21 17:04:28 +08005824 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005825 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005826
Li Zefan848cce02012-02-21 17:04:28 +08005827 if (inode) {
5828 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005829 if (btrfs_root_refs(&root->root_item) == 0)
5830 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005831
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005832 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005833 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005834 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005835 return 0;
5836}
5837
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005838static void btrfs_dentry_release(struct dentry *dentry)
5839{
Daeseok Youn944a4512014-04-14 15:37:02 +09005840 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005841}
5842
Chris Mason3de45862008-11-17 21:02:50 -05005843static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005844 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005845{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005846 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005847
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005848 inode = btrfs_lookup_dentry(dir, dentry);
5849 if (IS_ERR(inode)) {
5850 if (PTR_ERR(inode) == -ENOENT)
5851 inode = NULL;
5852 else
5853 return ERR_CAST(inode);
5854 }
5855
Al Viro41d28bc2014-10-12 22:24:21 -04005856 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005857}
5858
Miao Xie16cdcec2011-04-22 18:12:22 +08005859unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005860 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5861};
5862
Josef Bacik23b5ec72017-07-24 15:14:25 -04005863/*
5864 * All this infrastructure exists because dir_emit can fault, and we are holding
5865 * the tree lock when doing readdir. For now just allocate a buffer and copy
5866 * our information into that, and then dir_emit from the buffer. This is
5867 * similar to what NFS does, only we don't keep the buffer around in pagecache
5868 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5869 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5870 * tree lock.
5871 */
5872static int btrfs_opendir(struct inode *inode, struct file *file)
5873{
5874 struct btrfs_file_private *private;
5875
5876 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5877 if (!private)
5878 return -ENOMEM;
5879 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5880 if (!private->filldir_buf) {
5881 kfree(private);
5882 return -ENOMEM;
5883 }
5884 file->private_data = private;
5885 return 0;
5886}
5887
5888struct dir_entry {
5889 u64 ino;
5890 u64 offset;
5891 unsigned type;
5892 int name_len;
5893};
5894
5895static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5896{
5897 while (entries--) {
5898 struct dir_entry *entry = addr;
5899 char *name = (char *)(entry + 1);
5900
5901 ctx->pos = entry->offset;
5902 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5903 entry->type))
5904 return 1;
5905 addr += sizeof(struct dir_entry) + entry->name_len;
5906 ctx->pos++;
5907 }
5908 return 0;
5909}
5910
Al Viro9cdda8d2013-05-22 16:48:09 -04005911static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005912{
Al Viro9cdda8d2013-05-22 16:48:09 -04005913 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005914 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005915 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005916 struct btrfs_dir_item *di;
5917 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005918 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005919 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005920 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005921 struct list_head ins_list;
5922 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005923 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005924 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005925 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005926 char *name_ptr;
5927 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005928 int entries = 0;
5929 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005930 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005931 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005932
Al Viro9cdda8d2013-05-22 16:48:09 -04005933 if (!dir_emit_dots(file, ctx))
5934 return 0;
5935
David Woodhouse49593bf2008-08-17 17:08:36 +01005936 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005937 if (!path)
5938 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005939
Josef Bacik23b5ec72017-07-24 15:14:25 -04005940 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005941 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005942
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005943 INIT_LIST_HEAD(&ins_list);
5944 INIT_LIST_HEAD(&del_list);
5945 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005946
Josef Bacik23b5ec72017-07-24 15:14:25 -04005947again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005948 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005949 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005950 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005951
Chris Mason39279cc2007-06-12 06:35:45 -04005952 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5953 if (ret < 0)
5954 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005955
5956 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005957 struct dir_entry *entry;
5958
Chris Mason5f39d392007-10-15 16:14:19 -04005959 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005960 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005961 if (slot >= btrfs_header_nritems(leaf)) {
5962 ret = btrfs_next_leaf(root, path);
5963 if (ret < 0)
5964 goto err;
5965 else if (ret > 0)
5966 break;
5967 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005968 }
Chris Mason3de45862008-11-17 21:02:50 -05005969
Chris Mason5f39d392007-10-15 16:14:19 -04005970 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5971
5972 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005973 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005974 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005975 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005976 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005977 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005978 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005979 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005980 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005981 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005982 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5983 PAGE_SIZE) {
5984 btrfs_release_path(path);
5985 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5986 if (ret)
5987 goto nopos;
5988 addr = private->filldir_buf;
5989 entries = 0;
5990 total_len = 0;
5991 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005992 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005993
5994 entry = addr;
5995 entry->name_len = name_len;
5996 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005997 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5998 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005999 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006000 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006001 entry->ino = location.objectid;
6002 entry->offset = found_key.offset;
6003 entries++;
6004 addr += sizeof(struct dir_entry) + name_len;
6005 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006006next:
6007 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006008 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006009 btrfs_release_path(path);
6010
6011 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6012 if (ret)
6013 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006014
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006015 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006016 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006017 goto nopos;
6018
Zach Browndb62efb2013-07-11 16:19:42 -07006019 /*
6020 * Stop new entries from being returned after we return the last
6021 * entry.
6022 *
6023 * New directory entries are assigned a strictly increasing
6024 * offset. This means that new entries created during readdir
6025 * are *guaranteed* to be seen in the future by that readdir.
6026 * This has broken buggy programs which operate on names as
6027 * they're returned by readdir. Until we re-use freed offsets
6028 * we have this hack to stop new entries from being returned
6029 * under the assumption that they'll never reach this huge
6030 * offset.
6031 *
6032 * This is being careful not to overflow 32bit loff_t unless the
6033 * last entry requires it because doing so has broken 32bit apps
6034 * in the past.
6035 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006036 if (ctx->pos >= INT_MAX)
6037 ctx->pos = LLONG_MAX;
6038 else
6039 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006040nopos:
6041 ret = 0;
6042err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006043 if (put)
6044 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006045 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006046 return ret;
6047}
6048
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006049int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006050{
6051 struct btrfs_root *root = BTRFS_I(inode)->root;
6052 struct btrfs_trans_handle *trans;
6053 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006054 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006055
Josef Bacik72ac3c02012-05-23 14:13:11 -04006056 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006057 return 0;
6058
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006059 if (btrfs_fs_closing(root->fs_info) &&
6060 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006061 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006062
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006063 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006064 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006065 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006066 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006067 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006068 if (IS_ERR(trans))
6069 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006070 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006071 }
6072 return ret;
6073}
6074
6075/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006076 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006077 * inode changes. But, it is most likely to find the inode in cache.
6078 * FIXME, needs more benchmarking...there are no reasons other than performance
6079 * to keep or drop this code.
6080 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006081static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006082{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006083 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006084 struct btrfs_root *root = BTRFS_I(inode)->root;
6085 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006086 int ret;
6087
Josef Bacik72ac3c02012-05-23 14:13:11 -04006088 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006089 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006090
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006091 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006092 if (IS_ERR(trans))
6093 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006094
6095 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006096 if (ret && ret == -ENOSPC) {
6097 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006098 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006099 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006100 if (IS_ERR(trans))
6101 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006102
Chris Mason94b60442010-05-26 11:02:00 -04006103 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006104 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006105 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006106 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006107 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006108
6109 return ret;
6110}
6111
6112/*
6113 * This is a copy of file_update_time. We need this so we can return error on
6114 * ENOSPC for updating the inode in the case of file write and mmap writes.
6115 */
Josef Bacike41f9412012-03-26 09:46:47 -04006116static int btrfs_update_time(struct inode *inode, struct timespec *now,
6117 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006118{
Alexander Block2bc5565282012-06-15 09:49:33 +02006119 struct btrfs_root *root = BTRFS_I(inode)->root;
6120
6121 if (btrfs_root_readonly(root))
6122 return -EROFS;
6123
Josef Bacike41f9412012-03-26 09:46:47 -04006124 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006125 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006126 if (flags & S_CTIME)
6127 inode->i_ctime = *now;
6128 if (flags & S_MTIME)
6129 inode->i_mtime = *now;
6130 if (flags & S_ATIME)
6131 inode->i_atime = *now;
6132 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006133}
6134
Chris Masond352ac62008-09-29 15:18:18 -04006135/*
6136 * find the highest existing sequence number in a directory
6137 * and then set the in-memory index_cnt variable to reflect
6138 * free sequence numbers
6139 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006140static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006141{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006142 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006143 struct btrfs_key key, found_key;
6144 struct btrfs_path *path;
6145 struct extent_buffer *leaf;
6146 int ret;
6147
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006148 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006149 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006150 key.offset = (u64)-1;
6151
6152 path = btrfs_alloc_path();
6153 if (!path)
6154 return -ENOMEM;
6155
6156 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6157 if (ret < 0)
6158 goto out;
6159 /* FIXME: we should be able to handle this */
6160 if (ret == 0)
6161 goto out;
6162 ret = 0;
6163
6164 /*
6165 * MAGIC NUMBER EXPLANATION:
6166 * since we search a directory based on f_pos we have to start at 2
6167 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6168 * else has to start at 2
6169 */
6170 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006171 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006172 goto out;
6173 }
6174
6175 path->slots[0]--;
6176
6177 leaf = path->nodes[0];
6178 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6179
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006180 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006181 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006182 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006183 goto out;
6184 }
6185
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006186 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006187out:
6188 btrfs_free_path(path);
6189 return ret;
6190}
6191
Chris Masond352ac62008-09-29 15:18:18 -04006192/*
6193 * helper to find a free sequence number in a given directory. This current
6194 * code is very simple, later versions will do smarter things in the btree
6195 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006196int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006197{
6198 int ret = 0;
6199
Nikolay Borisov877574e2017-02-20 13:50:33 +02006200 if (dir->index_cnt == (u64)-1) {
6201 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006202 if (ret) {
6203 ret = btrfs_set_inode_index_count(dir);
6204 if (ret)
6205 return ret;
6206 }
Josef Bacikaec74772008-07-24 12:12:38 -04006207 }
6208
Nikolay Borisov877574e2017-02-20 13:50:33 +02006209 *index = dir->index_cnt;
6210 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006211
6212 return ret;
6213}
6214
Chris Masonb0d5d102014-09-08 13:08:51 -07006215static int btrfs_insert_inode_locked(struct inode *inode)
6216{
6217 struct btrfs_iget_args args;
6218 args.location = &BTRFS_I(inode)->location;
6219 args.root = BTRFS_I(inode)->root;
6220
6221 return insert_inode_locked4(inode,
6222 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6223 btrfs_find_actor, &args);
6224}
6225
Anand Jain19aee8d2017-07-18 17:37:05 +08006226/*
6227 * Inherit flags from the parent inode.
6228 *
6229 * Currently only the compression flags and the cow flags are inherited.
6230 */
6231static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6232{
6233 unsigned int flags;
6234
6235 if (!dir)
6236 return;
6237
6238 flags = BTRFS_I(dir)->flags;
6239
6240 if (flags & BTRFS_INODE_NOCOMPRESS) {
6241 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6242 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6243 } else if (flags & BTRFS_INODE_COMPRESS) {
6244 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6245 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6246 }
6247
6248 if (flags & BTRFS_INODE_NODATACOW) {
6249 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6250 if (S_ISREG(inode->i_mode))
6251 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6252 }
6253
6254 btrfs_update_iflags(inode);
6255}
6256
Chris Mason39279cc2007-06-12 06:35:45 -04006257static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6258 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006259 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006260 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006261 u64 ref_objectid, u64 objectid,
6262 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006263{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006264 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006265 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006266 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006267 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006268 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006269 struct btrfs_inode_ref *ref;
6270 struct btrfs_key key[2];
6271 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006272 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006273 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006274 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006275
Chris Mason5f39d392007-10-15 16:14:19 -04006276 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006277 if (!path)
6278 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006279
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006280 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006281 if (!inode) {
6282 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006283 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006284 }
Chris Mason39279cc2007-06-12 06:35:45 -04006285
Li Zefan581bb052011-04-20 10:06:11 +08006286 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006287 * O_TMPFILE, set link count to 0, so that after this point,
6288 * we fill in an inode item with the correct link count.
6289 */
6290 if (!name)
6291 set_nlink(inode, 0);
6292
6293 /*
Li Zefan581bb052011-04-20 10:06:11 +08006294 * we have to initialize this early, so we can reclaim the inode
6295 * number if we fail afterwards in this function.
6296 */
6297 inode->i_ino = objectid;
6298
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006299 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006300 trace_btrfs_inode_request(dir);
6301
Nikolay Borisov877574e2017-02-20 13:50:33 +02006302 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006303 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006304 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006305 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006306 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006307 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006308 } else if (dir) {
6309 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006310 }
6311 /*
6312 * index_cnt is ignored for everything but a dir,
6313 * btrfs_get_inode_index_count has an explanation for the magic
6314 * number
6315 */
6316 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006317 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006318 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006319 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006320 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006321
Josef Bacik5dc562c2012-08-17 13:14:17 -04006322 /*
6323 * We could have gotten an inode number from somebody who was fsynced
6324 * and then removed in this same transaction, so let's just set full
6325 * sync since it will be a full sync anyway and this will blow away the
6326 * old info in the log.
6327 */
6328 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6329
Chris Mason9c583092008-01-29 15:15:18 -05006330 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006331 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006332 key[0].offset = 0;
6333
Chris Mason9c583092008-01-29 15:15:18 -05006334 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006335
6336 if (name) {
6337 /*
6338 * Start new inodes with an inode_ref. This is slightly more
6339 * efficient for small numbers of hard links since they will
6340 * be packed into one item. Extended refs will kick in if we
6341 * add more hard links than can fit in the ref item.
6342 */
6343 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006344 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006345 key[1].offset = ref_objectid;
6346
6347 sizes[1] = name_len + sizeof(*ref);
6348 }
Chris Mason9c583092008-01-29 15:15:18 -05006349
Chris Masonb0d5d102014-09-08 13:08:51 -07006350 location = &BTRFS_I(inode)->location;
6351 location->objectid = objectid;
6352 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006353 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006354
6355 ret = btrfs_insert_inode_locked(inode);
6356 if (ret < 0)
6357 goto fail;
6358
Chris Masonb9473432009-03-13 11:00:37 -04006359 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006360 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006361 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006362 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006363
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006364 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006365 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306366
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006367 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306368 inode->i_atime = inode->i_mtime;
6369 inode->i_ctime = inode->i_mtime;
6370 BTRFS_I(inode)->i_otime = inode->i_mtime;
6371
Chris Mason5f39d392007-10-15 16:14:19 -04006372 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6373 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006374 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006375 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006376 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006377
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006378 if (name) {
6379 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6380 struct btrfs_inode_ref);
6381 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6382 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6383 ptr = (unsigned long)(ref + 1);
6384 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6385 }
Chris Mason9c583092008-01-29 15:15:18 -05006386
Chris Mason5f39d392007-10-15 16:14:19 -04006387 btrfs_mark_buffer_dirty(path->nodes[0]);
6388 btrfs_free_path(path);
6389
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006390 btrfs_inherit_iflags(inode, dir);
6391
Al Viro569254b2011-07-24 17:08:40 -04006392 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006393 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006394 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006395 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006396 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6397 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006398 }
6399
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006400 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006401
6402 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006403 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006404
Alexander Block8ea05e32012-07-25 17:35:53 +02006405 btrfs_update_root_times(trans, root);
6406
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006407 ret = btrfs_inode_inherit_props(trans, inode, dir);
6408 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006409 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006410 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006411 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006412
Chris Mason39279cc2007-06-12 06:35:45 -04006413 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006414
6415fail_unlock:
6416 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006417fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006418 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006419 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006420 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006421 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006422 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006423}
6424
6425static inline u8 btrfs_inode_type(struct inode *inode)
6426{
6427 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6428}
6429
Chris Masond352ac62008-09-29 15:18:18 -04006430/*
6431 * utility function to add 'inode' into 'parent_inode' with
6432 * a give name and a given sequence number.
6433 * if 'add_backref' is true, also insert a backref from the
6434 * inode to the parent directory.
6435 */
Chris Masone02119d2008-09-05 16:13:11 -04006436int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006437 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006438 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006439{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006440 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006441 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006442 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006443 struct btrfs_root *root = parent_inode->root;
6444 u64 ino = btrfs_ino(inode);
6445 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006446
Li Zefan33345d012011-04-20 10:31:50 +08006447 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006448 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006449 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006450 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006451 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006452 key.offset = 0;
6453 }
Chris Mason39279cc2007-06-12 06:35:45 -04006454
Li Zefan33345d012011-04-20 10:31:50 +08006455 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006456 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6457 root->root_key.objectid, parent_ino,
6458 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006459 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006460 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6461 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006462 }
6463
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006464 /* Nothing to clean up yet */
6465 if (ret)
6466 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006467
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006468 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6469 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006470 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006471 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006472 goto fail_dir_item;
6473 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006474 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006475 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006476 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006477
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006478 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006479 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006480 inode_inc_iversion(&parent_inode->vfs_inode);
6481 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6482 current_time(&parent_inode->vfs_inode);
6483 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006484 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006485 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006486 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006487
6488fail_dir_item:
6489 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6490 u64 local_index;
6491 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006492 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6493 root->root_key.objectid, parent_ino,
6494 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006495
6496 } else if (add_backref) {
6497 u64 local_index;
6498 int err;
6499
6500 err = btrfs_del_inode_ref(trans, root, name, name_len,
6501 ino, parent_ino, &local_index);
6502 }
6503 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006504}
6505
6506static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006507 struct btrfs_inode *dir, struct dentry *dentry,
6508 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006509{
Josef Bacika1b075d2010-11-19 20:36:11 +00006510 int err = btrfs_add_link(trans, dir, inode,
6511 dentry->d_name.name, dentry->d_name.len,
6512 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006513 if (err > 0)
6514 err = -EEXIST;
6515 return err;
6516}
6517
Josef Bacik618e21d2007-07-11 10:18:17 -04006518static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006519 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006520{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006521 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006522 struct btrfs_trans_handle *trans;
6523 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006524 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006525 int err;
6526 int drop_inode = 0;
6527 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006528 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006529
Josef Bacik9ed74f22009-09-11 16:12:44 -04006530 /*
6531 * 2 for inode item and ref
6532 * 2 for dir items
6533 * 1 for xattr if selinux is on
6534 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006535 trans = btrfs_start_transaction(root, 5);
6536 if (IS_ERR(trans))
6537 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006538
Li Zefan581bb052011-04-20 10:06:11 +08006539 err = btrfs_find_free_ino(root, &objectid);
6540 if (err)
6541 goto out_unlock;
6542
Josef Bacikaec74772008-07-24 12:12:38 -04006543 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006544 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6545 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006546 if (IS_ERR(inode)) {
6547 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006548 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006549 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006550
Casey Schauflerad19db72011-12-15 10:09:07 -05006551 /*
6552 * If the active LSM wants to access the inode during
6553 * d_instantiate it needs these. Smack checks to see
6554 * if the filesystem supports xattrs by looking at the
6555 * ops vector.
6556 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006557 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006558 init_special_inode(inode, inode->i_mode, rdev);
6559
6560 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006561 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006562 goto out_unlock_inode;
6563
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006564 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6565 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006566 if (err) {
6567 goto out_unlock_inode;
6568 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006569 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006570 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006571 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006572 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006573
Josef Bacik618e21d2007-07-11 10:18:17 -04006574out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006575 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006576 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006577 if (drop_inode) {
6578 inode_dec_link_count(inode);
6579 iput(inode);
6580 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006581 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006582
6583out_unlock_inode:
6584 drop_inode = 1;
6585 unlock_new_inode(inode);
6586 goto out_unlock;
6587
Josef Bacik618e21d2007-07-11 10:18:17 -04006588}
6589
Chris Mason39279cc2007-06-12 06:35:45 -04006590static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006591 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006592{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006593 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006594 struct btrfs_trans_handle *trans;
6595 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006596 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006597 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006598 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006599 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006600 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006601
Josef Bacik9ed74f22009-09-11 16:12:44 -04006602 /*
6603 * 2 for inode item and ref
6604 * 2 for dir items
6605 * 1 for xattr if selinux is on
6606 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006607 trans = btrfs_start_transaction(root, 5);
6608 if (IS_ERR(trans))
6609 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006610
Li Zefan581bb052011-04-20 10:06:11 +08006611 err = btrfs_find_free_ino(root, &objectid);
6612 if (err)
6613 goto out_unlock;
6614
Josef Bacikaec74772008-07-24 12:12:38 -04006615 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006616 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6617 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006618 if (IS_ERR(inode)) {
6619 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006620 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006621 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006622 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006623 /*
6624 * If the active LSM wants to access the inode during
6625 * d_instantiate it needs these. Smack checks to see
6626 * if the filesystem supports xattrs by looking at the
6627 * ops vector.
6628 */
6629 inode->i_fop = &btrfs_file_operations;
6630 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006631 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006632
6633 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6634 if (err)
6635 goto out_unlock_inode;
6636
6637 err = btrfs_update_inode(trans, root, inode);
6638 if (err)
6639 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006640
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006641 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6642 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006643 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006644 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006645
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006646 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006647 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006648 d_instantiate(dentry, inode);
6649
Chris Mason39279cc2007-06-12 06:35:45 -04006650out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006651 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006652 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006653 inode_dec_link_count(inode);
6654 iput(inode);
6655 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006656 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006657 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006658
6659out_unlock_inode:
6660 unlock_new_inode(inode);
6661 goto out_unlock;
6662
Chris Mason39279cc2007-06-12 06:35:45 -04006663}
6664
6665static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6666 struct dentry *dentry)
6667{
Filipe Manana271dba42016-01-05 16:24:05 +00006668 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006669 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006670 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006671 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006672 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006673 int err;
6674 int drop_inode = 0;
6675
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006676 /* do not allow sys_link's with other subvols of the same device */
6677 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006678 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006679
Mark Fashehf1863732012-08-08 11:32:27 -07006680 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006681 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006682
Nikolay Borisov877574e2017-02-20 13:50:33 +02006683 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006684 if (err)
6685 goto fail;
6686
Yan, Zhenga22285a2010-05-16 10:48:46 -04006687 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006688 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006689 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006690 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006691 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006692 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006693 if (IS_ERR(trans)) {
6694 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006695 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006696 goto fail;
6697 }
Chris Mason5f39d392007-10-15 16:14:19 -04006698
Miao Xie67de1172013-12-26 13:07:06 +08006699 /* There are several dir indexes for this inode, clear the cache. */
6700 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006701 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006702 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006703 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006704 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006705 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006706
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006707 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6708 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006709
Yan, Zhenga5719522009-09-24 09:17:31 -04006710 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006711 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006712 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006713 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006714 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006715 if (err)
6716 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006717 if (inode->i_nlink == 1) {
6718 /*
6719 * If new hard link count is 1, it's a file created
6720 * with open(2) O_TMPFILE flag.
6721 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006722 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006723 if (err)
6724 goto fail;
6725 }
Al Viro08c422c2011-12-23 07:58:13 -05006726 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006727 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006728 }
Chris Mason39279cc2007-06-12 06:35:45 -04006729
Chris Mason1832a6d2007-12-21 16:27:21 -05006730fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006731 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006732 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006733 if (drop_inode) {
6734 inode_dec_link_count(inode);
6735 iput(inode);
6736 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006737 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006738 return err;
6739}
6740
Al Viro18bb1db2011-07-26 01:41:39 -04006741static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006742{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006743 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006744 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006745 struct btrfs_trans_handle *trans;
6746 struct btrfs_root *root = BTRFS_I(dir)->root;
6747 int err = 0;
6748 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006749 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006750 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006751
Josef Bacik9ed74f22009-09-11 16:12:44 -04006752 /*
6753 * 2 items for inode and ref
6754 * 2 items for dir items
6755 * 1 for xattr if selinux is on
6756 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006757 trans = btrfs_start_transaction(root, 5);
6758 if (IS_ERR(trans))
6759 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006760
Li Zefan581bb052011-04-20 10:06:11 +08006761 err = btrfs_find_free_ino(root, &objectid);
6762 if (err)
6763 goto out_fail;
6764
Josef Bacikaec74772008-07-24 12:12:38 -04006765 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006766 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6767 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006768 if (IS_ERR(inode)) {
6769 err = PTR_ERR(inode);
6770 goto out_fail;
6771 }
Chris Mason5f39d392007-10-15 16:14:19 -04006772
Chris Mason39279cc2007-06-12 06:35:45 -04006773 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006774 /* these must be set before we unlock the inode */
6775 inode->i_op = &btrfs_dir_inode_operations;
6776 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006777
Eric Paris2a7dba32011-02-01 11:05:39 -05006778 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006779 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006780 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006781
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006782 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006783 err = btrfs_update_inode(trans, root, inode);
6784 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006785 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006786
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006787 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6788 dentry->d_name.name,
6789 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006790 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006791 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006792
Chris Mason39279cc2007-06-12 06:35:45 -04006793 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006794 /*
6795 * mkdir is special. We're unlocking after we call d_instantiate
6796 * to avoid a race with nfsd calling d_instantiate.
6797 */
6798 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006799 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006800
6801out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006802 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006803 if (drop_on_err) {
6804 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006805 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006806 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006807 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006808 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006809
6810out_fail_inode:
6811 unlock_new_inode(inode);
6812 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006813}
6814
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006815/* Find next extent map of a given extent map, caller needs to ensure locks */
6816static struct extent_map *next_extent_map(struct extent_map *em)
6817{
6818 struct rb_node *next;
6819
6820 next = rb_next(&em->rb_node);
6821 if (!next)
6822 return NULL;
6823 return container_of(next, struct extent_map, rb_node);
6824}
6825
6826static struct extent_map *prev_extent_map(struct extent_map *em)
6827{
6828 struct rb_node *prev;
6829
6830 prev = rb_prev(&em->rb_node);
6831 if (!prev)
6832 return NULL;
6833 return container_of(prev, struct extent_map, rb_node);
6834}
6835
Chris Masond352ac62008-09-29 15:18:18 -04006836/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006837 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006838 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006839 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006840 */
Chris Mason3b951512008-04-17 11:29:12 -04006841static int merge_extent_mapping(struct extent_map_tree *em_tree,
6842 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006843 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006844 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006845{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006846 struct extent_map *prev;
6847 struct extent_map *next;
6848 u64 start;
6849 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006850 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006851
Chris Masone6dcd2d2008-07-17 12:53:50 -04006852 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006853
6854 if (existing->start > map_start) {
6855 next = existing;
6856 prev = prev_extent_map(next);
6857 } else {
6858 prev = existing;
6859 next = next_extent_map(prev);
6860 }
6861
6862 start = prev ? extent_map_end(prev) : em->start;
6863 start = max_t(u64, start, em->start);
6864 end = next ? next->start : extent_map_end(em);
6865 end = min_t(u64, end, extent_map_end(em));
6866 start_diff = start - em->start;
6867 em->start = start;
6868 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006869 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6870 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006871 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006872 em->block_len -= start_diff;
6873 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006874 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006875}
6876
Chris Masonc8b97812008-10-29 14:49:59 -04006877static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006878 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006879 size_t pg_offset, u64 extent_offset,
6880 struct btrfs_file_extent_item *item)
6881{
6882 int ret;
6883 struct extent_buffer *leaf = path->nodes[0];
6884 char *tmp;
6885 size_t max_size;
6886 unsigned long inline_size;
6887 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006888 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006889
6890 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006891 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006892 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6893 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006894 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006895 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006896 if (!tmp)
6897 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006898 ptr = btrfs_file_extent_inline_start(item);
6899
6900 read_extent_buffer(leaf, tmp, ptr, inline_size);
6901
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006902 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006903 ret = btrfs_decompress(compress_type, tmp, page,
6904 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006905
6906 /*
6907 * decompression code contains a memset to fill in any space between the end
6908 * of the uncompressed data and the end of max_size in case the decompressed
6909 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6910 * the end of an inline extent and the beginning of the next block, so we
6911 * cover that region here.
6912 */
6913
6914 if (max_size + pg_offset < PAGE_SIZE) {
6915 char *map = kmap(page);
6916 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6917 kunmap(page);
6918 }
Chris Masonc8b97812008-10-29 14:49:59 -04006919 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006920 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006921}
6922
Chris Masond352ac62008-09-29 15:18:18 -04006923/*
6924 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006925 * the ugly parts come from merging extents from the disk with the in-ram
6926 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006927 * where the in-ram extents might be locked pending data=ordered completion.
6928 *
6929 * This also copies inline extents directly into the page.
6930 */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006931struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6932 struct page *page,
6933 size_t pg_offset, u64 start, u64 len,
6934 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006935{
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006936 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006937 int ret;
6938 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006939 u64 extent_start = 0;
6940 u64 extent_end = 0;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006941 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006942 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006943 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006944 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006945 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006946 struct extent_buffer *leaf;
6947 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006948 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006949 struct extent_map_tree *em_tree = &inode->extent_tree;
6950 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006951 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006952
Chris Mason890871b2009-09-02 16:24:52 -04006953 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006954 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006955 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006956 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006957 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006958
Chris Masona52d9a82007-08-27 16:49:44 -04006959 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006960 if (em->start > start || em->start + em->len <= start)
6961 free_extent_map(em);
6962 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006963 free_extent_map(em);
6964 else
6965 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006966 }
David Sterba172ddd62011-04-21 00:48:27 +02006967 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006968 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006969 err = -ENOMEM;
6970 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006971 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006972 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006973 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006974 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006975 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006976 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006977
6978 if (!path) {
6979 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006980 if (!path) {
6981 err = -ENOMEM;
6982 goto out;
6983 }
6984 /*
6985 * Chances are we'll be called again, so go ahead and do
6986 * readahead
6987 */
David Sterbae4058b52015-11-27 16:31:35 +01006988 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006989 }
6990
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006991 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006992 if (ret < 0) {
6993 err = ret;
6994 goto out;
6995 }
6996
6997 if (ret != 0) {
6998 if (path->slots[0] == 0)
6999 goto not_found;
7000 path->slots[0]--;
7001 }
7002
Chris Mason5f39d392007-10-15 16:14:19 -04007003 leaf = path->nodes[0];
7004 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007005 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04007006 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04007007 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02007008 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04007009 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04007010 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007011 /*
7012 * If we backup past the first extent we want to move forward
7013 * and see if there is an extent in front of us, otherwise we'll
7014 * say there is a hole for our whole search range which can
7015 * cause problems.
7016 */
7017 extent_end = start;
7018 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007019 }
7020
Chris Mason5f39d392007-10-15 16:14:19 -04007021 found_type = btrfs_file_extent_type(leaf, item);
7022 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007023 if (found_type == BTRFS_FILE_EXTENT_REG ||
7024 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007025 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007026 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007027
7028 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7029 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007030 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7031 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007032 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007033 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007034 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007035
7036 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7037 path->slots[0],
7038 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007039 }
Josef Bacik25a50342013-10-14 12:08:38 -04007040next:
Yan Zheng9036c102008-10-30 14:19:41 -04007041 if (start >= extent_end) {
7042 path->slots[0]++;
7043 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7044 ret = btrfs_next_leaf(root, path);
7045 if (ret < 0) {
7046 err = ret;
7047 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007048 }
Yan Zheng9036c102008-10-30 14:19:41 -04007049 if (ret > 0)
7050 goto not_found;
7051 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007052 }
Yan Zheng9036c102008-10-30 14:19:41 -04007053 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7054 if (found_key.objectid != objectid ||
7055 found_key.type != BTRFS_EXTENT_DATA_KEY)
7056 goto not_found;
7057 if (start + len <= found_key.offset)
7058 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007059 if (start > found_key.offset)
7060 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007061 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007062 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007063 em->len = found_key.offset - start;
7064 goto not_found_em;
7065 }
7066
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007067 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007068 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007069
Yan Zhengd899e052008-10-30 14:25:28 -04007070 if (found_type == BTRFS_FILE_EXTENT_REG ||
7071 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007072 goto insert;
7073 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007074 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007075 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007076 size_t size;
7077 size_t extent_offset;
7078 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007079
Filipe Manana7ffbb592014-06-09 03:48:05 +01007080 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007081 goto out;
Yan689f9342007-10-29 11:41:07 -04007082
Chris Mason514ac8a2014-01-03 21:07:00 -08007083 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007084 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007085 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7086 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007087 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007088 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007089 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007090 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007091 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007092 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007093 if (btrfs_file_extent_compression(leaf, item) !=
7094 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007095 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007096 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007097 if (ret) {
7098 err = ret;
7099 goto out;
7100 }
Chris Masonc8b97812008-10-29 14:49:59 -04007101 } else {
7102 map = kmap(page);
7103 read_extent_buffer(leaf, map + pg_offset, ptr,
7104 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007105 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007106 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007107 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007108 copy_size);
7109 }
Chris Masonc8b97812008-10-29 14:49:59 -04007110 kunmap(page);
7111 }
Chris Mason179e29e2007-11-01 11:28:41 -04007112 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007113 }
Chris Masond1310b22008-01-24 16:13:08 -05007114 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007115 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007116 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007117 }
7118not_found:
7119 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007120 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007121 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007122not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007123 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007124insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007125 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007126 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007127 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007128 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7129 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007130 err = -EIO;
7131 goto out;
7132 }
Chris Masond1310b22008-01-24 16:13:08 -05007133
7134 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007135 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007136 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007137 /* it is possible that someone inserted the extent into the tree
7138 * while we had the lock dropped. It is also possible that
7139 * an overlapping map exists in the tree
7140 */
Chris Masona52d9a82007-08-27 16:49:44 -04007141 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007142 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007143
7144 ret = 0;
7145
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007146 existing = search_extent_mapping(em_tree, start, len);
7147 /*
7148 * existing will always be non-NULL, since there must be
7149 * extent causing the -EEXIST.
7150 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007151 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007152 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007153 em->block_start == existing->block_start) {
7154 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007155 * The existing extent map already encompasses the
7156 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007157 */
7158 free_extent_map(em);
7159 em = existing;
7160 err = 0;
7161
7162 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007163 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007164 /*
7165 * The existing extent map is the one nearest to
7166 * the [start, start + len) range which overlaps
7167 */
7168 err = merge_extent_mapping(em_tree, existing,
7169 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007170 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007171 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007172 free_extent_map(em);
7173 em = NULL;
7174 }
7175 } else {
7176 free_extent_map(em);
7177 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007178 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007179 }
Chris Masona52d9a82007-08-27 16:49:44 -04007180 }
Chris Mason890871b2009-09-02 16:24:52 -04007181 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007182out:
liubo1abe9b82011-03-24 11:18:59 +00007183
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007184 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007185
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007186 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007187 if (err) {
7188 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007189 return ERR_PTR(err);
7190 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007191 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007192 return em;
7193}
7194
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007195struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7196 struct page *page,
7197 size_t pg_offset, u64 start, u64 len,
7198 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007199{
7200 struct extent_map *em;
7201 struct extent_map *hole_em = NULL;
7202 u64 range_start = start;
7203 u64 end;
7204 u64 found;
7205 u64 found_end;
7206 int err = 0;
7207
7208 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7209 if (IS_ERR(em))
7210 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007211 /*
7212 * If our em maps to:
7213 * - a hole or
7214 * - a pre-alloc extent,
7215 * there might actually be delalloc bytes behind it.
7216 */
7217 if (em->block_start != EXTENT_MAP_HOLE &&
7218 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7219 return em;
7220 else
7221 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007222
7223 /* check to see if we've wrapped (len == -1 or similar) */
7224 end = start + len;
7225 if (end < start)
7226 end = (u64)-1;
7227 else
7228 end -= 1;
7229
7230 em = NULL;
7231
7232 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007233 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007234 end, len, EXTENT_DELALLOC, 1);
7235 found_end = range_start + found;
7236 if (found_end < range_start)
7237 found_end = (u64)-1;
7238
7239 /*
7240 * we didn't find anything useful, return
7241 * the original results from get_extent()
7242 */
7243 if (range_start > end || found_end <= start) {
7244 em = hole_em;
7245 hole_em = NULL;
7246 goto out;
7247 }
7248
7249 /* adjust the range_start to make sure it doesn't
7250 * go backwards from the start they passed in
7251 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307252 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007253 found = found_end - range_start;
7254
7255 if (found > 0) {
7256 u64 hole_start = start;
7257 u64 hole_len = len;
7258
David Sterba172ddd62011-04-21 00:48:27 +02007259 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007260 if (!em) {
7261 err = -ENOMEM;
7262 goto out;
7263 }
7264 /*
7265 * when btrfs_get_extent can't find anything it
7266 * returns one huge hole
7267 *
7268 * make sure what it found really fits our range, and
7269 * adjust to make sure it is based on the start from
7270 * the caller
7271 */
7272 if (hole_em) {
7273 u64 calc_end = extent_map_end(hole_em);
7274
7275 if (calc_end <= start || (hole_em->start > end)) {
7276 free_extent_map(hole_em);
7277 hole_em = NULL;
7278 } else {
7279 hole_start = max(hole_em->start, start);
7280 hole_len = calc_end - hole_start;
7281 }
7282 }
7283 em->bdev = NULL;
7284 if (hole_em && range_start > hole_start) {
7285 /* our hole starts before our delalloc, so we
7286 * have to return just the parts of the hole
7287 * that go until the delalloc starts
7288 */
7289 em->len = min(hole_len,
7290 range_start - hole_start);
7291 em->start = hole_start;
7292 em->orig_start = hole_start;
7293 /*
7294 * don't adjust block start at all,
7295 * it is fixed at EXTENT_MAP_HOLE
7296 */
7297 em->block_start = hole_em->block_start;
7298 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007299 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7300 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007301 } else {
7302 em->start = range_start;
7303 em->len = found;
7304 em->orig_start = range_start;
7305 em->block_start = EXTENT_MAP_DELALLOC;
7306 em->block_len = found;
7307 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007308 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007309 return hole_em;
7310 }
7311out:
7312
7313 free_extent_map(hole_em);
7314 if (err) {
7315 free_extent_map(em);
7316 return ERR_PTR(err);
7317 }
7318 return em;
7319}
7320
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007321static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7322 const u64 start,
7323 const u64 len,
7324 const u64 orig_start,
7325 const u64 block_start,
7326 const u64 block_len,
7327 const u64 orig_block_len,
7328 const u64 ram_bytes,
7329 const int type)
7330{
7331 struct extent_map *em = NULL;
7332 int ret;
7333
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007334 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007335 em = create_io_em(inode, start, len, orig_start,
7336 block_start, block_len, orig_block_len,
7337 ram_bytes,
7338 BTRFS_COMPRESS_NONE, /* compress_type */
7339 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007340 if (IS_ERR(em))
7341 goto out;
7342 }
7343 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7344 len, block_len, type);
7345 if (ret) {
7346 if (em) {
7347 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007348 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007349 start + len - 1, 0);
7350 }
7351 em = ERR_PTR(ret);
7352 }
7353 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007354
7355 return em;
7356}
7357
Josef Bacik4b46fce2010-05-23 11:00:55 -04007358static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7359 u64 start, u64 len)
7360{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007361 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007362 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007363 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007364 struct btrfs_key ins;
7365 u64 alloc_hint;
7366 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007367
Josef Bacik4b46fce2010-05-23 11:00:55 -04007368 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007369 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007370 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007371 if (ret)
7372 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007373
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007374 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7375 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007376 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007377 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007378 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007379 btrfs_free_reserved_extent(fs_info, ins.objectid,
7380 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007381
Josef Bacik4b46fce2010-05-23 11:00:55 -04007382 return em;
7383}
7384
Chris Mason46bfbb52010-05-26 11:04:10 -04007385/*
7386 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7387 * block must be cow'd
7388 */
Josef Bacik00361582013-08-14 14:02:47 -04007389noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007390 u64 *orig_start, u64 *orig_block_len,
7391 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007392{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007393 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007394 struct btrfs_path *path;
7395 int ret;
7396 struct extent_buffer *leaf;
7397 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007398 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007399 struct btrfs_file_extent_item *fi;
7400 struct btrfs_key key;
7401 u64 disk_bytenr;
7402 u64 backref_offset;
7403 u64 extent_end;
7404 u64 num_bytes;
7405 int slot;
7406 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007407 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007408
Chris Mason46bfbb52010-05-26 11:04:10 -04007409 path = btrfs_alloc_path();
7410 if (!path)
7411 return -ENOMEM;
7412
David Sterbaf85b7372017-01-20 14:54:07 +01007413 ret = btrfs_lookup_file_extent(NULL, root, path,
7414 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007415 if (ret < 0)
7416 goto out;
7417
7418 slot = path->slots[0];
7419 if (ret == 1) {
7420 if (slot == 0) {
7421 /* can't find the item, must cow */
7422 ret = 0;
7423 goto out;
7424 }
7425 slot--;
7426 }
7427 ret = 0;
7428 leaf = path->nodes[0];
7429 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007430 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007431 key.type != BTRFS_EXTENT_DATA_KEY) {
7432 /* not our file or wrong item type, must cow */
7433 goto out;
7434 }
7435
7436 if (key.offset > offset) {
7437 /* Wrong offset, must cow */
7438 goto out;
7439 }
7440
7441 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7442 found_type = btrfs_file_extent_type(leaf, fi);
7443 if (found_type != BTRFS_FILE_EXTENT_REG &&
7444 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7445 /* not a regular extent, must cow */
7446 goto out;
7447 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007448
7449 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7450 goto out;
7451
Miao Xiee77751a2013-12-27 21:11:50 +08007452 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7453 if (extent_end <= offset)
7454 goto out;
7455
Chris Mason46bfbb52010-05-26 11:04:10 -04007456 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007457 if (disk_bytenr == 0)
7458 goto out;
7459
7460 if (btrfs_file_extent_compression(leaf, fi) ||
7461 btrfs_file_extent_encryption(leaf, fi) ||
7462 btrfs_file_extent_other_encoding(leaf, fi))
7463 goto out;
7464
Chris Mason46bfbb52010-05-26 11:04:10 -04007465 backref_offset = btrfs_file_extent_offset(leaf, fi);
7466
Josef Bacik7ee9e442013-06-21 16:37:03 -04007467 if (orig_start) {
7468 *orig_start = key.offset - backref_offset;
7469 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7470 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7471 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007472
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007473 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007474 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007475
7476 num_bytes = min(offset + *len, extent_end) - offset;
7477 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7478 u64 range_end;
7479
Jeff Mahoneyda170662016-06-15 09:22:56 -04007480 range_end = round_up(offset + num_bytes,
7481 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007482 ret = test_range_bit(io_tree, offset, range_end,
7483 EXTENT_DELALLOC, 0, NULL);
7484 if (ret) {
7485 ret = -EAGAIN;
7486 goto out;
7487 }
7488 }
7489
Josef Bacik1bda19e2013-10-18 12:10:36 -04007490 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007491
7492 /*
7493 * look for other files referencing this extent, if we
7494 * find any we must cow
7495 */
Josef Bacik00361582013-08-14 14:02:47 -04007496
Liu Boe4c3b2d2017-01-30 12:25:28 -08007497 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007498 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007499 if (ret) {
7500 ret = 0;
7501 goto out;
7502 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007503
7504 /*
7505 * adjust disk_bytenr and num_bytes to cover just the bytes
7506 * in this extent we are about to write. If there
7507 * are any csums in that range we have to cow in order
7508 * to keep the csums correct
7509 */
7510 disk_bytenr += backref_offset;
7511 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007512 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7513 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007514 /*
7515 * all of the above have passed, it is safe to overwrite this extent
7516 * without cow
7517 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007518 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007519 ret = 1;
7520out:
7521 btrfs_free_path(path);
7522 return ret;
7523}
7524
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007525bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7526{
7527 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007528 bool found = false;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007529 void **pagep = NULL;
7530 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007531 unsigned long start_idx;
7532 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007533
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007534 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007535
7536 /*
7537 * end is the last byte in the last page. end == start is legal
7538 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007539 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007540
7541 rcu_read_lock();
7542
7543 /* Most of the code in this while loop is lifted from
7544 * find_get_page. It's been modified to begin searching from a
7545 * page and return just the first page found in that range. If the
7546 * found idx is less than or equal to the end idx then we know that
7547 * a page exists. If no pages are found or if those pages are
7548 * outside of the range then we're fine (yay!) */
7549 while (page == NULL &&
7550 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7551 page = radix_tree_deref_slot(pagep);
7552 if (unlikely(!page))
7553 break;
7554
7555 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007556 if (radix_tree_deref_retry(page)) {
7557 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007558 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007559 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007560 /*
7561 * Otherwise, shmem/tmpfs must be storing a swap entry
7562 * here as an exceptional entry: so return it without
7563 * attempting to raise page count.
7564 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007565 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007566 break; /* TODO: Is this relevant for this use case? */
7567 }
7568
Filipe Manana91405152014-06-05 13:22:24 +01007569 if (!page_cache_get_speculative(page)) {
7570 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007571 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007572 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007573
7574 /*
7575 * Has the page moved?
7576 * This is part of the lockless pagecache protocol. See
7577 * include/linux/pagemap.h for details.
7578 */
7579 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007580 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007581 page = NULL;
7582 }
7583 }
7584
7585 if (page) {
7586 if (page->index <= end_idx)
7587 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007588 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007589 }
7590
7591 rcu_read_unlock();
7592 return found;
7593}
7594
Josef Bacikeb838e72012-07-31 16:28:48 -04007595static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7596 struct extent_state **cached_state, int writing)
7597{
7598 struct btrfs_ordered_extent *ordered;
7599 int ret = 0;
7600
7601 while (1) {
7602 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007603 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007604 /*
7605 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007606 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007607 * extents in this range.
7608 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007609 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007610 lockend - lockstart + 1);
7611
7612 /*
7613 * We need to make sure there are no buffered pages in this
7614 * range either, we could have raced between the invalidate in
7615 * generic_file_direct_write and locking the extent. The
7616 * invalidate needs to happen so that reads after a write do not
7617 * get stale data.
7618 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007619 if (!ordered &&
7620 (!writing ||
7621 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007622 break;
7623
7624 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7625 cached_state, GFP_NOFS);
7626
7627 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007628 /*
7629 * If we are doing a DIO read and the ordered extent we
7630 * found is for a buffered write, we can not wait for it
7631 * to complete and retry, because if we do so we can
7632 * deadlock with concurrent buffered writes on page
7633 * locks. This happens only if our DIO read covers more
7634 * than one extent map, if at this point has already
7635 * created an ordered extent for a previous extent map
7636 * and locked its range in the inode's io tree, and a
7637 * concurrent write against that previous extent map's
7638 * range and this range started (we unlock the ranges
7639 * in the io tree only when the bios complete and
7640 * buffered writes always lock pages before attempting
7641 * to lock range in the io tree).
7642 */
7643 if (writing ||
7644 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7645 btrfs_start_ordered_extent(inode, ordered, 1);
7646 else
7647 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007648 btrfs_put_ordered_extent(ordered);
7649 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007650 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007651 * We could trigger writeback for this range (and wait
7652 * for it to complete) and then invalidate the pages for
7653 * this range (through invalidate_inode_pages2_range()),
7654 * but that can lead us to a deadlock with a concurrent
7655 * call to readpages() (a buffered read or a defrag call
7656 * triggered a readahead) on a page lock due to an
7657 * ordered dio extent we created before but did not have
7658 * yet a corresponding bio submitted (whence it can not
7659 * complete), which makes readpages() wait for that
7660 * ordered extent to complete while holding a lock on
7661 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007662 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007663 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007664 }
7665
Filipe Mananaade77022016-02-18 14:28:55 +00007666 if (ret)
7667 break;
7668
Josef Bacikeb838e72012-07-31 16:28:48 -04007669 cond_resched();
7670 }
7671
7672 return ret;
7673}
7674
Liu Bo6f9994d2017-01-31 07:50:22 -08007675/* The callers of this must take lock_extent() */
7676static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7677 u64 orig_start, u64 block_start,
7678 u64 block_len, u64 orig_block_len,
7679 u64 ram_bytes, int compress_type,
7680 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007681{
7682 struct extent_map_tree *em_tree;
7683 struct extent_map *em;
7684 struct btrfs_root *root = BTRFS_I(inode)->root;
7685 int ret;
7686
Liu Bo6f9994d2017-01-31 07:50:22 -08007687 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7688 type == BTRFS_ORDERED_COMPRESSED ||
7689 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007690 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007691
Josef Bacik69ffb542012-09-11 15:40:07 -04007692 em_tree = &BTRFS_I(inode)->extent_tree;
7693 em = alloc_extent_map();
7694 if (!em)
7695 return ERR_PTR(-ENOMEM);
7696
7697 em->start = start;
7698 em->orig_start = orig_start;
7699 em->len = len;
7700 em->block_len = block_len;
7701 em->block_start = block_start;
7702 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007703 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007704 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007705 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007706 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007707 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007708 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007709 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007710 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7711 em->compress_type = compress_type;
7712 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007713
7714 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007715 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007716 em->start + em->len - 1, 0);
7717 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007718 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007719 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007720 /*
7721 * The caller has taken lock_extent(), who could race with us
7722 * to add em?
7723 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007724 } while (ret == -EEXIST);
7725
7726 if (ret) {
7727 free_extent_map(em);
7728 return ERR_PTR(ret);
7729 }
7730
Liu Bo6f9994d2017-01-31 07:50:22 -08007731 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007732 return em;
7733}
7734
Josef Bacik4b46fce2010-05-23 11:00:55 -04007735static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7736 struct buffer_head *bh_result, int create)
7737{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007738 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007739 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007740 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307741 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007742 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007743 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007744 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007745 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007746 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007747
Miao Xie172a5042013-02-21 02:48:22 -07007748 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007749 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007750 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007751 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007752
Josef Bacikc3298612012-08-03 16:49:19 -04007753 lockstart = start;
7754 lockend = start + len - 1;
7755
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007756 if (current->journal_info) {
7757 /*
7758 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007759 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007760 * confused.
7761 */
chandan50745b02015-08-28 21:10:13 +05307762 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007763 current->journal_info = NULL;
7764 }
7765
Josef Bacikeb838e72012-07-31 16:28:48 -04007766 /*
7767 * If this errors out it's because we couldn't invalidate pagecache for
7768 * this range and we need to fallback to buffered.
7769 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007770 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7771 create)) {
7772 ret = -ENOTBLK;
7773 goto err;
7774 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007775
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007776 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007777 if (IS_ERR(em)) {
7778 ret = PTR_ERR(em);
7779 goto unlock_err;
7780 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007781
7782 /*
7783 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7784 * io. INLINE is special, and we could probably kludge it in here, but
7785 * it's still buffered so for safety lets just fall back to the generic
7786 * buffered path.
7787 *
7788 * For COMPRESSED we _have_ to read the entire extent in so we can
7789 * decompress it, so there will be buffering required no matter what we
7790 * do, so go ahead and fallback to buffered.
7791 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007792 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007793 * to buffered IO. Don't blame me, this is the price we pay for using
7794 * the generic code.
7795 */
7796 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7797 em->block_start == EXTENT_MAP_INLINE) {
7798 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007799 ret = -ENOTBLK;
7800 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007801 }
7802
7803 /* Just a good old fashioned hole, return */
7804 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7805 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7806 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007807 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007808 }
7809
7810 /*
7811 * We don't allocate a new extent in the following cases
7812 *
7813 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7814 * existing extent.
7815 * 2) The extent is marked as PREALLOC. We're good to go here and can
7816 * just use the extent.
7817 *
7818 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007819 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007820 len = min(len, em->len - (start - em->start));
7821 lockstart = start + len;
7822 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007823 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007824
7825 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7826 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7827 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007828 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007829 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007830
7831 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7832 type = BTRFS_ORDERED_PREALLOC;
7833 else
7834 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007835 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007836 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007837
Josef Bacik00361582013-08-14 14:02:47 -04007838 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007839 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007840 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007841 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007842
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007843 em2 = btrfs_create_dio_extent(inode, start, len,
7844 orig_start, block_start,
7845 len, orig_block_len,
7846 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007847 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007848 if (type == BTRFS_ORDERED_PREALLOC) {
7849 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007850 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007851 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007852 if (em2 && IS_ERR(em2)) {
7853 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007854 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007855 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007856 /*
7857 * For inode marked NODATACOW or extent marked PREALLOC,
7858 * use the existing or preallocated extent, so does not
7859 * need to adjust btrfs_space_info's bytes_may_use.
7860 */
7861 btrfs_free_reserved_data_space_noquota(inode,
7862 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007863 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007864 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007865 }
Josef Bacik00361582013-08-14 14:02:47 -04007866
Chris Mason46bfbb52010-05-26 11:04:10 -04007867 /*
7868 * this will cow the extent, reset the len in case we changed
7869 * it above
7870 */
7871 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007872 free_extent_map(em);
7873 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007874 if (IS_ERR(em)) {
7875 ret = PTR_ERR(em);
7876 goto unlock_err;
7877 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007878 len = min(len, em->len - (start - em->start));
7879unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007880 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7881 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007882 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007883 bh_result->b_bdev = em->bdev;
7884 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007885 if (create) {
7886 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7887 set_buffer_new(bh_result);
7888
7889 /*
7890 * Need to update the i_size under the extent lock so buffered
7891 * readers will get the updated i_size when we unlock.
7892 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007893 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007894 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007895
chandan50745b02015-08-28 21:10:13 +05307896 WARN_ON(dio_data->reserve < len);
7897 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007898 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307899 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007900 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007901
Josef Bacikeb838e72012-07-31 16:28:48 -04007902 /*
7903 * In the case of write we need to clear and unlock the entire range,
7904 * in the case of read we need to unlock only the end area that we
7905 * aren't using if there is any left over space.
7906 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007907 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007908 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7909 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007910 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007911 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007912 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007913 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007914
Josef Bacik4b46fce2010-05-23 11:00:55 -04007915 free_extent_map(em);
7916
7917 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007918
7919unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007920 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007921 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007922err:
chandan50745b02015-08-28 21:10:13 +05307923 if (dio_data)
7924 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007925 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007926}
7927
Omar Sandoval58efbc92017-08-22 23:45:59 -07007928static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7929 struct bio *bio,
7930 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007931{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007932 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007933 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007934
Mike Christie37226b22016-06-05 14:31:52 -05007935 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007936
7937 bio_get(bio);
7938
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007939 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007940 if (ret)
7941 goto err;
7942
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007943 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007944err:
7945 bio_put(bio);
7946 return ret;
7947}
7948
7949static int btrfs_check_dio_repairable(struct inode *inode,
7950 struct bio *failed_bio,
7951 struct io_failure_record *failrec,
7952 int failed_mirror)
7953{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007954 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007955 int num_copies;
7956
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007957 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007958 if (num_copies == 1) {
7959 /*
7960 * we only have a single copy of the data, so don't bother with
7961 * all the retry and error correction code that follows. no
7962 * matter what the error is, it is very likely to persist.
7963 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007964 btrfs_debug(fs_info,
7965 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7966 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007967 return 0;
7968 }
7969
7970 failrec->failed_mirror = failed_mirror;
7971 failrec->this_mirror++;
7972 if (failrec->this_mirror == failed_mirror)
7973 failrec->this_mirror++;
7974
7975 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007976 btrfs_debug(fs_info,
7977 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7978 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007979 return 0;
7980 }
7981
7982 return 1;
7983}
7984
Omar Sandoval58efbc92017-08-22 23:45:59 -07007985static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7986 struct page *page, unsigned int pgoff,
7987 u64 start, u64 end, int failed_mirror,
7988 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007989{
7990 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007991 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7992 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007993 struct bio *bio;
7994 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007995 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007996 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007997 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007998 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007999
Mike Christie37226b22016-06-05 14:31:52 -05008000 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008001
8002 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8003 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07008004 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08008005
8006 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8007 failed_mirror);
8008 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008009 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008010 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08008011 }
8012
Liu Bo17347ce2017-05-15 15:33:27 -07008013 segs = bio_segments(failed_bio);
8014 if (segs > 1 ||
8015 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008016 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008017
8018 isector = start - btrfs_io_bio(failed_bio)->logical;
8019 isector >>= inode->i_sb->s_blocksize_bits;
8020 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308021 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05008022 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008023
8024 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02008025 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08008026 read_mode, failrec->this_mirror, failrec->in_validation);
8027
Omar Sandoval58efbc92017-08-22 23:45:59 -07008028 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
8029 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04008030 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008031 bio_put(bio);
8032 }
8033
Omar Sandoval58efbc92017-08-22 23:45:59 -07008034 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08008035}
8036
8037struct btrfs_retry_complete {
8038 struct completion done;
8039 struct inode *inode;
8040 u64 start;
8041 int uptodate;
8042};
8043
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008044static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008045{
8046 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008047 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008048 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008049 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008050 int i;
8051
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008052 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008053 goto end;
8054
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308055 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008056 io_tree = &BTRFS_I(inode)->io_tree;
8057 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8058 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308059
Miao Xie8b110e32014-09-12 18:44:03 +08008060 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008061 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008062 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008063 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8064 io_tree, done->start, bvec->bv_page,
8065 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008066end:
8067 complete(&done->done);
8068 bio_put(bio);
8069}
8070
Omar Sandoval58efbc92017-08-22 23:45:59 -07008071static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8072 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008073{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308074 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008075 struct bio_vec bvec;
8076 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008077 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008078 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308079 unsigned int pgoff;
8080 u32 sectorsize;
8081 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008082 blk_status_t ret;
8083 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008084
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308085 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008086 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308087
Miao Xiec1dc0892014-09-12 18:43:56 +08008088 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008089 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008090 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008091
Liu Bo17347ce2017-05-15 15:33:27 -07008092 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8093 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8094 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308095
8096next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008097 done.uptodate = 0;
8098 done.start = start;
8099 init_completion(&done.done);
8100
Liu Bo17347ce2017-05-15 15:33:27 -07008101 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308102 pgoff, start, start + sectorsize - 1,
8103 io_bio->mirror_num,
8104 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008105 if (ret) {
8106 err = ret;
8107 goto next;
8108 }
Miao Xie8b110e32014-09-12 18:44:03 +08008109
David Sterba9c17f6c2017-07-19 19:26:45 +02008110 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008111
8112 if (!done.uptodate) {
8113 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308114 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008115 }
8116
Liu Bo629ebf42017-05-15 17:20:07 -07008117next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308118 start += sectorsize;
8119
Liu Bo97bf5a52017-04-07 13:11:10 -07008120 nr_sectors--;
8121 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308122 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008123 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308124 goto next_block_or_try_again;
8125 }
Miao Xie8b110e32014-09-12 18:44:03 +08008126 }
8127
Liu Bo629ebf42017-05-15 17:20:07 -07008128 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008129}
8130
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008131static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008132{
8133 struct btrfs_retry_complete *done = bio->bi_private;
8134 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008135 struct extent_io_tree *io_tree, *failure_tree;
8136 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008137 struct bio_vec *bvec;
8138 int uptodate;
8139 int ret;
8140 int i;
8141
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008142 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008143 goto end;
8144
8145 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308146
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308147 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008148 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308149
Josef Bacik7870d082017-05-05 11:57:15 -04008150 io_tree = &BTRFS_I(inode)->io_tree;
8151 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8152
David Sterbac09abff2017-07-13 18:10:07 +02008153 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008154 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008155 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8156 bvec->bv_offset, done->start,
8157 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008158 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008159 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8160 failure_tree, io_tree, done->start,
8161 bvec->bv_page,
8162 btrfs_ino(BTRFS_I(inode)),
8163 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008164 else
8165 uptodate = 0;
8166 }
8167
8168 done->uptodate = uptodate;
8169end:
8170 complete(&done->done);
8171 bio_put(bio);
8172}
8173
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008174static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8175 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008176{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308177 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008178 struct bio_vec bvec;
8179 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008180 struct btrfs_retry_complete done;
8181 u64 start;
8182 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308183 u32 sectorsize;
8184 int nr_sectors;
8185 unsigned int pgoff;
8186 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008187 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008188 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008189 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008190
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308191 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008192 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308193
Omar Sandoval58efbc92017-08-22 23:45:59 -07008194 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008195 start = io_bio->logical;
8196 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008197 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008198
Liu Bo17347ce2017-05-15 15:33:27 -07008199 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8200 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308201
Liu Bo17347ce2017-05-15 15:33:27 -07008202 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308203next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008204 if (uptodate) {
8205 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8206 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8207 bvec.bv_page, pgoff, start, sectorsize);
8208 if (likely(!ret))
8209 goto next;
8210 }
Miao Xie8b110e32014-09-12 18:44:03 +08008211try_again:
8212 done.uptodate = 0;
8213 done.start = start;
8214 init_completion(&done.done);
8215
Omar Sandoval58efbc92017-08-22 23:45:59 -07008216 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8217 pgoff, start, start + sectorsize - 1,
8218 io_bio->mirror_num, btrfs_retry_endio,
8219 &done);
8220 if (status) {
8221 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008222 goto next;
8223 }
8224
David Sterba9c17f6c2017-07-19 19:26:45 +02008225 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008226
8227 if (!done.uptodate) {
8228 /* We might have another mirror, so try again */
8229 goto try_again;
8230 }
8231next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308232 offset += sectorsize;
8233 start += sectorsize;
8234
8235 ASSERT(nr_sectors);
8236
Liu Bo97bf5a52017-04-07 13:11:10 -07008237 nr_sectors--;
8238 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308239 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008240 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308241 goto next_block;
8242 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008243 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008244
8245 return err;
8246}
8247
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008248static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8249 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008250{
8251 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8252
8253 if (skip_csum) {
8254 if (unlikely(err))
8255 return __btrfs_correct_data_nocsum(inode, io_bio);
8256 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008257 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008258 } else {
8259 return __btrfs_subio_endio_read(inode, io_bio, err);
8260 }
8261}
8262
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008263static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008264{
8265 struct btrfs_dio_private *dip = bio->bi_private;
8266 struct inode *inode = dip->inode;
8267 struct bio *dio_bio;
8268 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008269 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008270
Liu Bo99c4e3b2017-09-15 15:06:51 -06008271 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008272 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008273
Josef Bacik4b46fce2010-05-23 11:00:55 -04008274 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008275 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008276 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008277
Josef Bacik4b46fce2010-05-23 11:00:55 -04008278 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008279
Liu Bo99c4e3b2017-09-15 15:06:51 -06008280 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008281 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008282
8283 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008284 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008285 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008286}
8287
Qu Wenruo524272602017-03-08 10:25:52 +08008288static void __endio_write_update_ordered(struct inode *inode,
8289 const u64 offset, const u64 bytes,
8290 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008291{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008292 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008293 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008294 struct btrfs_workqueue *wq;
8295 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008296 u64 ordered_offset = offset;
8297 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008298 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008299 int ret;
8300
Qu Wenruo524272602017-03-08 10:25:52 +08008301 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8302 wq = fs_info->endio_freespace_worker;
8303 func = btrfs_freespace_write_helper;
8304 } else {
8305 wq = fs_info->endio_write_workers;
8306 func = btrfs_endio_write_helper;
8307 }
8308
Chris Mason163cf092010-11-28 19:56:33 -05008309again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008310 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008311 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8312 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008313 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008314 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008315 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008316 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008317
Qu Wenruo524272602017-03-08 10:25:52 +08008318 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8319 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008320out_test:
8321 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008322 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8323 * in the range, we can exit.
8324 */
8325 if (ordered_offset == last_offset)
8326 return;
8327 /*
Chris Mason163cf092010-11-28 19:56:33 -05008328 * our bio might span multiple ordered extents. If we haven't
8329 * completed the accounting for the whole dio, go back and try again
8330 */
Filipe Manana14543772015-11-24 16:23:54 +00008331 if (ordered_offset < offset + bytes) {
8332 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008333 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008334 goto again;
8335 }
Filipe Manana14543772015-11-24 16:23:54 +00008336}
8337
8338static void btrfs_endio_direct_write(struct bio *bio)
8339{
8340 struct btrfs_dio_private *dip = bio->bi_private;
8341 struct bio *dio_bio = dip->dio_bio;
8342
Qu Wenruo524272602017-03-08 10:25:52 +08008343 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008344 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008345
Josef Bacik4b46fce2010-05-23 11:00:55 -04008346 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008347
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008348 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008349 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008350 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008351}
8352
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008353static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008354 struct bio *bio, int mirror_num,
8355 unsigned long bio_flags, u64 offset)
8356{
Josef Bacikc6100a42017-05-05 11:57:13 -04008357 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008358 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008359 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008360 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008361 return 0;
8362}
8363
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008364static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008365{
8366 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008367 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008368
Miao Xie8b110e32014-09-12 18:44:03 +08008369 if (err)
8370 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008371 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008372 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8373 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008374 (unsigned long long)bio->bi_iter.bi_sector,
8375 bio->bi_iter.bi_size, err);
8376
8377 if (dip->subio_endio)
8378 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008379
8380 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008381 dip->errors = 1;
8382
8383 /*
8384 * before atomic variable goto zero, we must make sure
8385 * dip->errors is perceived to be set.
8386 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008387 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008388 }
8389
8390 /* if there are more bios still pending for this dio, just exit */
8391 if (!atomic_dec_and_test(&dip->pending_bios))
8392 goto out;
8393
Chris Mason9be33952013-05-17 18:30:14 -04008394 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008395 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008396 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008397 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008398 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008399 }
8400out:
8401 bio_put(bio);
8402}
8403
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008404static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008405 struct btrfs_dio_private *dip,
8406 struct bio *bio,
8407 u64 file_offset)
8408{
8409 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8410 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008411 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008412
8413 /*
8414 * We load all the csum data we need when we submit
8415 * the first bio to reduce the csum tree search and
8416 * contention.
8417 */
8418 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008419 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008420 file_offset);
8421 if (ret)
8422 return ret;
8423 }
8424
8425 if (bio == dip->orig_bio)
8426 return 0;
8427
8428 file_offset -= dip->logical_offset;
8429 file_offset >>= inode->i_sb->s_blocksize_bits;
8430 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8431
8432 return 0;
8433}
8434
Omar Sandoval58efbc92017-08-22 23:45:59 -07008435static inline blk_status_t
8436__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008437 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008438{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008439 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008440 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008441 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008442 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008443
Liu Bo4c274bc2017-11-01 17:19:27 -06008444 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008445 if (async_submit)
8446 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8447
Miao Xiee65e1532010-11-22 03:04:43 +00008448 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008449
8450 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008451 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008452 if (ret)
8453 goto err;
8454 }
Miao Xiee65e1532010-11-22 03:04:43 +00008455
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008456 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008457 goto map;
8458
8459 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008460 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8461 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008462 __btrfs_submit_bio_start_direct_io,
8463 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008464 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008465 } else if (write) {
8466 /*
8467 * If we aren't doing async submit, calculate the csum of the
8468 * bio now.
8469 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008470 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008471 if (ret)
8472 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008473 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008474 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008475 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008476 if (ret)
8477 goto err;
8478 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008479map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008480 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008481err:
8482 bio_put(bio);
8483 return ret;
8484}
8485
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008486static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008487{
8488 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008489 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008490 struct bio *bio;
8491 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008492 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008493 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008494 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008495 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008496 u64 submit_len;
8497 int clone_offset = 0;
8498 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308499 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008500 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008501
Kent Overstreet4f024f32013-10-11 15:44:27 -07008502 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008503 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008504 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8505 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008506 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008507 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008508
Liu Bo725130b2017-05-16 09:51:39 -07008509 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008510 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008511 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008512 goto submit;
8513 }
8514
David Woodhouse53b381b2013-01-29 18:40:14 -05008515 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008516 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008517 async_submit = 0;
8518 else
8519 async_submit = 1;
8520
Liu Bo725130b2017-05-16 09:51:39 -07008521 /* bio split */
8522 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008523 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008524 do {
Liu Bo725130b2017-05-16 09:51:39 -07008525 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008526
Liu Bo725130b2017-05-16 09:51:39 -07008527 /*
8528 * This will never fail as it's passing GPF_NOFS and
8529 * the allocation is backed by btrfs_bioset.
8530 */
Liu Boe4770942017-05-16 10:57:14 -07008531 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008532 clone_len);
8533 bio->bi_private = dip;
8534 bio->bi_end_io = btrfs_end_dio_bio;
8535 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008536
Liu Bo725130b2017-05-16 09:51:39 -07008537 ASSERT(submit_len >= clone_len);
8538 submit_len -= clone_len;
8539 if (submit_len == 0)
8540 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008541
Liu Bo725130b2017-05-16 09:51:39 -07008542 /*
8543 * Increase the count before we submit the bio so we know
8544 * the end IO handler won't happen before we increase the
8545 * count. Otherwise, the dip might get freed before we're
8546 * done setting it up.
8547 */
8548 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008549
Linus Torvalds66ba7722017-09-09 13:27:51 -07008550 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008551 async_submit);
8552 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008553 bio_put(bio);
8554 atomic_dec(&dip->pending_bios);
8555 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008556 }
Liu Bo725130b2017-05-16 09:51:39 -07008557
8558 clone_offset += clone_len;
8559 start_sector += clone_len >> 9;
8560 file_offset += clone_len;
8561
8562 map_length = submit_len;
8563 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8564 start_sector << 9, &map_length, NULL, 0);
8565 if (ret)
8566 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008567 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008568
Josef Bacik02f57c72011-04-06 14:25:44 -04008569submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008570 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008571 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008572 return 0;
8573
8574 bio_put(bio);
8575out_err:
8576 dip->errors = 1;
8577 /*
8578 * before atomic variable goto zero, we must
8579 * make sure dip->errors is perceived to be set.
8580 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008581 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008582 if (atomic_dec_and_test(&dip->pending_bios))
8583 bio_io_error(dip->orig_bio);
8584
8585 /* bio_end_io() will handle error, so we needn't return it */
8586 return 0;
8587}
8588
Mike Christie8a4c1e42016-06-05 14:31:50 -05008589static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8590 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008591{
Filipe Manana61de7182015-07-01 12:13:10 +01008592 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008593 struct bio *bio = NULL;
8594 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008595 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008596 int ret = 0;
8597
David Sterba8b6c1d52017-06-02 17:48:13 +02008598 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008599
Miao Xiec1dc0892014-09-12 18:43:56 +08008600 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008601 if (!dip) {
8602 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008603 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008604 }
8605
8606 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008607 dip->inode = inode;
8608 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008609 dip->bytes = dio_bio->bi_iter.bi_size;
8610 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008611 bio->bi_private = dip;
8612 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008613 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008614 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008615 io_bio = btrfs_io_bio(bio);
8616 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008617
Miao Xiec1dc0892014-09-12 18:43:56 +08008618 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008619 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008620 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008621 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008622 dip->subio_endio = btrfs_subio_endio_read;
8623 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008624
Filipe Mananaf28a4922015-12-08 19:23:20 +00008625 /*
8626 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8627 * even if we fail to submit a bio, because in such case we do the
8628 * corresponding error handling below and it must not be done a second
8629 * time by btrfs_direct_IO().
8630 */
8631 if (write) {
8632 struct btrfs_dio_data *dio_data = current->journal_info;
8633
8634 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8635 dip->bytes;
8636 dio_data->unsubmitted_oe_range_start =
8637 dio_data->unsubmitted_oe_range_end;
8638 }
8639
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008640 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008641 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008642 return;
Chris Mason9be33952013-05-17 18:30:14 -04008643
Liu Bo3892ac92017-04-17 15:00:28 -07008644 if (io_bio->end_io)
8645 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008646
Josef Bacik4b46fce2010-05-23 11:00:55 -04008647free_ordered:
8648 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008649 * If we arrived here it means either we failed to submit the dip
8650 * or we either failed to clone the dio_bio or failed to allocate the
8651 * dip. If we cloned the dio_bio and allocated the dip, we can just
8652 * call bio_endio against our io_bio so that we get proper resource
8653 * cleanup if we fail to submit the dip, otherwise, we must do the
8654 * same as btrfs_endio_direct_[write|read] because we can't call these
8655 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008656 */
Liu Bo3892ac92017-04-17 15:00:28 -07008657 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008658 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008659 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008660 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008661 * and all the cleanup and final put for dio_bio (through
8662 * dio_end_io()).
8663 */
8664 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008665 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008666 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008667 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008668 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008669 file_offset,
8670 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008671 false);
Filipe Manana14543772015-11-24 16:23:54 +00008672 else
Filipe Manana61de7182015-07-01 12:13:10 +01008673 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8674 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008675
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008676 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008677 /*
8678 * Releases and cleans up our dio_bio, no need to bio_put()
8679 * nor bio_endio()/bio_io_error() against dio_bio.
8680 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008681 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008682 }
Liu Bo3892ac92017-04-17 15:00:28 -07008683 if (bio)
8684 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008685 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008686}
8687
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008688static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008689 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008690{
8691 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008692 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008693 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008694 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008695
8696 if (offset & blocksize_mask)
8697 goto out;
8698
Al Viro28060d52014-03-22 05:15:17 -04008699 if (iov_iter_alignment(iter) & blocksize_mask)
8700 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008701
Al Viro28060d52014-03-22 05:15:17 -04008702 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008703 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008704 return 0;
8705 /*
8706 * Check to make sure we don't have duplicate iov_base's in this
8707 * iovec, if so return EINVAL, otherwise we'll get csum errors
8708 * when reading back.
8709 */
8710 for (seg = 0; seg < iter->nr_segs; seg++) {
8711 for (i = seg + 1; i < iter->nr_segs; i++) {
8712 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008713 goto out;
8714 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008715 }
8716 retval = 0;
8717out:
8718 return retval;
8719}
Josef Bacikeb838e72012-07-31 16:28:48 -04008720
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008721static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008722{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008723 struct file *file = iocb->ki_filp;
8724 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008725 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308726 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008727 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008728 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008729 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008730 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008731 bool wakeup = true;
8732 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008733 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008734
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008735 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008736 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008737
Jens Axboefe0f07d2015-04-15 17:05:48 -06008738 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008739
Josef Bacik0e267c42013-07-02 10:38:02 -04008740 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008741 * The generic stuff only does filemap_write_and_wait_range, which
8742 * isn't enough if we've written compressed pages to this area, so
8743 * we need to flush the dirty pages again to make absolutely sure
8744 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008745 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008746 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008747 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8748 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008749 filemap_fdatawrite_range(inode->i_mapping, offset,
8750 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008751
Omar Sandoval6f673762015-03-16 04:33:52 -07008752 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008753 /*
8754 * If the write DIO is beyond the EOF, we need update
8755 * the isize, but it is protected by i_mutex. So we can
8756 * not unlock the i_mutex at this case.
8757 */
8758 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008759 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008760 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008761 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008762 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8763 ret = -EAGAIN;
8764 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008765 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008766 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8767 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008768 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008769 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008770
8771 /*
8772 * We need to know how many extents we reserved so that we can
8773 * do the accounting properly if we go over the number we
8774 * originally calculated. Abuse current->journal_info for this.
8775 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008776 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008777 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008778 dio_data.unsubmitted_oe_range_start = (u64)offset;
8779 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308780 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308781 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008782 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8783 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008784 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008785 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8786 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008787 }
8788
Omar Sandoval17f8c842015-03-16 04:33:50 -07008789 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008790 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008791 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008792 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008793 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308794 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008795 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008796 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308797 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008798 btrfs_delalloc_release_space(inode, data_reserved,
8799 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008800 /*
8801 * On error we might have left some ordered extents
8802 * without submitting corresponding bios for them, so
8803 * cleanup them up to avoid other tasks getting them
8804 * and waiting for them to complete forever.
8805 */
8806 if (dio_data.unsubmitted_oe_range_start <
8807 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008808 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008809 dio_data.unsubmitted_oe_range_start,
8810 dio_data.unsubmitted_oe_range_end -
8811 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008812 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008813 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008814 btrfs_delalloc_release_space(inode, data_reserved,
8815 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008816 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008817 }
Miao Xie38851cc2013-02-08 07:04:11 +00008818out:
Miao Xie2e60a512013-02-08 07:01:08 +00008819 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008820 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008821 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008822 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008823
Qu Wenruo364ecf32017-02-27 15:10:38 +08008824 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008825 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008826}
8827
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008828#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8829
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008830static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8831 __u64 start, __u64 len)
8832{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008833 int ret;
8834
8835 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8836 if (ret)
8837 return ret;
8838
David Sterba2135fb92017-06-23 04:09:57 +02008839 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008840}
8841
Chris Mason9ebefb182007-06-15 13:50:00 -04008842int btrfs_readpage(struct file *file, struct page *page)
8843{
Chris Masond1310b22008-01-24 16:13:08 -05008844 struct extent_io_tree *tree;
8845 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008846 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008847}
Chris Mason1832a6d2007-12-21 16:27:21 -05008848
Chris Mason39279cc2007-06-12 06:35:45 -04008849static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8850{
Chris Masond1310b22008-01-24 16:13:08 -05008851 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008852 struct inode *inode = page->mapping->host;
8853 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008854
8855 if (current->flags & PF_MEMALLOC) {
8856 redirty_page_for_writepage(wbc, page);
8857 unlock_page(page);
8858 return 0;
8859 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008860
8861 /*
8862 * If we are under memory pressure we will call this directly from the
8863 * VM, we need to make sure we have the inode referenced for the ordered
8864 * extent. If not just return like we didn't do anything.
8865 */
8866 if (!igrab(inode)) {
8867 redirty_page_for_writepage(wbc, page);
8868 return AOP_WRITEPAGE_ACTIVATE;
8869 }
Chris Masond1310b22008-01-24 16:13:08 -05008870 tree = &BTRFS_I(page->mapping->host)->io_tree;
David Sterbadeac6422017-06-23 03:47:28 +02008871 ret = extent_write_full_page(tree, page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008872 btrfs_add_delayed_iput(inode);
8873 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008874}
Chris Mason39279cc2007-06-12 06:35:45 -04008875
Eric Sandeen48a3b632013-04-25 20:41:01 +00008876static int btrfs_writepages(struct address_space *mapping,
8877 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008878{
Chris Masond1310b22008-01-24 16:13:08 -05008879 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008880
Chris Masond1310b22008-01-24 16:13:08 -05008881 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba43317592017-06-23 03:46:07 +02008882 return extent_writepages(tree, mapping, wbc);
Chris Masonb293f022007-11-01 19:45:34 -04008883}
8884
Chris Mason3ab2fb52007-11-08 10:59:22 -05008885static int
8886btrfs_readpages(struct file *file, struct address_space *mapping,
8887 struct list_head *pages, unsigned nr_pages)
8888{
Chris Masond1310b22008-01-24 16:13:08 -05008889 struct extent_io_tree *tree;
8890 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba09325842017-06-23 04:09:57 +02008891 return extent_readpages(tree, mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008892}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008893static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008894{
Chris Masond1310b22008-01-24 16:13:08 -05008895 struct extent_io_tree *tree;
8896 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008897 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008898
Chris Masond1310b22008-01-24 16:13:08 -05008899 tree = &BTRFS_I(page->mapping->host)->io_tree;
8900 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008901 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008902 if (ret == 1) {
8903 ClearPagePrivate(page);
8904 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008905 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008906 }
8907 return ret;
8908}
Chris Mason39279cc2007-06-12 06:35:45 -04008909
Chris Masone6dcd2d2008-07-17 12:53:50 -04008910static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8911{
Chris Mason98509cf2008-09-11 15:51:43 -04008912 if (PageWriteback(page) || PageDirty(page))
8913 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008914 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008915}
8916
Lukas Czernerd47992f2013-05-21 23:17:23 -04008917static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8918 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008919{
Josef Bacik5fd02042012-05-02 14:00:54 -04008920 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008921 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008922 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008923 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008924 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008925 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308926 u64 start;
8927 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008928 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008929
Chris Mason8b62b722009-09-02 16:53:46 -04008930 /*
8931 * we have the page locked, so new writeback can't start,
8932 * and the dirty bit won't be cleared while we are here.
8933 *
8934 * Wait for IO on this page so that we can safely clear
8935 * the PagePrivate2 bit and do ordered accounting
8936 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008937 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008938
Josef Bacik5fd02042012-05-02 14:00:54 -04008939 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008940 if (offset) {
8941 btrfs_releasepage(page, GFP_NOFS);
8942 return;
8943 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008944
8945 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008946 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308947again:
8948 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008949 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308950 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008951 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308952 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008953 /*
8954 * IO on this page will never be started, so we need
8955 * to account for any ordered extents now
8956 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008957 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308958 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008959 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008960 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008961 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008962 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008963 /*
8964 * whoever cleared the private bit is responsible
8965 * for the finish_ordered_io
8966 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008967 if (TestClearPagePrivate2(page)) {
8968 struct btrfs_ordered_inode_tree *tree;
8969 u64 new_len;
8970
8971 tree = &BTRFS_I(inode)->ordered_tree;
8972
8973 spin_lock_irq(&tree->lock);
8974 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308975 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008976 if (new_len < ordered->truncated_len)
8977 ordered->truncated_len = new_len;
8978 spin_unlock_irq(&tree->lock);
8979
8980 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308981 start,
8982 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008983 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008984 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008985 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008986 if (!inode_evicting) {
8987 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308988 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008989 &cached_state);
8990 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308991
8992 start = end + 1;
8993 if (start < page_end)
8994 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008995 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008996
Qu Wenruob9d0b382015-09-29 10:35:16 +08008997 /*
8998 * Qgroup reserved space handler
8999 * Page here will be either
9000 * 1) Already written to disk
9001 * In this case, its reserved space is released from data rsv map
9002 * and will be freed by delayed_ref handler finally.
9003 * So even we call qgroup_free_data(), it won't decrease reserved
9004 * space.
9005 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009006 * This means the reserved space should be freed here. However,
9007 * if a truncate invalidates the page (by clearing PageDirty)
9008 * and the page is accounted for while allocating extent
9009 * in btrfs_check_data_free_space() we let delayed_ref to
9010 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009011 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009012 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08009013 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009014 if (!inode_evicting) {
9015 clear_extent_bit(tree, page_start, page_end,
9016 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009017 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9018 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01009019 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009020
9021 __btrfs_releasepage(page, GFP_NOFS);
9022 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009023
Chris Mason4a096752008-07-21 10:29:44 -04009024 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009025 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009026 ClearPagePrivate(page);
9027 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009028 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009029 }
Chris Mason39279cc2007-06-12 06:35:45 -04009030}
9031
Chris Mason9ebefb182007-06-15 13:50:00 -04009032/*
9033 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9034 * called from a page fault handler when a page is first dirtied. Hence we must
9035 * be careful to check for EOF conditions here. We set the page up correctly
9036 * for a written page which means we get ENOSPC checking when writing into
9037 * holes and correct delalloc and unwritten extent mapping on filesystems that
9038 * support these features.
9039 *
9040 * We are not allowed to take the i_mutex here so we have to play games to
9041 * protect against truncate races as the page could now be beyond EOF. Because
9042 * vmtruncate() writes the inode size before removing pages, once we have the
9043 * page lock we can determine safely if the page is beyond EOF. If it is not
9044 * beyond EOF, then the page is guaranteed safe against truncation until we
9045 * unlock the page.
9046 */
Dave Jiang11bac802017-02-24 14:56:41 -08009047int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009048{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009049 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009050 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009051 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009052 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9053 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009054 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009055 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009056 char *kaddr;
9057 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009058 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009059 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009060 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309061 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009062 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009063 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309064 u64 end;
9065
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009066 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009067
Jan Karab2b5ef52012-06-12 16:20:45 +02009068 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009069 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009070 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309071 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009072
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309073 /*
9074 * Reserving delalloc space after obtaining the page lock can lead to
9075 * deadlock. For example, if a dirty page is locked by this function
9076 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9077 * dirty page write out, then the btrfs_writepage() function could
9078 * end up waiting indefinitely to get a lock on the page currently
9079 * being processed by btrfs_page_mkwrite() function.
9080 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009081 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309082 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009083 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009084 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009085 reserved = 1;
9086 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009087 if (ret) {
9088 if (ret == -ENOMEM)
9089 ret = VM_FAULT_OOM;
9090 else /* -ENOSPC, -EIO, etc */
9091 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009092 if (reserved)
9093 goto out;
9094 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009095 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009096
Nick Piggin56a76f82009-03-31 15:23:23 -07009097 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009098again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009099 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009100 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009101
Chris Mason9ebefb182007-06-15 13:50:00 -04009102 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009103 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009104 /* page got truncated out from underneath us */
9105 goto out_unlock;
9106 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009107 wait_on_page_writeback(page);
9108
David Sterbaff13db42015-12-03 14:30:40 +01009109 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009110 set_page_extent_mapped(page);
9111
Chris Masoneb84ae02008-07-17 13:53:27 -04009112 /*
9113 * we can't set the delalloc bits if there are pending ordered
9114 * extents. Drop our locks and wait for them to finish
9115 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009116 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9117 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009118 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009119 unlock_extent_cached(io_tree, page_start, page_end,
9120 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009121 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009122 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009123 btrfs_put_ordered_extent(ordered);
9124 goto again;
9125 }
9126
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009127 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009128 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009129 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009130 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309131 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009132 btrfs_delalloc_release_space(inode, data_reserved,
9133 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309134 }
9135 }
9136
Josef Bacikfbf19082009-10-01 17:10:23 -04009137 /*
Liu Bo54160342016-12-13 12:15:19 -08009138 * page_mkwrite gets called when the page is firstly dirtied after it's
9139 * faulted in, but write(2) could also dirty a page and set delalloc
9140 * bits, thus in this case for space account reason, we still need to
9141 * clear any delalloc bits within this page range since we have to
9142 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009143 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309144 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009145 EXTENT_DIRTY | EXTENT_DELALLOC |
9146 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009147 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009148
Filipe Mananae3b8a482017-11-04 00:16:59 +00009149 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009150 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009151 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009152 unlock_extent_cached(io_tree, page_start, page_end,
9153 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009154 ret = VM_FAULT_SIGBUS;
9155 goto out_unlock;
9156 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009157 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009158
9159 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009160 if (page_start + PAGE_SIZE > size)
9161 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009162 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009163 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009164
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009165 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009166 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009167 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009168 flush_dcache_page(page);
9169 kunmap(page);
9170 }
Chris Mason247e7432008-07-17 12:53:51 -04009171 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009172 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009173 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009174
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009175 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009176 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009177 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009178
Josef Bacik2ac55d42010-02-03 19:33:23 +00009179 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009180
9181out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009182 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009183 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009184 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009185 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009186 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009187 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009188 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009189out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009190 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009191 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9192 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009193out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009194 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009195 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009196 return ret;
9197}
9198
Josef Bacika41ad392011-01-31 15:30:16 -05009199static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009200{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009201 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009202 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009203 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009204 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009205 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009206 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009207 u64 mask = fs_info->sectorsize - 1;
9208 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009209
Josef Bacik0ef8b722013-10-25 16:13:35 -04009210 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9211 (u64)-1);
9212 if (ret)
9213 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009214
Josef Bacikfcb80c22011-05-03 10:40:22 -04009215 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009216 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009217 * 3 things going on here
9218 *
9219 * 1) We need to reserve space for our orphan item and the space to
9220 * delete our orphan item. Lord knows we don't want to have a dangling
9221 * orphan item because we didn't reserve space to remove it.
9222 *
9223 * 2) We need to reserve space to update our inode.
9224 *
9225 * 3) We need to have something to cache all the space that is going to
9226 * be free'd up by the truncate operation, but also have some slack
9227 * space reserved in case it uses space during the truncate (thank you
9228 * very much snapshotting).
9229 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009230 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009231 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009232 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009233 * doesn't end up using space reserved for updating the inode or
9234 * removing the orphan item. We also need to be able to stop the
9235 * transaction and start a new one, which means we need to be able to
9236 * update the inode several times, and we have no idea of knowing how
9237 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009238 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009239 * Then there is the orphan item, which does indeed need to be held on
9240 * to for the whole operation, and we need nobody to touch this reserved
9241 * space except the orphan code.
9242 *
9243 * So that leaves us with
9244 *
9245 * 1) root->orphan_block_rsv - for the orphan deletion.
9246 * 2) rsv - for the truncate reservation, which we will steal from the
9247 * transaction reservation.
9248 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9249 * updating the inode.
9250 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009251 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009252 if (!rsv)
9253 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009254 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009255 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009256
Josef Bacik907cbce2011-08-08 13:46:15 -04009257 /*
Josef Bacik07127182011-08-19 10:29:59 -04009258 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009259 * 1 for updating the inode.
9260 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009261 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009262 if (IS_ERR(trans)) {
9263 err = PTR_ERR(trans);
9264 goto out;
9265 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009266
Josef Bacik907cbce2011-08-08 13:46:15 -04009267 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009268 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009269 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009270 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009271
Chris Mason5a3f23d2009-03-31 13:27:11 -04009272 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009273 * So if we truncate and then write and fsync we normally would just
9274 * write the extents that changed, which is a problem if we need to
9275 * first truncate that entire inode. So set this flag so we write out
9276 * all of the extents in the inode to the sync log so we're completely
9277 * safe.
9278 */
9279 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009280 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009281
Yan, Zheng80825102009-11-12 09:35:36 +00009282 while (1) {
9283 ret = btrfs_truncate_inode_items(trans, root, inode,
9284 inode->i_size,
9285 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009286 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009287 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009288 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009289 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009290 }
Chris Mason39279cc2007-06-12 06:35:45 -04009291
Yan, Zheng80825102009-11-12 09:35:36 +00009292 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009293 if (ret) {
9294 err = ret;
9295 break;
9296 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009297
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009298 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009299 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009300
9301 trans = btrfs_start_transaction(root, 2);
9302 if (IS_ERR(trans)) {
9303 ret = err = PTR_ERR(trans);
9304 trans = NULL;
9305 break;
9306 }
9307
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009308 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009309 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009310 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009311 BUG_ON(ret); /* shouldn't happen */
9312 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009313 }
9314
Josef Bacikddfae632017-10-19 14:16:02 -04009315 /*
9316 * We can't call btrfs_truncate_block inside a trans handle as we could
9317 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9318 * we've truncated everything except the last little bit, and can do
9319 * btrfs_truncate_block and then update the disk_i_size.
9320 */
9321 if (ret == NEED_TRUNCATE_BLOCK) {
9322 btrfs_end_transaction(trans);
9323 btrfs_btree_balance_dirty(fs_info);
9324
9325 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9326 if (ret)
9327 goto out;
9328 trans = btrfs_start_transaction(root, 1);
9329 if (IS_ERR(trans)) {
9330 ret = PTR_ERR(trans);
9331 goto out;
9332 }
9333 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9334 }
9335
Yan, Zheng80825102009-11-12 09:35:36 +00009336 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009337 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009338 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009339 if (ret)
9340 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009341 }
9342
Chris Mason917c16b2011-11-08 14:49:59 -05009343 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009344 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009345 ret = btrfs_update_inode(trans, root, inode);
9346 if (ret && !err)
9347 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009348
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009349 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009350 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009351 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009352out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009353 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009354
Josef Bacik3893e332011-01-31 16:03:11 -05009355 if (ret && !err)
9356 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009357
Josef Bacik3893e332011-01-31 16:03:11 -05009358 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009359}
9360
Sven Wegener3b963622008-06-09 21:57:42 -04009361/*
Chris Masond352ac62008-09-29 15:18:18 -04009362 * create a new subvolume directory/inode (helper for the ioctl).
9363 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009364int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009365 struct btrfs_root *new_root,
9366 struct btrfs_root *parent_root,
9367 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009368{
Chris Mason39279cc2007-06-12 06:35:45 -04009369 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009370 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009371 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009372
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009373 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9374 new_dirid, new_dirid,
9375 S_IFDIR | (~current_umask() & S_IRWXUGO),
9376 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009377 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009378 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009379 inode->i_op = &btrfs_dir_inode_operations;
9380 inode->i_fop = &btrfs_dir_file_operations;
9381
Miklos Szeredibfe86842011-10-28 14:13:29 +02009382 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009383 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009384 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009385
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009386 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9387 if (err)
9388 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009389 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009390 new_root->root_key.objectid, err);
9391
Yan, Zheng76dda932009-09-21 16:00:26 -04009392 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009393
Yan, Zheng76dda932009-09-21 16:00:26 -04009394 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009395 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009396}
9397
Chris Mason39279cc2007-06-12 06:35:45 -04009398struct inode *btrfs_alloc_inode(struct super_block *sb)
9399{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009400 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009401 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009402 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009403
David Sterba712e36c2017-10-31 17:08:27 +01009404 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009405 if (!ei)
9406 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009407
9408 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009409 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009410 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009411 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009412 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009413 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009414 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009415 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009416 ei->disk_i_size = 0;
9417 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009418 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009419 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009420 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009421 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009422 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009423 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009424
Josef Bacik9e0baf62011-07-15 15:16:44 +00009425 spin_lock_init(&ei->lock);
9426 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009427 if (sb->s_magic != BTRFS_TEST_MAGIC)
9428 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9429 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009430 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009431 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009432 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009433
Miao Xie16cdcec2011-04-22 18:12:22 +08009434 ei->delayed_node = NULL;
9435
chandan r9cc97d62012-07-04 12:48:07 +05309436 ei->i_otime.tv_sec = 0;
9437 ei->i_otime.tv_nsec = 0;
9438
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009439 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009440 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009441 extent_io_tree_init(&ei->io_tree, inode);
9442 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009443 ei->io_tree.track_uptodate = 1;
9444 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009445 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009446 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009447 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009448 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009449 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009450 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009451 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009452 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009453
9454 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009455}
9456
Josef Bacikaaedb552013-10-11 14:44:09 -04009457#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9458void btrfs_test_destroy_inode(struct inode *inode)
9459{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009460 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009461 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9462}
9463#endif
9464
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009465static void btrfs_i_callback(struct rcu_head *head)
9466{
9467 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009468 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9469}
9470
Chris Mason39279cc2007-06-12 06:35:45 -04009471void btrfs_destroy_inode(struct inode *inode)
9472{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009473 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009474 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009475 struct btrfs_root *root = BTRFS_I(inode)->root;
9476
Al Virob3d9b7a2012-06-09 13:51:19 -04009477 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009478 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009479 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9480 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009481 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009482 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009483 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009484 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009485 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009486
Chris Mason5a3f23d2009-03-31 13:27:11 -04009487 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009488 * This can happen where we create an inode, but somebody else also
9489 * created the same inode and we need to destroy the one we already
9490 * created.
9491 */
9492 if (!root)
9493 goto free;
9494
Josef Bacik8a35d952012-05-23 14:26:42 -04009495 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9496 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009497 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009498 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009499 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009500 }
Josef Bacik7b128762008-07-24 12:17:14 -04009501
Chris Masond3977122009-01-05 21:25:51 -05009502 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009503 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9504 if (!ordered)
9505 break;
9506 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009507 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009508 "found ordered extent %llu %llu on inode cleanup",
9509 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009510 btrfs_remove_ordered_extent(inode, ordered);
9511 btrfs_put_ordered_extent(ordered);
9512 btrfs_put_ordered_extent(ordered);
9513 }
9514 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009515 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009516 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009517 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009518free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009519 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009520}
9521
Al Viro45321ac2010-06-07 13:43:19 -04009522int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009523{
9524 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009525
Naohiro Aota6379ef92013-06-06 09:56:34 +00009526 if (root == NULL)
9527 return 1;
9528
Liu Bofa6ac872013-02-20 14:10:23 +00009529 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009530 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009531 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009532 else
Al Viro45321ac2010-06-07 13:43:19 -04009533 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009534}
9535
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009536static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009537{
9538 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9539
9540 inode_init_once(&ei->vfs_inode);
9541}
9542
9543void btrfs_destroy_cachep(void)
9544{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009545 /*
9546 * Make sure all delayed rcu free inodes are flushed before we
9547 * destroy cache.
9548 */
9549 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009550 kmem_cache_destroy(btrfs_inode_cachep);
9551 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009552 kmem_cache_destroy(btrfs_path_cachep);
9553 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009554}
9555
Liu Bof5c29bd2017-11-02 17:21:50 -06009556int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009557{
David Sterba837e1972012-09-07 03:00:48 -06009558 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009559 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009560 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9561 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009562 if (!btrfs_inode_cachep)
9563 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009564
David Sterba837e1972012-09-07 03:00:48 -06009565 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009566 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009567 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009568 if (!btrfs_trans_handle_cachep)
9569 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009570
David Sterba837e1972012-09-07 03:00:48 -06009571 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009572 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009573 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009574 if (!btrfs_path_cachep)
9575 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009576
David Sterba837e1972012-09-07 03:00:48 -06009577 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009578 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009579 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009580 if (!btrfs_free_space_cachep)
9581 goto fail;
9582
Chris Mason39279cc2007-06-12 06:35:45 -04009583 return 0;
9584fail:
9585 btrfs_destroy_cachep();
9586 return -ENOMEM;
9587}
9588
David Howellsa528d352017-01-31 16:46:22 +00009589static int btrfs_getattr(const struct path *path, struct kstat *stat,
9590 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009591{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009592 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009593 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009594 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009595 u32 bi_flags = BTRFS_I(inode)->flags;
9596
9597 stat->result_mask |= STATX_BTIME;
9598 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9599 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9600 if (bi_flags & BTRFS_INODE_APPEND)
9601 stat->attributes |= STATX_ATTR_APPEND;
9602 if (bi_flags & BTRFS_INODE_COMPRESS)
9603 stat->attributes |= STATX_ATTR_COMPRESSED;
9604 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9605 stat->attributes |= STATX_ATTR_IMMUTABLE;
9606 if (bi_flags & BTRFS_INODE_NODUMP)
9607 stat->attributes |= STATX_ATTR_NODUMP;
9608
9609 stat->attributes_mask |= (STATX_ATTR_APPEND |
9610 STATX_ATTR_COMPRESSED |
9611 STATX_ATTR_IMMUTABLE |
9612 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009613
Chris Mason39279cc2007-06-12 06:35:45 -04009614 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009615 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009616
9617 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009618 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009619 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009620 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009621 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009622 return 0;
9623}
9624
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009625static int btrfs_rename_exchange(struct inode *old_dir,
9626 struct dentry *old_dentry,
9627 struct inode *new_dir,
9628 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009629{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009630 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009631 struct btrfs_trans_handle *trans;
9632 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009633 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009634 struct inode *new_inode = new_dentry->d_inode;
9635 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009636 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009637 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009638 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9639 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009640 u64 old_idx = 0;
9641 u64 new_idx = 0;
9642 u64 root_objectid;
9643 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009644 bool root_log_pinned = false;
9645 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009646
9647 /* we only allow rename subvolume link between subvolumes */
9648 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9649 return -EXDEV;
9650
9651 /* close the race window with snapshot create/destroy ioctl */
9652 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009653 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009654 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009655 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009656
9657 /*
9658 * We want to reserve the absolute worst case amount of items. So if
9659 * both inodes are subvols and we need to unlink them then that would
9660 * require 4 item modifications, but if they are both normal inodes it
9661 * would require 5 item modifications, so we'll assume their normal
9662 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9663 * should cover the worst case number of items we'll modify.
9664 */
9665 trans = btrfs_start_transaction(root, 12);
9666 if (IS_ERR(trans)) {
9667 ret = PTR_ERR(trans);
9668 goto out_notrans;
9669 }
9670
9671 /*
9672 * We need to find a free sequence number both in the source and
9673 * in the destination directory for the exchange.
9674 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009675 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009676 if (ret)
9677 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009678 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009679 if (ret)
9680 goto out_fail;
9681
9682 BTRFS_I(old_inode)->dir_index = 0ULL;
9683 BTRFS_I(new_inode)->dir_index = 0ULL;
9684
9685 /* Reference for the source. */
9686 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9687 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009688 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009689 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009690 btrfs_pin_log_trans(root);
9691 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009692 ret = btrfs_insert_inode_ref(trans, dest,
9693 new_dentry->d_name.name,
9694 new_dentry->d_name.len,
9695 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009696 btrfs_ino(BTRFS_I(new_dir)),
9697 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009698 if (ret)
9699 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009700 }
9701
9702 /* And now for the dest. */
9703 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9704 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009705 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009706 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009707 btrfs_pin_log_trans(dest);
9708 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009709 ret = btrfs_insert_inode_ref(trans, root,
9710 old_dentry->d_name.name,
9711 old_dentry->d_name.len,
9712 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009713 btrfs_ino(BTRFS_I(old_dir)),
9714 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009715 if (ret)
9716 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009717 }
9718
9719 /* Update inode version and ctime/mtime. */
9720 inode_inc_iversion(old_dir);
9721 inode_inc_iversion(new_dir);
9722 inode_inc_iversion(old_inode);
9723 inode_inc_iversion(new_inode);
9724 old_dir->i_ctime = old_dir->i_mtime = ctime;
9725 new_dir->i_ctime = new_dir->i_mtime = ctime;
9726 old_inode->i_ctime = ctime;
9727 new_inode->i_ctime = ctime;
9728
9729 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009730 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9731 BTRFS_I(old_inode), 1);
9732 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9733 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009734 }
9735
9736 /* src is a subvolume */
9737 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9738 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9739 ret = btrfs_unlink_subvol(trans, root, old_dir,
9740 root_objectid,
9741 old_dentry->d_name.name,
9742 old_dentry->d_name.len);
9743 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009744 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9745 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009746 old_dentry->d_name.name,
9747 old_dentry->d_name.len);
9748 if (!ret)
9749 ret = btrfs_update_inode(trans, root, old_inode);
9750 }
9751 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009752 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009753 goto out_fail;
9754 }
9755
9756 /* dest is a subvolume */
9757 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9758 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9759 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9760 root_objectid,
9761 new_dentry->d_name.name,
9762 new_dentry->d_name.len);
9763 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009764 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9765 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009766 new_dentry->d_name.name,
9767 new_dentry->d_name.len);
9768 if (!ret)
9769 ret = btrfs_update_inode(trans, dest, new_inode);
9770 }
9771 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009772 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009773 goto out_fail;
9774 }
9775
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009776 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009777 new_dentry->d_name.name,
9778 new_dentry->d_name.len, 0, old_idx);
9779 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009780 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009781 goto out_fail;
9782 }
9783
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009784 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009785 old_dentry->d_name.name,
9786 old_dentry->d_name.len, 0, new_idx);
9787 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009788 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009789 goto out_fail;
9790 }
9791
9792 if (old_inode->i_nlink == 1)
9793 BTRFS_I(old_inode)->dir_index = old_idx;
9794 if (new_inode->i_nlink == 1)
9795 BTRFS_I(new_inode)->dir_index = new_idx;
9796
Filipe Manana86e8aa02016-05-05 02:02:27 +01009797 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009798 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009799 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9800 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009801 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009802 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009803 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009804 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009805 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009806 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9807 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009808 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009809 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009810 }
9811out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009812 /*
9813 * If we have pinned a log and an error happened, we unpin tasks
9814 * trying to sync the log and force them to fallback to a transaction
9815 * commit if the log currently contains any of the inodes involved in
9816 * this rename operation (to ensure we do not persist a log with an
9817 * inconsistent state for any of these inodes or leading to any
9818 * inconsistencies when replayed). If the transaction was aborted, the
9819 * abortion reason is propagated to userspace when attempting to commit
9820 * the transaction. If the log does not contain any of these inodes, we
9821 * allow the tasks to sync it.
9822 */
9823 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009824 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9825 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9826 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009827 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009828 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009829 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009830
9831 if (root_log_pinned) {
9832 btrfs_end_log_trans(root);
9833 root_log_pinned = false;
9834 }
9835 if (dest_log_pinned) {
9836 btrfs_end_log_trans(dest);
9837 dest_log_pinned = false;
9838 }
9839 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009840 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009841out_notrans:
9842 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009843 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009844 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009845 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009846
9847 return ret;
9848}
9849
9850static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9851 struct btrfs_root *root,
9852 struct inode *dir,
9853 struct dentry *dentry)
9854{
9855 int ret;
9856 struct inode *inode;
9857 u64 objectid;
9858 u64 index;
9859
9860 ret = btrfs_find_free_ino(root, &objectid);
9861 if (ret)
9862 return ret;
9863
9864 inode = btrfs_new_inode(trans, root, dir,
9865 dentry->d_name.name,
9866 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009867 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009868 objectid,
9869 S_IFCHR | WHITEOUT_MODE,
9870 &index);
9871
9872 if (IS_ERR(inode)) {
9873 ret = PTR_ERR(inode);
9874 return ret;
9875 }
9876
9877 inode->i_op = &btrfs_special_inode_operations;
9878 init_special_inode(inode, inode->i_mode,
9879 WHITEOUT_DEV);
9880
9881 ret = btrfs_init_inode_security(trans, inode, dir,
9882 &dentry->d_name);
9883 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009884 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009885
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009886 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9887 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009888 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009889 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009890
9891 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009892out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009893 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009894 if (ret)
9895 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009896 iput(inode);
9897
Filipe Mananac9901612016-05-05 01:41:57 +01009898 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009899}
9900
Chris Mason39279cc2007-06-12 06:35:45 -04009901static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009902 struct inode *new_dir, struct dentry *new_dentry,
9903 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009904{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009905 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009906 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009907 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009908 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9909 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009910 struct inode *new_inode = d_inode(new_dentry);
9911 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009912 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009913 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009914 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009915 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009916 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009917
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009918 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009919 return -EPERM;
9920
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009921 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009922 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009923 return -EXDEV;
9924
Li Zefan33345d012011-04-20 10:31:50 +08009925 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009926 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009927 return -ENOTEMPTY;
9928
Chris Mason39279cc2007-06-12 06:35:45 -04009929 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009930 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009931 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009932
9933
9934 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009935 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009936 new_dentry->d_name.name,
9937 new_dentry->d_name.len);
9938
9939 if (ret) {
9940 if (ret == -EEXIST) {
9941 /* we shouldn't get
9942 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309943 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009944 return ret;
9945 }
9946 } else {
9947 /* maybe -EOVERFLOW */
9948 return ret;
9949 }
9950 }
9951 ret = 0;
9952
Chris Mason5a3f23d2009-03-31 13:27:11 -04009953 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009954 * we're using rename to replace one file with another. Start IO on it
9955 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009956 */
Chris Mason8d875f92014-08-12 10:47:42 -07009957 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009958 filemap_flush(old_inode->i_mapping);
9959
Yan, Zheng76dda932009-09-21 16:00:26 -04009960 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009961 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009962 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009963 /*
9964 * We want to reserve the absolute worst case amount of items. So if
9965 * both inodes are subvols and we need to unlink them then that would
9966 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009967 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009968 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9969 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009970 * If our rename has the whiteout flag, we need more 5 units for the
9971 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9972 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009973 */
Filipe Manana5062af32016-05-05 10:26:26 +01009974 trans_num_items = 11;
9975 if (flags & RENAME_WHITEOUT)
9976 trans_num_items += 5;
9977 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009978 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009979 ret = PTR_ERR(trans);
9980 goto out_notrans;
9981 }
Chris Mason5f39d392007-10-15 16:14:19 -04009982
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009983 if (dest != root)
9984 btrfs_record_root_in_trans(trans, dest);
9985
Nikolay Borisov877574e2017-02-20 13:50:33 +02009986 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009987 if (ret)
9988 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009989
Miao Xie67de1172013-12-26 13:07:06 +08009990 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009991 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009992 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009993 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009994 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009995 btrfs_pin_log_trans(root);
9996 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009997 ret = btrfs_insert_inode_ref(trans, dest,
9998 new_dentry->d_name.name,
9999 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +080010000 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010001 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010002 if (ret)
10003 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010004 }
Chris Mason5a3f23d2009-03-31 13:27:11 -040010005
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010006 inode_inc_iversion(old_dir);
10007 inode_inc_iversion(new_dir);
10008 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010009 old_dir->i_ctime = old_dir->i_mtime =
10010 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010011 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -040010012
Chris Mason12fcfd22009-03-24 10:24:20 -040010013 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +010010014 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10015 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -040010016
Li Zefan33345d012011-04-20 10:31:50 +080010017 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010018 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
10019 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
10020 old_dentry->d_name.name,
10021 old_dentry->d_name.len);
10022 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010023 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10024 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +000010025 old_dentry->d_name.name,
10026 old_dentry->d_name.len);
10027 if (!ret)
10028 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010029 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010030 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010031 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010032 goto out_fail;
10033 }
Chris Mason39279cc2007-06-12 06:35:45 -040010034
10035 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010036 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010037 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010038 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010039 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10040 root_objectid = BTRFS_I(new_inode)->location.objectid;
10041 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10042 root_objectid,
10043 new_dentry->d_name.name,
10044 new_dentry->d_name.len);
10045 BUG_ON(new_inode->i_nlink == 0);
10046 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010047 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10048 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010049 new_dentry->d_name.name,
10050 new_dentry->d_name.len);
10051 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010052 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010053 ret = btrfs_orphan_add(trans,
10054 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010055 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010056 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010057 goto out_fail;
10058 }
Chris Mason39279cc2007-06-12 06:35:45 -040010059 }
Josef Bacikaec74772008-07-24 12:12:38 -040010060
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010061 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010062 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010063 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010064 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010065 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010066 goto out_fail;
10067 }
Chris Mason39279cc2007-06-12 06:35:45 -040010068
Miao Xie67de1172013-12-26 13:07:06 +080010069 if (old_inode->i_nlink == 1)
10070 BTRFS_I(old_inode)->dir_index = index;
10071
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010072 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010073 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010074
David Sterbaf85b7372017-01-20 14:54:07 +010010075 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10076 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010077 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010078 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010079 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010080
10081 if (flags & RENAME_WHITEOUT) {
10082 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10083 old_dentry);
10084
10085 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010086 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010087 goto out_fail;
10088 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010089 }
Chris Mason39279cc2007-06-12 06:35:45 -040010090out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010091 /*
10092 * If we have pinned the log and an error happened, we unpin tasks
10093 * trying to sync the log and force them to fallback to a transaction
10094 * commit if the log currently contains any of the inodes involved in
10095 * this rename operation (to ensure we do not persist a log with an
10096 * inconsistent state for any of these inodes or leading to any
10097 * inconsistencies when replayed). If the transaction was aborted, the
10098 * abortion reason is propagated to userspace when attempting to commit
10099 * the transaction. If the log does not contain any of these inodes, we
10100 * allow the tasks to sync it.
10101 */
10102 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010103 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10104 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10105 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010106 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010107 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010108 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010109
10110 btrfs_end_log_trans(root);
10111 log_pinned = false;
10112 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010113 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010114out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010115 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010116 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010117
Chris Mason39279cc2007-06-12 06:35:45 -040010118 return ret;
10119}
10120
Miklos Szeredi80ace852014-07-23 15:15:32 +020010121static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10122 struct inode *new_dir, struct dentry *new_dentry,
10123 unsigned int flags)
10124{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010125 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010126 return -EINVAL;
10127
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010128 if (flags & RENAME_EXCHANGE)
10129 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10130 new_dentry);
10131
10132 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010133}
10134
Miao Xie8ccf6f192012-10-25 09:28:04 +000010135static void btrfs_run_delalloc_work(struct btrfs_work *work)
10136{
10137 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010138 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010139
10140 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10141 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010142 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010143 filemap_flush(inode->i_mapping);
10144 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10145 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010146 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010147
10148 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010149 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010150 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010151 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010152 complete(&delalloc_work->completion);
10153}
10154
10155struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010156 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010157{
10158 struct btrfs_delalloc_work *work;
10159
David Sterba100d5702015-12-08 14:39:32 +010010160 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010161 if (!work)
10162 return NULL;
10163
10164 init_completion(&work->completion);
10165 INIT_LIST_HEAD(&work->list);
10166 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010167 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010168 WARN_ON_ONCE(!inode);
10169 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10170 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010171
10172 return work;
10173}
10174
10175void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10176{
10177 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010178 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010179}
10180
Chris Masond352ac62008-09-29 15:18:18 -040010181/*
10182 * some fairly slow code that needs optimization. This walks the list
10183 * of all the inodes with pending delalloc and forces them to disk.
10184 */
Miao Xie6c255e62014-03-06 13:55:01 +080010185static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10186 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010187{
Chris Masonea8c2812008-08-04 23:17:27 -040010188 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010189 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010190 struct btrfs_delalloc_work *work, *next;
10191 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010192 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010193 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010194
Miao Xie8ccf6f192012-10-25 09:28:04 +000010195 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010196 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010197
Miao Xie573bfb72014-03-06 13:55:03 +080010198 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010199 spin_lock(&root->delalloc_lock);
10200 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010201 while (!list_empty(&splice)) {
10202 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010203 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010204
Miao Xieeb73c1b2013-05-15 07:48:22 +000010205 list_move_tail(&binode->delalloc_inodes,
10206 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010207 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010208 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010209 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010210 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010211 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010212 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010213
David Sterba651d4942015-11-27 19:24:16 +010010214 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010215 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010216 if (delay_iput)
10217 btrfs_add_delayed_iput(inode);
10218 else
10219 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010220 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010221 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010222 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010223 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010224 btrfs_queue_work(root->fs_info->flush_workers,
10225 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010226 ret++;
10227 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010228 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010229 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010230 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010231 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010232 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010233
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010234out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010235 list_for_each_entry_safe(work, next, &works, list) {
10236 list_del_init(&work->list);
10237 btrfs_wait_and_free_delalloc_work(work);
10238 }
10239
Miao Xieeb73c1b2013-05-15 07:48:22 +000010240 if (!list_empty_careful(&splice)) {
10241 spin_lock(&root->delalloc_lock);
10242 list_splice_tail(&splice, &root->delalloc_inodes);
10243 spin_unlock(&root->delalloc_lock);
10244 }
Miao Xie573bfb72014-03-06 13:55:03 +080010245 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010246 return ret;
10247}
10248
10249int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10250{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010251 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010252 int ret;
10253
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010254 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010255 return -EROFS;
10256
Miao Xie6c255e62014-03-06 13:55:01 +080010257 ret = __start_delalloc_inodes(root, delay_iput, -1);
10258 if (ret > 0)
10259 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010260 return ret;
10261}
10262
Miao Xie6c255e62014-03-06 13:55:01 +080010263int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10264 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010265{
10266 struct btrfs_root *root;
10267 struct list_head splice;
10268 int ret;
10269
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010270 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010271 return -EROFS;
10272
10273 INIT_LIST_HEAD(&splice);
10274
Miao Xie573bfb72014-03-06 13:55:03 +080010275 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010276 spin_lock(&fs_info->delalloc_root_lock);
10277 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010278 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010279 root = list_first_entry(&splice, struct btrfs_root,
10280 delalloc_root);
10281 root = btrfs_grab_fs_root(root);
10282 BUG_ON(!root);
10283 list_move_tail(&root->delalloc_root,
10284 &fs_info->delalloc_roots);
10285 spin_unlock(&fs_info->delalloc_root_lock);
10286
Miao Xie6c255e62014-03-06 13:55:01 +080010287 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010288 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010289 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010290 goto out;
10291
Miao Xie6c255e62014-03-06 13:55:01 +080010292 if (nr != -1) {
10293 nr -= ret;
10294 WARN_ON(nr < 0);
10295 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010296 spin_lock(&fs_info->delalloc_root_lock);
10297 }
10298 spin_unlock(&fs_info->delalloc_root_lock);
10299
Miao Xie6c255e62014-03-06 13:55:01 +080010300 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010301out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010302 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010303 spin_lock(&fs_info->delalloc_root_lock);
10304 list_splice_tail(&splice, &fs_info->delalloc_roots);
10305 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010306 }
Miao Xie573bfb72014-03-06 13:55:03 +080010307 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010308 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010309}
10310
Chris Mason39279cc2007-06-12 06:35:45 -040010311static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10312 const char *symname)
10313{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010314 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010315 struct btrfs_trans_handle *trans;
10316 struct btrfs_root *root = BTRFS_I(dir)->root;
10317 struct btrfs_path *path;
10318 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010319 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010320 int err;
10321 int drop_inode = 0;
10322 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010323 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010324 int name_len;
10325 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010326 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010327 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010328 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010329
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010330 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010331 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010332 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010333
Josef Bacik9ed74f22009-09-11 16:12:44 -040010334 /*
10335 * 2 items for inode item and ref
10336 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010337 * 1 item for updating parent inode item
10338 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010339 * 1 item for xattr if selinux is on
10340 */
Filipe Manana9269d122015-12-31 18:16:29 +000010341 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010342 if (IS_ERR(trans))
10343 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010344
Li Zefan581bb052011-04-20 10:06:11 +080010345 err = btrfs_find_free_ino(root, &objectid);
10346 if (err)
10347 goto out_unlock;
10348
Josef Bacikaec74772008-07-24 12:12:38 -040010349 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010350 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10351 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010352 if (IS_ERR(inode)) {
10353 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010354 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010355 }
Chris Mason39279cc2007-06-12 06:35:45 -040010356
Casey Schauflerad19db72011-12-15 10:09:07 -050010357 /*
10358 * If the active LSM wants to access the inode during
10359 * d_instantiate it needs these. Smack checks to see
10360 * if the filesystem supports xattrs by looking at the
10361 * ops vector.
10362 */
10363 inode->i_fop = &btrfs_file_operations;
10364 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010365 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010366 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10367
10368 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10369 if (err)
10370 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010371
Chris Mason39279cc2007-06-12 06:35:45 -040010372 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010373 if (!path) {
10374 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010375 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010376 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010377 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010378 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010379 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010380 datasize = btrfs_file_extent_calc_inline_size(name_len);
10381 err = btrfs_insert_empty_item(trans, root, path, &key,
10382 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010383 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010384 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010385 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010386 }
Chris Mason5f39d392007-10-15 16:14:19 -040010387 leaf = path->nodes[0];
10388 ei = btrfs_item_ptr(leaf, path->slots[0],
10389 struct btrfs_file_extent_item);
10390 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10391 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010392 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010393 btrfs_set_file_extent_encryption(leaf, ei, 0);
10394 btrfs_set_file_extent_compression(leaf, ei, 0);
10395 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10396 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10397
Chris Mason39279cc2007-06-12 06:35:45 -040010398 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010399 write_extent_buffer(leaf, symname, ptr, name_len);
10400 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010401 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010402
Chris Mason39279cc2007-06-12 06:35:45 -040010403 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010404 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010405 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010406 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010407 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010408 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010409 /*
10410 * Last step, add directory indexes for our symlink inode. This is the
10411 * last step to avoid extra cleanup of these indexes if an error happens
10412 * elsewhere above.
10413 */
10414 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010415 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10416 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010417 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010418 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010419 goto out_unlock_inode;
10420 }
10421
10422 unlock_new_inode(inode);
10423 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010424
10425out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010426 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010427 if (drop_inode) {
10428 inode_dec_link_count(inode);
10429 iput(inode);
10430 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010431 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010432 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010433
10434out_unlock_inode:
10435 drop_inode = 1;
10436 unlock_new_inode(inode);
10437 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010438}
Chris Mason16432982008-04-10 10:23:21 -040010439
Josef Bacik0af3d002010-06-21 14:48:16 -040010440static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10441 u64 start, u64 num_bytes, u64 min_size,
10442 loff_t actual_len, u64 *alloc_hint,
10443 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010444{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010445 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010446 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10447 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010448 struct btrfs_root *root = BTRFS_I(inode)->root;
10449 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010450 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010451 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010452 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010453 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010454 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010455 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010456 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010457
Josef Bacik0af3d002010-06-21 14:48:16 -040010458 if (trans)
10459 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010460 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010461 if (own_trans) {
10462 trans = btrfs_start_transaction(root, 3);
10463 if (IS_ERR(trans)) {
10464 ret = PTR_ERR(trans);
10465 break;
10466 }
Yan Zhengd899e052008-10-30 14:25:28 -040010467 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010468
Byongho Leeee221842015-12-15 01:42:10 +090010469 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010470 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010471 /*
10472 * If we are severely fragmented we could end up with really
10473 * small allocations, so if the allocator is returning small
10474 * chunks lets make its job easier by only searching for those
10475 * sized chunks.
10476 */
10477 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010478 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10479 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010480 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010481 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010482 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010483 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010484 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010485 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010486
Josef Bacik0b670dc2015-09-23 17:11:16 -040010487 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010488 ret = insert_reserved_file_extent(trans, inode,
10489 cur_offset, ins.objectid,
10490 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010491 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010492 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010493 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010494 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010495 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010496 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010497 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010498 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010499 break;
10500 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010501
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010502 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010503 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010504
Josef Bacik5dc562c2012-08-17 13:14:17 -040010505 em = alloc_extent_map();
10506 if (!em) {
10507 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10508 &BTRFS_I(inode)->runtime_flags);
10509 goto next;
10510 }
10511
10512 em->start = cur_offset;
10513 em->orig_start = cur_offset;
10514 em->len = ins.offset;
10515 em->block_start = ins.objectid;
10516 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010517 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010518 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010519 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010520 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10521 em->generation = trans->transid;
10522
10523 while (1) {
10524 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010525 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010526 write_unlock(&em_tree->lock);
10527 if (ret != -EEXIST)
10528 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010529 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010530 cur_offset + ins.offset - 1,
10531 0);
10532 }
10533 free_extent_map(em);
10534next:
Yan Zhengd899e052008-10-30 14:25:28 -040010535 num_bytes -= ins.offset;
10536 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010537 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010538
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010539 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010540 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010541 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010542 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010543 (actual_len > inode->i_size) &&
10544 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010545 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010546 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010547 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010548 i_size = cur_offset;
10549 i_size_write(inode, i_size);
10550 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010551 }
10552
Yan Zhengd899e052008-10-30 14:25:28 -040010553 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010554
10555 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010556 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010557 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010558 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010559 break;
10560 }
Yan Zhengd899e052008-10-30 14:25:28 -040010561
Josef Bacik0af3d002010-06-21 14:48:16 -040010562 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010563 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010564 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010565 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010566 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010567 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010568 return ret;
10569}
10570
Josef Bacik0af3d002010-06-21 14:48:16 -040010571int btrfs_prealloc_file_range(struct inode *inode, int mode,
10572 u64 start, u64 num_bytes, u64 min_size,
10573 loff_t actual_len, u64 *alloc_hint)
10574{
10575 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10576 min_size, actual_len, alloc_hint,
10577 NULL);
10578}
10579
10580int btrfs_prealloc_file_range_trans(struct inode *inode,
10581 struct btrfs_trans_handle *trans, int mode,
10582 u64 start, u64 num_bytes, u64 min_size,
10583 loff_t actual_len, u64 *alloc_hint)
10584{
10585 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10586 min_size, actual_len, alloc_hint, trans);
10587}
10588
Chris Masone6dcd2d2008-07-17 12:53:50 -040010589static int btrfs_set_page_dirty(struct page *page)
10590{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010591 return __set_page_dirty_nobuffers(page);
10592}
10593
Al Viro10556cb2011-06-20 19:28:19 -040010594static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010595{
Li Zefanb83cc962010-12-20 16:04:08 +080010596 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010597 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010598
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010599 if (mask & MAY_WRITE &&
10600 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10601 if (btrfs_root_readonly(root))
10602 return -EROFS;
10603 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10604 return -EACCES;
10605 }
Al Viro2830ba72011-06-20 19:16:29 -040010606 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010607}
Chris Mason39279cc2007-06-12 06:35:45 -040010608
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010609static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10610{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010611 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010612 struct btrfs_trans_handle *trans;
10613 struct btrfs_root *root = BTRFS_I(dir)->root;
10614 struct inode *inode = NULL;
10615 u64 objectid;
10616 u64 index;
10617 int ret = 0;
10618
10619 /*
10620 * 5 units required for adding orphan entry
10621 */
10622 trans = btrfs_start_transaction(root, 5);
10623 if (IS_ERR(trans))
10624 return PTR_ERR(trans);
10625
10626 ret = btrfs_find_free_ino(root, &objectid);
10627 if (ret)
10628 goto out;
10629
10630 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010631 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010632 if (IS_ERR(inode)) {
10633 ret = PTR_ERR(inode);
10634 inode = NULL;
10635 goto out;
10636 }
10637
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010638 inode->i_fop = &btrfs_file_operations;
10639 inode->i_op = &btrfs_file_inode_operations;
10640
10641 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010642 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10643
Chris Masonb0d5d102014-09-08 13:08:51 -070010644 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10645 if (ret)
10646 goto out_inode;
10647
10648 ret = btrfs_update_inode(trans, root, inode);
10649 if (ret)
10650 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010651 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010652 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010653 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010654
Filipe Manana5762b5c2014-08-01 00:10:32 +010010655 /*
10656 * We set number of links to 0 in btrfs_new_inode(), and here we set
10657 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10658 * through:
10659 *
10660 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10661 */
10662 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010663 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010664 d_tmpfile(dentry, inode);
10665 mark_inode_dirty(inode);
10666
10667out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010668 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010669 if (ret)
10670 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010671 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010672 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010673
10674out_inode:
10675 unlock_new_inode(inode);
10676 goto out;
10677
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010678}
10679
David Sterba20a7db82017-02-17 16:24:29 +010010680__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010681static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010682{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010683 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010684}
10685
Josef Bacikc6100a42017-05-05 11:57:13 -040010686static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10687{
10688 struct inode *inode = private_data;
10689 return btrfs_sb(inode->i_sb);
10690}
10691
10692static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10693 u64 start, u64 end)
10694{
10695 struct inode *inode = private_data;
10696 u64 isize;
10697
10698 isize = i_size_read(inode);
10699 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10700 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10701 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10702 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10703 }
10704}
10705
10706void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10707{
10708 struct inode *inode = private_data;
10709 unsigned long index = start >> PAGE_SHIFT;
10710 unsigned long end_index = end >> PAGE_SHIFT;
10711 struct page *page;
10712
10713 while (index <= end_index) {
10714 page = find_get_page(inode->i_mapping, index);
10715 ASSERT(page); /* Pages should be in the extent_io_tree */
10716 set_page_writeback(page);
10717 put_page(page);
10718 index++;
10719 }
10720}
10721
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010722static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010723 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010724 .lookup = btrfs_lookup,
10725 .create = btrfs_create,
10726 .unlink = btrfs_unlink,
10727 .link = btrfs_link,
10728 .mkdir = btrfs_mkdir,
10729 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010730 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010731 .symlink = btrfs_symlink,
10732 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010733 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010734 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010735 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010736 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010737 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010738 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010739 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010740};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010741static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010742 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010743 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010744 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010745};
Yan, Zheng76dda932009-09-21 16:00:26 -040010746
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010747static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010748 .llseek = generic_file_llseek,
10749 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010750 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010751 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010752 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010753#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010754 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010755#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010756 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010757 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010758};
10759
David Sterba20e55062015-11-19 11:42:28 +010010760static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010761 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010762 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010763 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010764 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010765 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010766 .tree_fs_info = iotree_fs_info,
10767 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010768
10769 /* optional callbacks */
10770 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010771 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010772 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010773 .set_bit_hook = btrfs_set_bit_hook,
10774 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010775 .merge_extent_hook = btrfs_merge_extent_hook,
10776 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010777 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010778};
10779
Chris Mason35054392009-01-21 13:11:13 -050010780/*
10781 * btrfs doesn't support the bmap operation because swapfiles
10782 * use bmap to make a mapping of extents in the file. They assume
10783 * these extents won't change over the life of the file and they
10784 * use the bmap result to do IO directly to the drive.
10785 *
10786 * the btrfs bmap call would return logical addresses that aren't
10787 * suitable for IO and they also will change frequently as COW
10788 * operations happen. So, swapfile + btrfs == corruption.
10789 *
10790 * For now we're avoiding this by dropping bmap.
10791 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010792static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010793 .readpage = btrfs_readpage,
10794 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010795 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010796 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010797 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010798 .invalidatepage = btrfs_invalidatepage,
10799 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010800 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010801 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010802};
10803
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010804static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010805 .readpage = btrfs_readpage,
10806 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010807 .invalidatepage = btrfs_invalidatepage,
10808 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010809};
10810
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010811static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010812 .getattr = btrfs_getattr,
10813 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010814 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010815 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010816 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010817 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010818 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010819 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010820};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010821static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010822 .getattr = btrfs_getattr,
10823 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010824 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010825 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010826 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010827 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010828 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010829};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010830static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010831 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010832 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010833 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010834 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010835 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010836 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010837};
Yan, Zheng76dda932009-09-21 16:00:26 -040010838
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010839const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010840 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010841 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010842};