blob: 1131db7a0b2871352ac595fd422c987dee27f79a [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040045#include <linux/magic.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080063#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040064
65struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080066 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040067 struct btrfs_root *root;
68};
69
Filipe Mananaf28a4922015-12-08 19:23:20 +000070struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000071 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040089struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050090struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040091
92#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010093static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040094 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
95 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
96 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
97 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
98 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
99 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
100 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
101};
102
Eric Sandeen3972f262013-01-12 02:57:22 +0000103static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500104static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400105static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500106static noinline int cow_file_range(struct inode *inode,
107 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800108 u64 start, u64 end, u64 delalloc_end,
109 int *page_started, unsigned long *nr_written,
110 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800111static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
112 u64 orig_start, u64 block_start,
113 u64 block_len, u64 orig_block_len,
114 u64 ram_bytes, int compress_type,
115 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400116
Qu Wenruo524272602017-03-08 10:25:52 +0800117static void __endio_write_update_ordered(struct inode *inode,
118 const u64 offset, const u64 bytes,
119 const bool uptodate);
120
121/*
122 * Cleanup all submitted ordered extents in specified range to handle errors
123 * from the fill_dellaloc() callback.
124 *
125 * NOTE: caller must ensure that when an error happens, it can not call
126 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
127 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
128 * to be released, which we want to happen only when finishing the ordered
129 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
130 * fill_delalloc() callback already does proper cleanup for the first page of
131 * the range, that is, it invokes the callback writepage_end_io_hook() for the
132 * range of the first page.
133 */
134static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
135 const u64 offset,
136 const u64 bytes)
137{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900138 unsigned long index = offset >> PAGE_SHIFT;
139 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
140 struct page *page;
141
142 while (index <= end_index) {
143 page = find_get_page(inode->i_mapping, index);
144 index++;
145 if (!page)
146 continue;
147 ClearPagePrivate2(page);
148 put_page(page);
149 }
Qu Wenruo524272602017-03-08 10:25:52 +0800150 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
151 bytes - PAGE_SIZE, false);
152}
153
Eric Sandeen48a3b632013-04-25 20:41:01 +0000154static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400155
Josef Bacik6a3891c2015-03-16 17:38:52 -0400156#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
157void btrfs_test_inode_set_ops(struct inode *inode)
158{
159 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
160}
161#endif
162
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000163static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500164 struct inode *inode, struct inode *dir,
165 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500166{
167 int err;
168
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000169 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500170 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500171 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500172 return err;
173}
174
Chris Masond352ac62008-09-29 15:18:18 -0400175/*
Chris Masonc8b97812008-10-29 14:49:59 -0400176 * this does all the hard work for inserting an inline extent into
177 * the btree. The caller should have done a btrfs_drop_extents so that
178 * no overlapping inline items exist in the btree
179 */
Chris Mason40f76582014-05-21 13:35:51 -0700180static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000181 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400182 struct btrfs_root *root, struct inode *inode,
183 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000184 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400185 struct page **compressed_pages)
186{
Chris Masonc8b97812008-10-29 14:49:59 -0400187 struct extent_buffer *leaf;
188 struct page *page = NULL;
189 char *kaddr;
190 unsigned long ptr;
191 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400192 int ret;
193 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400194 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400195
Li Zefanfe3f5662011-03-28 08:30:38 +0000196 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400198
Chris Masonc8b97812008-10-29 14:49:59 -0400199 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000200
201 if (!extent_inserted) {
202 struct btrfs_key key;
203 size_t datasize;
204
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200205 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200207 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000208
209 datasize = btrfs_file_extent_calc_inline_size(cur_size);
210 path->leave_spinning = 1;
211 ret = btrfs_insert_empty_item(trans, root, path, &key,
212 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200213 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000214 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400215 }
216 leaf = path->nodes[0];
217 ei = btrfs_item_ptr(leaf, path->slots[0],
218 struct btrfs_file_extent_item);
219 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
220 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
221 btrfs_set_file_extent_encryption(leaf, ei, 0);
222 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
223 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
224 ptr = btrfs_file_extent_inline_start(ei);
225
Li Zefan261507a02010-12-17 14:21:50 +0800226 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400227 struct page *cpage;
228 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500229 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400230 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500231 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300232 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400233
Cong Wang7ac687d2011-11-25 23:14:28 +0800234 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800236 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400237
238 i++;
239 ptr += cur_size;
240 compressed_size -= cur_size;
241 }
242 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800243 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400244 } else {
245 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300246 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800248 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300249 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400250 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800251 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300252 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400253 }
254 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000255 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400256
Yan, Zhengc2167752009-11-12 09:34:21 +0000257 /*
258 * we're an inline extent, so nobody can
259 * extend the file past i_size without locking
260 * a page we already have locked.
261 *
262 * We must do any isize and inode updates
263 * before we unlock the pages. Otherwise we
264 * could end up racing with unlink.
265 */
Chris Masonc8b97812008-10-29 14:49:59 -0400266 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000268
Chris Masonc8b97812008-10-29 14:49:59 -0400269fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200270 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400271}
272
273
274/*
275 * conditionally insert an inline extent into the file. This
276 * does the checks required to make sure the data is small enough
277 * to fit as an inline extent.
278 */
Josef Bacik00361582013-08-14 14:02:47 -0400279static noinline int cow_file_range_inline(struct btrfs_root *root,
280 struct inode *inode, u64 start,
281 u64 end, size_t compressed_size,
282 int compress_type,
283 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400284{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400285 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400286 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400287 u64 isize = i_size_read(inode);
288 u64 actual_end = min(end + 1, isize);
289 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400291 u64 data_len = inline_len;
292 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000293 struct btrfs_path *path;
294 int extent_inserted = 0;
295 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400296
297 if (compressed_size)
298 data_len = compressed_size;
299
300 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400301 actual_end > fs_info->sectorsize ||
302 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400303 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400305 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400306 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400307 return 1;
308 }
309
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 path = btrfs_alloc_path();
311 if (!path)
312 return -ENOMEM;
313
Josef Bacik00361582013-08-14 14:02:47 -0400314 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000315 if (IS_ERR(trans)) {
316 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400317 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000318 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400319 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400320
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000321 if (compressed_size && compressed_pages)
322 extent_item_size = btrfs_file_extent_calc_inline_size(
323 compressed_size);
324 else
325 extent_item_size = btrfs_file_extent_calc_inline_size(
326 inline_len);
327
328 ret = __btrfs_drop_extents(trans, root, inode, path,
329 start, aligned_end, NULL,
330 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400331 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400332 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400333 goto out;
334 }
Chris Masonc8b97812008-10-29 14:49:59 -0400335
336 if (isize > actual_end)
337 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000338 ret = insert_inline_extent(trans, path, extent_inserted,
339 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400340 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000341 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400342 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400343 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400344 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400345 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400346 ret = 1;
347 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400349
Josef Bacikbdc20e62013-02-28 13:23:38 -0500350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200351 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400352out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800353 /*
354 * Don't forget to free the reserved space, as for inlined extent
355 * it won't count as data extent, free them directly here.
356 * And at reserve time, it's always aligned to page size, so
357 * just free one page here.
358 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800359 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000360 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400361 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400362 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400363}
364
Chris Mason771ed682008-11-06 22:02:51 -0500365struct async_extent {
366 u64 start;
367 u64 ram_size;
368 u64 compressed_size;
369 struct page **pages;
370 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800371 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500372 struct list_head list;
373};
374
375struct async_cow {
376 struct inode *inode;
377 struct btrfs_root *root;
378 struct page *locked_page;
379 u64 start;
380 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600381 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500382 struct list_head extents;
383 struct btrfs_work work;
384};
385
386static noinline int add_async_extent(struct async_cow *cow,
387 u64 start, u64 ram_size,
388 u64 compressed_size,
389 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800390 unsigned long nr_pages,
391 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500392{
393 struct async_extent *async_extent;
394
395 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100396 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500397 async_extent->start = start;
398 async_extent->ram_size = ram_size;
399 async_extent->compressed_size = compressed_size;
400 async_extent->pages = pages;
401 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800402 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500403 list_add_tail(&async_extent->list, &cow->extents);
404 return 0;
405}
406
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300407static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400409 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800410
411 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400412 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800413 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200414 /* defrag ioctl */
415 if (BTRFS_I(inode)->defrag_compress)
416 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800417 /* bad compression ratios */
418 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
419 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400420 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800421 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200422 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300423 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800424 return 0;
425}
426
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200427static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800428 u64 start, u64 end, u64 num_bytes, u64 small_write)
429{
430 /* If this is a small write inside eof, kick off a defrag */
431 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200432 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800433 btrfs_add_inode_defrag(NULL, inode);
434}
435
Chris Masonc8b97812008-10-29 14:49:59 -0400436/*
Chris Mason771ed682008-11-06 22:02:51 -0500437 * we create compressed extents in two phases. The first
438 * phase compresses a range of pages that have already been
439 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400440 *
Chris Mason771ed682008-11-06 22:02:51 -0500441 * This is done inside an ordered work queue, and the compression
442 * is spread across many cpus. The actual IO submission is step
443 * two, and the ordered work queue takes care of making sure that
444 * happens in the same order things were put onto the queue by
445 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400446 *
Chris Mason771ed682008-11-06 22:02:51 -0500447 * If this code finds it can't get good compression, it puts an
448 * entry onto the work queue to write the uncompressed bytes. This
449 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300450 * are written in the same order that the flusher thread sent them
451 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400452 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100453static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500454 struct page *locked_page,
455 u64 start, u64 end,
456 struct async_cow *async_cow,
457 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400458{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400459 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400460 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400461 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400462 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500463 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400464 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400465 struct page **pages = NULL;
466 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400467 unsigned long total_compressed = 0;
468 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400469 int i;
470 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400471 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400472 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400473
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200474 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
475 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400476
Chris Mason42dc7ba2008-12-15 11:44:56 -0500477 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400478again:
479 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300480 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100481 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
482 nr_pages = min_t(unsigned long, nr_pages,
483 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400484
Chris Masonf03d9301f2009-02-04 09:31:06 -0500485 /*
486 * we don't want to send crud past the end of i_size through
487 * compression, that's just a waste of CPU time. So, if the
488 * end of the file is before the start of our current
489 * requested range of bytes, we bail out to the uncompressed
490 * cleanup code that can deal with all of this.
491 *
492 * It isn't really the fastest way to fix things, but this is a
493 * very uncommon corner.
494 */
495 if (actual_end <= start)
496 goto cleanup_and_bail_uncompressed;
497
Chris Masonc8b97812008-10-29 14:49:59 -0400498 total_compressed = actual_end - start;
499
Shilong Wang4bcbb332014-10-07 18:44:35 -0400500 /*
501 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400502 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400503 */
504 if (total_compressed <= blocksize &&
505 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
506 goto cleanup_and_bail_uncompressed;
507
David Sterba069eac72017-02-14 19:36:54 +0100508 total_compressed = min_t(unsigned long, total_compressed,
509 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400510 total_in = 0;
511 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400512
Chris Mason771ed682008-11-06 22:02:51 -0500513 /*
514 * we do compression for mount -o compress and when the
515 * inode has not been flagged as nocompress. This flag can
516 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400517 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300518 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400519 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100520 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800521 if (!pages) {
522 /* just bail out to the uncompressed code */
523 goto cont;
524 }
Chris Mason179e29e2007-11-01 11:28:41 -0400525
David Sterbaeec63c62017-07-17 19:41:31 +0200526 if (BTRFS_I(inode)->defrag_compress)
527 compress_type = BTRFS_I(inode)->defrag_compress;
528 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200529 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800530
Chris Mason4adaa612013-03-26 13:07:00 -0400531 /*
532 * we need to call clear_page_dirty_for_io on each
533 * page in the range. Otherwise applications with the file
534 * mmap'd can wander in and change the page contents while
535 * we are compressing them.
536 *
537 * If the compression fails for any reason, we set the pages
538 * dirty again later on.
539 */
540 extent_range_clear_dirty_for_io(inode, start, end);
541 redirty = 1;
David Sterbaf51d2b52017-09-15 17:36:57 +0200542
543 /* Compression level is applied here and only here */
544 ret = btrfs_compress_pages(
545 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800546 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100547 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100548 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800549 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100550 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400551
552 if (!ret) {
553 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300554 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100555 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400556 char *kaddr;
557
558 /* zero the tail end of the last page, we might be
559 * sending it down to disk
560 */
561 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800562 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400563 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300564 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800565 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400566 }
567 will_compress = 1;
568 }
569 }
Li Zefan560f7d72011-09-08 10:22:01 +0800570cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400571 if (start == 0) {
572 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300573 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400574 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500575 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400576 */
Josef Bacik00361582013-08-14 14:02:47 -0400577 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800578 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400579 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500580 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400581 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000582 total_compressed,
583 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400584 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100585 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400586 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400587 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
588 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100589 unsigned long page_error_op;
590
Filipe Mananae6eb4312014-10-10 10:45:12 +0100591 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400592
Chris Mason771ed682008-11-06 22:02:51 -0500593 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100594 * inline extent creation worked or returned error,
595 * we don't need to create any more async work items.
596 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400597 *
598 * We use DO_ACCOUNTING here because we need the
599 * delalloc_release_metadata to be done _after_ we drop
600 * our outstanding extent for clearing delalloc for this
601 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500602 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800603 extent_clear_unlock_delalloc(inode, start, end, end,
604 NULL, clear_flags,
605 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400606 PAGE_CLEAR_DIRTY |
607 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100608 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400609 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400610 goto free_pages_out;
611 }
612 }
613
614 if (will_compress) {
615 /*
616 * we aren't doing an inline extent round the compressed size
617 * up to a block size boundary so the allocator does sane
618 * things
619 */
Qu Wenruofda28322013-02-26 08:10:22 +0000620 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400621
622 /*
623 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300624 * win, compare the page count read with the blocks on disk,
625 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400626 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300627 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300628 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700629 *num_added += 1;
630
631 /*
632 * The async work queues will take care of doing actual
633 * allocation on disk for these compressed pages, and
634 * will submit them to the elevator.
635 */
Timofey Titovets11708622017-10-03 18:06:01 +0300636 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100637 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700638 compress_type);
639
Timofey Titovets11708622017-10-03 18:06:01 +0300640 if (start + total_in < end) {
641 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700642 pages = NULL;
643 cond_resched();
644 goto again;
645 }
646 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400647 }
648 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700649 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400650 /*
651 * the compression code ran but failed to make things smaller,
652 * free any pages it allocated and our page pointer array
653 */
David Sterba4d3a8002017-02-14 19:04:07 +0100654 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400655 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300656 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400657 }
658 kfree(pages);
659 pages = NULL;
660 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100661 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400662
663 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400664 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200665 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500666 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500667 }
Chris Masonc8b97812008-10-29 14:49:59 -0400668 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500669cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700670 /*
671 * No compression, but we still need to write the pages in the file
672 * we've been given so far. redirty the locked page if it corresponds
673 * to our extent and set things up for the async work queue to run
674 * cow_file_range to do the normal delalloc dance.
675 */
676 if (page_offset(locked_page) >= start &&
677 page_offset(locked_page) <= end)
678 __set_page_dirty_nobuffers(locked_page);
679 /* unlocked later on in the async handlers */
680
681 if (redirty)
682 extent_range_redirty_for_io(inode, start, end);
683 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
684 BTRFS_COMPRESS_NONE);
685 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500686
Filipe Mananac44f6492014-10-09 21:15:44 +0100687 return;
Chris Mason771ed682008-11-06 22:02:51 -0500688
689free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100690 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500691 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300692 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500693 }
Chris Masond3977122009-01-05 21:25:51 -0500694 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500695}
Chris Mason771ed682008-11-06 22:02:51 -0500696
Filipe Manana40ae8372014-10-06 22:14:24 +0100697static void free_async_extent_pages(struct async_extent *async_extent)
698{
699 int i;
700
701 if (!async_extent->pages)
702 return;
703
704 for (i = 0; i < async_extent->nr_pages; i++) {
705 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300706 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100707 }
708 kfree(async_extent->pages);
709 async_extent->nr_pages = 0;
710 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500711}
712
713/*
714 * phase two of compressed writeback. This is the ordered portion
715 * of the code, which only gets called in the order the work was
716 * queued. We walk all the async extents created by compress_file_range
717 * and send them down to the disk.
718 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100719static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500720 struct async_cow *async_cow)
721{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400722 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500723 struct async_extent *async_extent;
724 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500725 struct btrfs_key ins;
726 struct extent_map *em;
727 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500728 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500729 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500730
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500731again:
Chris Masond3977122009-01-05 21:25:51 -0500732 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500733 async_extent = list_entry(async_cow->extents.next,
734 struct async_extent, list);
735 list_del(&async_extent->list);
736
737 io_tree = &BTRFS_I(inode)->io_tree;
738
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500739retry:
Chris Mason771ed682008-11-06 22:02:51 -0500740 /* did the compression code fall back to uncompressed IO? */
741 if (!async_extent->pages) {
742 int page_started = 0;
743 unsigned long nr_written = 0;
744
745 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000746 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100747 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500748
749 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500750 ret = cow_file_range(inode, async_cow->locked_page,
751 async_extent->start,
752 async_extent->start +
753 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800754 async_extent->start +
755 async_extent->ram_size - 1,
756 &page_started, &nr_written, 0,
757 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500758
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100759 /* JDM XXX */
760
Chris Mason771ed682008-11-06 22:02:51 -0500761 /*
762 * if page_started, cow_file_range inserted an
763 * inline extent and took care of all the unlocking
764 * and IO for us. Otherwise, we need to submit
765 * all those pages down to the drive.
766 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500767 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500768 extent_write_locked_range(io_tree,
769 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500770 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500771 async_extent->ram_size - 1,
772 btrfs_get_extent,
773 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500774 else if (ret)
775 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500776 kfree(async_extent);
777 cond_resched();
778 continue;
779 }
780
781 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100782 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500783
Wang Xiaoguang18513092016-07-25 15:51:40 +0800784 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500785 async_extent->compressed_size,
786 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800787 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500788 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100789 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500790
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400791 if (ret == -ENOSPC) {
792 unlock_extent(io_tree, async_extent->start,
793 async_extent->start +
794 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800795
796 /*
797 * we need to redirty the pages if we decide to
798 * fallback to uncompressed IO, otherwise we
799 * will not submit these pages down to lower
800 * layers.
801 */
802 extent_range_redirty_for_io(inode,
803 async_extent->start,
804 async_extent->start +
805 async_extent->ram_size - 1);
806
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100807 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400808 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500809 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500810 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000811 /*
812 * here we're doing allocation and writeback of the
813 * compressed pages
814 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800815 em = create_io_em(inode, async_extent->start,
816 async_extent->ram_size, /* len */
817 async_extent->start, /* orig_start */
818 ins.objectid, /* block_start */
819 ins.offset, /* block_len */
820 ins.offset, /* orig_block_len */
821 async_extent->ram_size, /* ram_bytes */
822 async_extent->compress_type,
823 BTRFS_ORDERED_COMPRESSED);
824 if (IS_ERR(em))
825 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500826 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800827 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500828
Li Zefan261507a02010-12-17 14:21:50 +0800829 ret = btrfs_add_ordered_extent_compress(inode,
830 async_extent->start,
831 ins.objectid,
832 async_extent->ram_size,
833 ins.offset,
834 BTRFS_ORDERED_COMPRESSED,
835 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100836 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200837 btrfs_drop_extent_cache(BTRFS_I(inode),
838 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100839 async_extent->start +
840 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500841 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100842 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400843 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500844
Chris Mason771ed682008-11-06 22:02:51 -0500845 /*
846 * clear dirty, set writeback and unlock the pages.
847 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400848 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400849 async_extent->start +
850 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800851 async_extent->start +
852 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400853 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
854 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400855 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200856 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500857 async_extent->start,
858 async_extent->ram_size,
859 ins.objectid,
860 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600861 async_extent->nr_pages,
862 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100863 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
864 struct page *p = async_extent->pages[0];
865 const u64 start = async_extent->start;
866 const u64 end = start + async_extent->ram_size - 1;
867
868 p->mapping = inode->i_mapping;
869 tree->ops->writepage_end_io_hook(p, start, end,
870 NULL, 0);
871 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800872 extent_clear_unlock_delalloc(inode, start, end, end,
873 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100874 PAGE_END_WRITEBACK |
875 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100876 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100877 }
Chris Mason771ed682008-11-06 22:02:51 -0500878 alloc_hint = ins.objectid + ins.offset;
879 kfree(async_extent);
880 cond_resched();
881 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100882 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500883out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400884 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400885 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100886out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400887 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500888 async_extent->start +
889 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800890 async_extent->start +
891 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400892 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100893 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400894 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
895 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100896 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
897 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100898 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100899 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500900 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500901}
902
Josef Bacik4b46fce2010-05-23 11:00:55 -0400903static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
904 u64 num_bytes)
905{
906 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
907 struct extent_map *em;
908 u64 alloc_hint = 0;
909
910 read_lock(&em_tree->lock);
911 em = search_extent_mapping(em_tree, start, num_bytes);
912 if (em) {
913 /*
914 * if block start isn't an actual block number then find the
915 * first block in this inode and use that as a hint. If that
916 * block is also bogus then just don't worry about it.
917 */
918 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
919 free_extent_map(em);
920 em = search_extent_mapping(em_tree, 0, 0);
921 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
922 alloc_hint = em->block_start;
923 if (em)
924 free_extent_map(em);
925 } else {
926 alloc_hint = em->block_start;
927 free_extent_map(em);
928 }
929 }
930 read_unlock(&em_tree->lock);
931
932 return alloc_hint;
933}
934
Chris Mason771ed682008-11-06 22:02:51 -0500935/*
936 * when extent_io.c finds a delayed allocation range in the file,
937 * the call backs end up in this code. The basic idea is to
938 * allocate extents on disk for the range, and create ordered data structs
939 * in ram to track those extents.
940 *
941 * locked_page is the page that writepage had locked already. We use
942 * it to make sure we don't do extra locks or unlocks.
943 *
944 * *page_started is set to one if we unlock locked_page and do everything
945 * required to start IO on it. It may be clean and already done with
946 * IO when we return.
947 */
Josef Bacik00361582013-08-14 14:02:47 -0400948static noinline int cow_file_range(struct inode *inode,
949 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800950 u64 start, u64 end, u64 delalloc_end,
951 int *page_started, unsigned long *nr_written,
952 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500953{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400954 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400955 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500956 u64 alloc_hint = 0;
957 u64 num_bytes;
958 unsigned long ram_size;
959 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000960 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400961 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500962 struct btrfs_key ins;
963 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000964 unsigned clear_bits;
965 unsigned long page_ops;
966 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500967 int ret = 0;
968
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200969 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400970 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500971 ret = -EINVAL;
972 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400973 }
Chris Mason771ed682008-11-06 22:02:51 -0500974
Qu Wenruofda28322013-02-26 08:10:22 +0000975 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500976 num_bytes = max(blocksize, num_bytes);
977 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500978
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200979 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400980
Chris Mason771ed682008-11-06 22:02:51 -0500981 if (start == 0) {
982 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800983 ret = cow_file_range_inline(root, inode, start, end, 0,
984 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500985 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400986 /*
987 * We use DO_ACCOUNTING here because we need the
988 * delalloc_release_metadata to be run _after_ we drop
989 * our outstanding extent for clearing delalloc for this
990 * range.
991 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800992 extent_clear_unlock_delalloc(inode, start, end,
993 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400994 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400995 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
996 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400997 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
998 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500999 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001000 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001001 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001002 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001003 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001005 }
1006 }
Chris Masonc8b97812008-10-29 14:49:59 -04001007
1008 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001009 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001010
Josef Bacik4b46fce2010-05-23 11:00:55 -04001011 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001012 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1013 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001014
Chris Masond3977122009-01-05 21:25:51 -05001015 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +00001016 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001017 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001018 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001019 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001020 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001021 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001022 cur_alloc_size = ins.offset;
1023 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001024
Chris Mason771ed682008-11-06 22:02:51 -05001025 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001026 em = create_io_em(inode, start, ins.offset, /* len */
1027 start, /* orig_start */
1028 ins.objectid, /* block_start */
1029 ins.offset, /* block_len */
1030 ins.offset, /* orig_block_len */
1031 ram_size, /* ram_bytes */
1032 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001033 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001034 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001035 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001036 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001037
Chris Masone6dcd2d2008-07-17 12:53:50 -04001038 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001039 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001040 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001041 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001042
Yan Zheng17d217f2008-12-12 10:03:38 -05001043 if (root->root_key.objectid ==
1044 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1045 ret = btrfs_reloc_clone_csums(inode, start,
1046 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001047 /*
1048 * Only drop cache here, and process as normal.
1049 *
1050 * We must not allow extent_clear_unlock_delalloc()
1051 * at out_unlock label to free meta of this ordered
1052 * extent, as its meta should be freed by
1053 * btrfs_finish_ordered_io().
1054 *
1055 * So we must continue until @start is increased to
1056 * skip current ordered extent.
1057 */
Josef Bacik00361582013-08-14 14:02:47 -04001058 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001059 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1060 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001061 }
1062
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001063 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001064
Chris Masonc8b97812008-10-29 14:49:59 -04001065 /* we're not doing compressed IO, don't unlock the first
1066 * page (which the caller expects to stay locked), don't
1067 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001068 *
1069 * Do set the Private2 bit so we know this page was properly
1070 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001071 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001072 page_ops = unlock ? PAGE_UNLOCK : 0;
1073 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001074
Josef Bacikc2790a22013-07-29 11:20:47 -04001075 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001076 start + ram_size - 1,
1077 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001078 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001079 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001080 if (disk_num_bytes < cur_alloc_size)
1081 disk_num_bytes = 0;
1082 else
1083 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001084 num_bytes -= cur_alloc_size;
1085 alloc_hint = ins.objectid + ins.offset;
1086 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001087 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001088
1089 /*
1090 * btrfs_reloc_clone_csums() error, since start is increased
1091 * extent_clear_unlock_delalloc() at out_unlock label won't
1092 * free metadata of current ordered extent, we're OK to exit.
1093 */
1094 if (ret)
1095 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001096 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001097out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001098 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001099
Filipe Mananad9f85962014-08-25 10:43:00 +01001100out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001101 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001102out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001103 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001104 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001105out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001106 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1107 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001108 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1109 PAGE_END_WRITEBACK;
1110 /*
1111 * If we reserved an extent for our delalloc range (or a subrange) and
1112 * failed to create the respective ordered extent, then it means that
1113 * when we reserved the extent we decremented the extent's size from
1114 * the data space_info's bytes_may_use counter and incremented the
1115 * space_info's bytes_reserved counter by the same amount. We must make
1116 * sure extent_clear_unlock_delalloc() does not try to decrement again
1117 * the data space_info's bytes_may_use counter, therefore we do not pass
1118 * it the flag EXTENT_CLEAR_DATA_RESV.
1119 */
1120 if (extent_reserved) {
1121 extent_clear_unlock_delalloc(inode, start,
1122 start + cur_alloc_size,
1123 start + cur_alloc_size,
1124 locked_page,
1125 clear_bits,
1126 page_ops);
1127 start += cur_alloc_size;
1128 if (start >= end)
1129 goto out;
1130 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001131 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1132 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001133 clear_bits | EXTENT_CLEAR_DATA_RESV,
1134 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001135 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001136}
Chris Masonc8b97812008-10-29 14:49:59 -04001137
Chris Mason771ed682008-11-06 22:02:51 -05001138/*
1139 * work queue call back to started compression on a file and pages
1140 */
1141static noinline void async_cow_start(struct btrfs_work *work)
1142{
1143 struct async_cow *async_cow;
1144 int num_added = 0;
1145 async_cow = container_of(work, struct async_cow, work);
1146
1147 compress_file_range(async_cow->inode, async_cow->locked_page,
1148 async_cow->start, async_cow->end, async_cow,
1149 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001150 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001151 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001152 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001153 }
Chris Mason771ed682008-11-06 22:02:51 -05001154}
1155
1156/*
1157 * work queue call back to submit previously compressed pages
1158 */
1159static noinline void async_cow_submit(struct btrfs_work *work)
1160{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001161 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001162 struct async_cow *async_cow;
1163 struct btrfs_root *root;
1164 unsigned long nr_pages;
1165
1166 async_cow = container_of(work, struct async_cow, work);
1167
1168 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001169 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001170 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1171 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001172
David Sterbaee863952015-02-16 19:41:40 +01001173 /*
1174 * atomic_sub_return implies a barrier for waitqueue_active
1175 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001176 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001177 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001178 waitqueue_active(&fs_info->async_submit_wait))
1179 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001180
Chris Masond3977122009-01-05 21:25:51 -05001181 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001182 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001183}
Chris Masonc8b97812008-10-29 14:49:59 -04001184
Chris Mason771ed682008-11-06 22:02:51 -05001185static noinline void async_cow_free(struct btrfs_work *work)
1186{
1187 struct async_cow *async_cow;
1188 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001189 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001190 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001191 kfree(async_cow);
1192}
1193
1194static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1195 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001196 unsigned long *nr_written,
1197 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001198{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001199 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001200 struct async_cow *async_cow;
1201 struct btrfs_root *root = BTRFS_I(inode)->root;
1202 unsigned long nr_pages;
1203 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001204
Chris Masona3429ab2009-10-08 12:30:20 -04001205 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1206 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001207 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001208 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001209 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001210 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001211 async_cow->root = root;
1212 async_cow->locked_page = locked_page;
1213 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001214 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001215
Wang Shilongf79707b2014-07-17 11:44:09 +08001216 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001217 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001218 cur_end = end;
1219 else
Byongho Leeee221842015-12-15 01:42:10 +09001220 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001221
1222 async_cow->end = cur_end;
1223 INIT_LIST_HEAD(&async_cow->extents);
1224
Liu Bo9e0af232014-08-15 23:36:53 +08001225 btrfs_init_work(&async_cow->work,
1226 btrfs_delalloc_helper,
1227 async_cow_start, async_cow_submit,
1228 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001229
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001230 nr_pages = (cur_end - start + PAGE_SIZE) >>
1231 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001232 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001233
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001234 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001235
Chris Mason771ed682008-11-06 22:02:51 -05001236 *nr_written += nr_pages;
1237 start = cur_end + 1;
1238 }
1239 *page_started = 1;
1240 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001241}
1242
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001243static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001244 u64 bytenr, u64 num_bytes)
1245{
1246 int ret;
1247 struct btrfs_ordered_sum *sums;
1248 LIST_HEAD(list);
1249
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001250 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001251 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001252 if (ret == 0 && list_empty(&list))
1253 return 0;
1254
1255 while (!list_empty(&list)) {
1256 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1257 list_del(&sums->list);
1258 kfree(sums);
1259 }
1260 return 1;
1261}
1262
Chris Masond352ac62008-09-29 15:18:18 -04001263/*
1264 * when nowcow writeback call back. This checks for snapshots or COW copies
1265 * of the extents that exist in the file, and COWs the file as required.
1266 *
1267 * If no cow copies or snapshots exist, we write directly to the existing
1268 * blocks on disk
1269 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001270static noinline int run_delalloc_nocow(struct inode *inode,
1271 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001272 u64 start, u64 end, int *page_started, int force,
1273 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001274{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001275 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001276 struct btrfs_root *root = BTRFS_I(inode)->root;
1277 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001278 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001280 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001281 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 u64 cow_start;
1283 u64 cur_offset;
1284 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001285 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 u64 disk_bytenr;
1287 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001288 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001289 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001291 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001292 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 int nocow;
1294 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001295 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001296 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001297
1298 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001299 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001300 extent_clear_unlock_delalloc(inode, start, end, end,
1301 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001302 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001303 EXTENT_DO_ACCOUNTING |
1304 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001305 PAGE_CLEAR_DIRTY |
1306 PAGE_SET_WRITEBACK |
1307 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001308 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001309 }
Li Zefan82d59022011-04-20 10:33:24 +08001310
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001311 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001312
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 cow_start = (u64)-1;
1314 cur_offset = start;
1315 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001316 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001318 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001319 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001320 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1321 leaf = path->nodes[0];
1322 btrfs_item_key_to_cpu(leaf, &found_key,
1323 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001324 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 found_key.type == BTRFS_EXTENT_DATA_KEY)
1326 path->slots[0]--;
1327 }
1328 check_prev = 0;
1329next_slot:
1330 leaf = path->nodes[0];
1331 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1332 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001333 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001334 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001335 if (ret > 0)
1336 break;
1337 leaf = path->nodes[0];
1338 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001339
Yan Zheng80ff3852008-10-30 14:20:02 -04001340 nocow = 0;
1341 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001342 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001344
Filipe Manana1d512cb2015-11-09 00:33:58 +00001345 if (found_key.objectid > ino)
1346 break;
1347 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1348 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1349 path->slots[0]++;
1350 goto next_slot;
1351 }
1352 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001353 found_key.offset > end)
1354 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001355
Yan Zheng80ff3852008-10-30 14:20:02 -04001356 if (found_key.offset > cur_offset) {
1357 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001358 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 goto out_check;
1360 }
Chris Masonc31f8832008-01-08 15:46:31 -05001361
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 fi = btrfs_item_ptr(leaf, path->slots[0],
1363 struct btrfs_file_extent_item);
1364 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001365
Josef Bacikcc95bef2013-04-04 14:31:27 -04001366 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001367 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1368 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001369 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001370 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 extent_end = found_key.offset +
1372 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001373 disk_num_bytes =
1374 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001375 if (extent_end <= start) {
1376 path->slots[0]++;
1377 goto next_slot;
1378 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001379 if (disk_bytenr == 0)
1380 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 if (btrfs_file_extent_compression(leaf, fi) ||
1382 btrfs_file_extent_encryption(leaf, fi) ||
1383 btrfs_file_extent_other_encoding(leaf, fi))
1384 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001385 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1386 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001387 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001388 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001389 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001390 found_key.offset -
1391 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001392 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001393 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001394 disk_bytenr += cur_offset - found_key.offset;
1395 num_bytes = min(end + 1, extent_end) - cur_offset;
1396 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001397 * if there are pending snapshots for this root,
1398 * we fall into common COW way.
1399 */
1400 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001401 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001402 if (!err)
1403 goto out_check;
1404 }
1405 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001406 * force cow if csum exists in the range.
1407 * this ensure that csum for a given extent are
1408 * either valid or do not exist.
1409 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001410 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001411 num_bytes)) {
1412 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001413 btrfs_end_write_no_snapshotting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001414 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001415 }
1416 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1417 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001418 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001419 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001420 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001421 nocow = 1;
1422 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1423 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001424 btrfs_file_extent_inline_len(leaf,
1425 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001426 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001427 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001428 } else {
1429 BUG_ON(1);
1430 }
1431out_check:
1432 if (extent_end <= start) {
1433 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001434 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001435 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001436 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001437 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001438 goto next_slot;
1439 }
1440 if (!nocow) {
1441 if (cow_start == (u64)-1)
1442 cow_start = cur_offset;
1443 cur_offset = extent_end;
1444 if (cur_offset > end)
1445 break;
1446 path->slots[0]++;
1447 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001448 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001449
David Sterbab3b4aa72011-04-21 01:20:15 +02001450 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001451 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001452 ret = cow_file_range(inode, locked_page,
1453 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001454 end, page_started, nr_written, 1,
1455 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001456 if (ret) {
1457 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001458 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001459 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001460 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001461 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001462 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001463 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001464 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001465 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001466
Yan Zhengd899e052008-10-30 14:25:28 -04001467 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001468 u64 orig_start = found_key.offset - extent_offset;
1469
1470 em = create_io_em(inode, cur_offset, num_bytes,
1471 orig_start,
1472 disk_bytenr, /* block_start */
1473 num_bytes, /* block_len */
1474 disk_num_bytes, /* orig_block_len */
1475 ram_bytes, BTRFS_COMPRESS_NONE,
1476 BTRFS_ORDERED_PREALLOC);
1477 if (IS_ERR(em)) {
1478 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001479 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001480 if (nocow)
1481 btrfs_dec_nocow_writers(fs_info,
1482 disk_bytenr);
1483 ret = PTR_ERR(em);
1484 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001485 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001486 free_extent_map(em);
1487 }
1488
1489 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001490 type = BTRFS_ORDERED_PREALLOC;
1491 } else {
1492 type = BTRFS_ORDERED_NOCOW;
1493 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001494
1495 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001496 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001497 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001498 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001499 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001500
Yan, Zhengefa56462010-05-16 10:49:59 -04001501 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001502 BTRFS_DATA_RELOC_TREE_OBJECTID)
1503 /*
1504 * Error handled later, as we must prevent
1505 * extent_clear_unlock_delalloc() in error handler
1506 * from freeing metadata of created ordered extent.
1507 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001508 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1509 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001510
Josef Bacikc2790a22013-07-29 11:20:47 -04001511 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001512 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001513 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001514 EXTENT_DELALLOC |
1515 EXTENT_CLEAR_DATA_RESV,
1516 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1517
Wang Shilonge9894fd2014-03-27 11:12:25 +08001518 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001519 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001520 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001521
1522 /*
1523 * btrfs_reloc_clone_csums() error, now we're OK to call error
1524 * handler, as metadata for created ordered extent will only
1525 * be freed by btrfs_finish_ordered_io().
1526 */
1527 if (ret)
1528 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001529 if (cur_offset > end)
1530 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001531 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001532 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001533
Josef Bacik17ca04a2012-05-31 15:58:55 -04001534 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001535 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001536 cur_offset = end;
1537 }
1538
Yan Zheng80ff3852008-10-30 14:20:02 -04001539 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001540 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1541 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001542 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001543 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001544 }
1545
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001546error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001547 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001548 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001549 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001550 EXTENT_DELALLOC | EXTENT_DEFRAG |
1551 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1552 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001553 PAGE_SET_WRITEBACK |
1554 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001555 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001556 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001557}
1558
Wang Shilong47059d92014-07-03 18:22:07 +08001559static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1560{
1561
1562 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1563 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1564 return 0;
1565
1566 /*
1567 * @defrag_bytes is a hint value, no spinlock held here,
1568 * if is not zero, it means the file is defragging.
1569 * Force cow if given extent needs to be defragged.
1570 */
1571 if (BTRFS_I(inode)->defrag_bytes &&
1572 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1573 EXTENT_DEFRAG, 0, NULL))
1574 return 1;
1575
1576 return 0;
1577}
1578
Chris Masond352ac62008-09-29 15:18:18 -04001579/*
1580 * extent_io.c call back to do delayed allocation processing
1581 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001582static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001583 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001584 unsigned long *nr_written,
1585 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001586{
Josef Bacikc6100a42017-05-05 11:57:13 -04001587 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001588 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001589 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001590 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001591
Wang Shilong47059d92014-07-03 18:22:07 +08001592 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001593 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001594 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001595 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001596 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001597 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001598 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001599 ret = cow_file_range(inode, locked_page, start, end, end,
1600 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001601 } else {
1602 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1603 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001604 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001605 page_started, nr_written,
1606 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001607 }
Qu Wenruo524272602017-03-08 10:25:52 +08001608 if (ret)
1609 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001610 return ret;
1611}
1612
Josef Bacikc6100a42017-05-05 11:57:13 -04001613static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001614 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001615{
Josef Bacikc6100a42017-05-05 11:57:13 -04001616 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001617 u64 size;
1618
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001619 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001620 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001621 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001622
Josef Bacikdcab6a32015-02-11 15:08:59 -05001623 size = orig->end - orig->start + 1;
1624 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001625 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001626 u64 new_size;
1627
1628 /*
Josef Bacikba117212015-03-13 15:01:24 -04001629 * See the explanation in btrfs_merge_extent_hook, the same
1630 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001631 */
1632 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001633 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001634 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001635 num_extents += count_max_extents(new_size);
1636 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001637 return;
1638 }
1639
Josef Bacik9e0baf62011-07-15 15:16:44 +00001640 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001641 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001642 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001643}
1644
1645/*
1646 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1647 * extents so we can keep track of new extents that are just merged onto old
1648 * extents, such as when we are doing sequential writes, so we can properly
1649 * account for the metadata space we'll need.
1650 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001651static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001652 struct extent_state *new,
1653 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001654{
Josef Bacikc6100a42017-05-05 11:57:13 -04001655 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001656 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001657 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001658
Josef Bacik9ed74f22009-09-11 16:12:44 -04001659 /* not delalloc, ignore it */
1660 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001661 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001662
Josef Bacik8461a3d2015-03-13 15:12:08 -04001663 if (new->start > other->start)
1664 new_size = new->end - other->start + 1;
1665 else
1666 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001667
1668 /* we're not bigger than the max, unreserve the space and go */
1669 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1670 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001671 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001672 spin_unlock(&BTRFS_I(inode)->lock);
1673 return;
1674 }
1675
1676 /*
Josef Bacikba117212015-03-13 15:01:24 -04001677 * We have to add up either side to figure out how many extents were
1678 * accounted for before we merged into one big extent. If the number of
1679 * extents we accounted for is <= the amount we need for the new range
1680 * then we can return, otherwise drop. Think of it like this
1681 *
1682 * [ 4k][MAX_SIZE]
1683 *
1684 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1685 * need 2 outstanding extents, on one side we have 1 and the other side
1686 * we have 1 so they are == and we can return. But in this case
1687 *
1688 * [MAX_SIZE+4k][MAX_SIZE+4k]
1689 *
1690 * Each range on their own accounts for 2 extents, but merged together
1691 * they are only 3 extents worth of accounting, so we need to drop in
1692 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001693 */
Josef Bacikba117212015-03-13 15:01:24 -04001694 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001695 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001696 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001697 num_extents += count_max_extents(old_size);
1698 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001699 return;
1700
Josef Bacik9e0baf62011-07-15 15:16:44 +00001701 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001702 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001703 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001704}
1705
Miao Xieeb73c1b2013-05-15 07:48:22 +00001706static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1707 struct inode *inode)
1708{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001709 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1710
Miao Xieeb73c1b2013-05-15 07:48:22 +00001711 spin_lock(&root->delalloc_lock);
1712 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1713 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1714 &root->delalloc_inodes);
1715 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1716 &BTRFS_I(inode)->runtime_flags);
1717 root->nr_delalloc_inodes++;
1718 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001719 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001720 BUG_ON(!list_empty(&root->delalloc_root));
1721 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001722 &fs_info->delalloc_roots);
1723 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724 }
1725 }
1726 spin_unlock(&root->delalloc_lock);
1727}
1728
1729static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001730 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001731{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001732 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001733
Miao Xieeb73c1b2013-05-15 07:48:22 +00001734 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001735 if (!list_empty(&inode->delalloc_inodes)) {
1736 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001737 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001738 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001739 root->nr_delalloc_inodes--;
1740 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001741 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001742 BUG_ON(list_empty(&root->delalloc_root));
1743 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001744 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001745 }
1746 }
1747 spin_unlock(&root->delalloc_lock);
1748}
1749
Chris Masond352ac62008-09-29 15:18:18 -04001750/*
1751 * extent_io.c set_bit_hook, used to track delayed allocation
1752 * bytes in this file, and to maintain the list of inodes that
1753 * have pending delalloc work to be done.
1754 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001755static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001756 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001757{
Josef Bacikc6100a42017-05-05 11:57:13 -04001758 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001759
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001760 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1761
Wang Shilong47059d92014-07-03 18:22:07 +08001762 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1763 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001764 /*
1765 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001766 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001767 * bit, which is only set or cleared with irqs on
1768 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001769 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001770 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001771 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001772 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001773 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001774
Josef Bacik8b62f872017-10-19 14:15:55 -04001775 spin_lock(&BTRFS_I(inode)->lock);
1776 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1777 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001778
Josef Bacik6a3891c2015-03-16 17:38:52 -04001779 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001780 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001781 return;
1782
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001783 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1784 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001785 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001786 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001787 if (*bits & EXTENT_DEFRAG)
1788 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001789 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001790 &BTRFS_I(inode)->runtime_flags))
1791 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001792 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001793 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001794
1795 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1796 (*bits & EXTENT_DELALLOC_NEW)) {
1797 spin_lock(&BTRFS_I(inode)->lock);
1798 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1799 state->start;
1800 spin_unlock(&BTRFS_I(inode)->lock);
1801 }
Chris Mason291d6732008-01-29 15:55:23 -05001802}
1803
Chris Masond352ac62008-09-29 15:18:18 -04001804/*
1805 * extent_io.c clear_bit_hook, see set_bit_hook for why
1806 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001807static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001808 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001809 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001810{
Josef Bacikc6100a42017-05-05 11:57:13 -04001811 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001812 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001813 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001814 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001815
Filipe Manana4a4b9642017-07-27 19:52:55 +01001816 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1817 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001818 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001819 spin_unlock(&inode->lock);
1820 }
Wang Shilong47059d92014-07-03 18:22:07 +08001821
Chris Mason75eff682008-12-15 15:54:40 -05001822 /*
1823 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001824 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001825 * bit, which is only set or cleared with irqs on
1826 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001827 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001828 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001829 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001830
Josef Bacik8b62f872017-10-19 14:15:55 -04001831 spin_lock(&inode->lock);
1832 btrfs_mod_outstanding_extents(inode, -num_extents);
1833 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001834
Josef Bacikb6d08f02013-09-27 14:57:43 -04001835 /*
1836 * We don't reserve metadata space for space cache inodes so we
1837 * don't need to call dellalloc_release_metadata if there is an
1838 * error.
1839 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001840 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001841 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001842 btrfs_delalloc_release_metadata(inode, len);
1843
Josef Bacik6a3891c2015-03-16 17:38:52 -04001844 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001845 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001846 return;
1847
Filipe Mananaa315e682017-03-06 23:04:20 +00001848 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1849 do_list && !(state->state & EXTENT_NORESERVE) &&
1850 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001851 btrfs_free_reserved_data_space_noquota(
1852 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001853 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001854
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001855 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1856 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001857 spin_lock(&inode->lock);
1858 inode->delalloc_bytes -= len;
1859 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001860 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001861 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001862 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001863 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001864 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001865
1866 if ((state->state & EXTENT_DELALLOC_NEW) &&
1867 (*bits & EXTENT_DELALLOC_NEW)) {
1868 spin_lock(&inode->lock);
1869 ASSERT(inode->new_delalloc_bytes >= len);
1870 inode->new_delalloc_bytes -= len;
1871 spin_unlock(&inode->lock);
1872 }
Chris Mason291d6732008-01-29 15:55:23 -05001873}
1874
Chris Masond352ac62008-09-29 15:18:18 -04001875/*
1876 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1877 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001878 *
1879 * return 1 if page cannot be merged to bio
1880 * return 0 if page can be merged to bio
1881 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001882 */
Mike Christie81a75f672016-06-05 14:31:54 -05001883int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001884 size_t size, struct bio *bio,
1885 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001886{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001887 struct inode *inode = page->mapping->host;
1888 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001889 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001890 u64 length = 0;
1891 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001892 int ret;
1893
Chris Mason771ed682008-11-06 22:02:51 -05001894 if (bio_flags & EXTENT_BIO_COMPRESSED)
1895 return 0;
1896
Kent Overstreet4f024f32013-10-11 15:44:27 -07001897 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001898 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001899 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1900 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001901 if (ret < 0)
1902 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001903 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001904 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001905 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001906}
1907
Chris Masond352ac62008-09-29 15:18:18 -04001908/*
1909 * in order to insert checksums into the metadata in large chunks,
1910 * we wait until bio submission time. All the pages in the bio are
1911 * checksummed and sums are attached onto the ordered extent record.
1912 *
1913 * At IO completion time the cums attached on the ordered extent record
1914 * are inserted into the btree
1915 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001916static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001917 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001918 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001919{
Josef Bacikc6100a42017-05-05 11:57:13 -04001920 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001921 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001922
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001923 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001924 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001925 return 0;
1926}
Chris Masone0156402008-04-16 11:15:20 -04001927
Chris Mason4a69a412008-11-06 22:03:00 -05001928/*
1929 * in order to insert checksums into the metadata in large chunks,
1930 * we wait until bio submission time. All the pages in the bio are
1931 * checksummed and sums are attached onto the ordered extent record.
1932 *
1933 * At IO completion time the cums attached on the ordered extent record
1934 * are inserted into the btree
1935 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001936static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001937 int mirror_num, unsigned long bio_flags,
1938 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001939{
Josef Bacikc6100a42017-05-05 11:57:13 -04001940 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001941 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001942 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001943
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001944 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001945 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001946 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001947 bio_endio(bio);
1948 }
Stefan Behrens61891922012-11-05 18:51:52 +01001949 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001950}
1951
Chris Masond352ac62008-09-29 15:18:18 -04001952/*
Chris Masoncad321a2008-12-17 14:51:42 -05001953 * extent_io.c submission hook. This does the right thing for csum calculation
1954 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001955 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001956static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001957 int mirror_num, unsigned long bio_flags,
1958 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001959{
Josef Bacikc6100a42017-05-05 11:57:13 -04001960 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001961 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001962 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301963 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001964 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001965 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001966 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001967
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001968 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001969
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001970 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301971 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001972
Mike Christie37226b22016-06-05 14:31:52 -05001973 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001974 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001975 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001976 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001977
Chris Masond20f7042008-12-08 16:58:54 -05001978 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001979 ret = btrfs_submit_compressed_read(inode, bio,
1980 mirror_num,
1981 bio_flags);
1982 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001983 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001984 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001985 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001986 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001987 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001988 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001989 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001990 /* csum items have already been cloned */
1991 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1992 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001993 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04001994 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
1995 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001996 __btrfs_submit_bio_start,
1997 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001998 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001999 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002000 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002001 if (ret)
2002 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002003 }
2004
Chris Mason0b86a832008-03-24 15:01:56 -04002005mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002006 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002007
2008out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002009 if (ret) {
2010 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002011 bio_endio(bio);
2012 }
Stefan Behrens61891922012-11-05 18:51:52 +01002013 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002014}
Chris Mason6885f302008-02-20 16:11:05 -05002015
Chris Masond352ac62008-09-29 15:18:18 -04002016/*
2017 * given a list of ordered sums record them in the inode. This happens
2018 * at IO completion time based on sums calculated at bio submission time.
2019 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002020static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002021 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002022{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002023 struct btrfs_ordered_sum *sum;
2024
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002025 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002026 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002027 btrfs_csum_file_blocks(trans,
2028 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002029 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002030 }
2031 return 0;
2032}
2033
Josef Bacik2ac55d42010-02-03 19:33:23 +00002034int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002035 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002036{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002037 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002038 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002039 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002040}
2041
Chris Masond352ac62008-09-29 15:18:18 -04002042/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002043struct btrfs_writepage_fixup {
2044 struct page *page;
2045 struct btrfs_work work;
2046};
2047
Christoph Hellwigb2950862008-12-02 09:54:17 -05002048static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002049{
2050 struct btrfs_writepage_fixup *fixup;
2051 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002052 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002053 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002054 struct page *page;
2055 struct inode *inode;
2056 u64 page_start;
2057 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002058 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002059
2060 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2061 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002062again:
Chris Mason247e7432008-07-17 12:53:51 -04002063 lock_page(page);
2064 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2065 ClearPageChecked(page);
2066 goto out_page;
2067 }
2068
2069 inode = page->mapping->host;
2070 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002071 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002072
David Sterbaff13db42015-12-03 14:30:40 +01002073 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002074 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002075
2076 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002077 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002078 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002079
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002080 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002081 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002082 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002083 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2084 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002085 unlock_page(page);
2086 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002087 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002088 goto again;
2089 }
Chris Mason247e7432008-07-17 12:53:51 -04002090
Qu Wenruo364ecf32017-02-27 15:10:38 +08002091 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002092 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002093 if (ret) {
2094 mapping_set_error(page->mapping, ret);
2095 end_extent_writepage(page, ret, page_start, page_end);
2096 ClearPageChecked(page);
2097 goto out;
2098 }
2099
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002100 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2101 0);
Chris Mason247e7432008-07-17 12:53:51 -04002102 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002103 set_page_dirty(page);
Josef Bacik8b62f872017-10-19 14:15:55 -04002104 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002105out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002106 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2107 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002108out_page:
2109 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002110 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002111 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002112 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002113}
2114
2115/*
2116 * There are a few paths in the higher layers of the kernel that directly
2117 * set the page dirty bit without asking the filesystem if it is a
2118 * good idea. This causes problems because we want to make sure COW
2119 * properly happens and the data=ordered rules are followed.
2120 *
Chris Masonc8b97812008-10-29 14:49:59 -04002121 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002122 * hasn't been properly setup for IO. We kick off an async process
2123 * to fix it up. The async helper will wait for ordered extents, set
2124 * the delalloc bit and make it safe to write the page.
2125 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002126static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002127{
2128 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002129 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002130 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002131
Chris Mason8b62b722009-09-02 16:53:46 -04002132 /* this page is properly in the ordered list */
2133 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002134 return 0;
2135
2136 if (PageChecked(page))
2137 return -EAGAIN;
2138
2139 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2140 if (!fixup)
2141 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002142
Chris Mason247e7432008-07-17 12:53:51 -04002143 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002144 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002145 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2146 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002147 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002148 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002149 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002150}
2151
Yan Zhengd899e052008-10-30 14:25:28 -04002152static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2153 struct inode *inode, u64 file_pos,
2154 u64 disk_bytenr, u64 disk_num_bytes,
2155 u64 num_bytes, u64 ram_bytes,
2156 u8 compression, u8 encryption,
2157 u16 other_encoding, int extent_type)
2158{
2159 struct btrfs_root *root = BTRFS_I(inode)->root;
2160 struct btrfs_file_extent_item *fi;
2161 struct btrfs_path *path;
2162 struct extent_buffer *leaf;
2163 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002164 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002165 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002166 int ret;
2167
2168 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002169 if (!path)
2170 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002171
Chris Masona1ed8352009-09-11 12:27:37 -04002172 /*
2173 * we may be replacing one extent in the tree with another.
2174 * The new extent is pinned in the extent map, and we don't want
2175 * to drop it from the cache until it is completely in the btree.
2176 *
2177 * So, tell btrfs_drop_extents to leave this extent in the cache.
2178 * the caller is expected to unpin it and allow it to be merged
2179 * with the others.
2180 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002181 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2182 file_pos + num_bytes, NULL, 0,
2183 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002184 if (ret)
2185 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002186
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002187 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002188 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002189 ins.offset = file_pos;
2190 ins.type = BTRFS_EXTENT_DATA_KEY;
2191
2192 path->leave_spinning = 1;
2193 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2194 sizeof(*fi));
2195 if (ret)
2196 goto out;
2197 }
Yan Zhengd899e052008-10-30 14:25:28 -04002198 leaf = path->nodes[0];
2199 fi = btrfs_item_ptr(leaf, path->slots[0],
2200 struct btrfs_file_extent_item);
2201 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2202 btrfs_set_file_extent_type(leaf, fi, extent_type);
2203 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2204 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2205 btrfs_set_file_extent_offset(leaf, fi, 0);
2206 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2207 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2208 btrfs_set_file_extent_compression(leaf, fi, compression);
2209 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2210 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002211
Yan Zhengd899e052008-10-30 14:25:28 -04002212 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002213 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002214
2215 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002216
2217 ins.objectid = disk_bytenr;
2218 ins.offset = disk_num_bytes;
2219 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002220
Qu Wenruo297d7502015-09-08 17:08:37 +08002221 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002222 * Release the reserved range from inode dirty range map, as it is
2223 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002224 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002225 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2226 if (ret < 0)
2227 goto out;
2228 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002229 ret = btrfs_alloc_reserved_file_extent(trans, root,
2230 btrfs_ino(BTRFS_I(inode)),
2231 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002232out:
Yan Zhengd899e052008-10-30 14:25:28 -04002233 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002234
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002235 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002236}
2237
Liu Bo38c227d2013-01-29 03:18:40 +00002238/* snapshot-aware defrag */
2239struct sa_defrag_extent_backref {
2240 struct rb_node node;
2241 struct old_sa_defrag_extent *old;
2242 u64 root_id;
2243 u64 inum;
2244 u64 file_pos;
2245 u64 extent_offset;
2246 u64 num_bytes;
2247 u64 generation;
2248};
2249
2250struct old_sa_defrag_extent {
2251 struct list_head list;
2252 struct new_sa_defrag_extent *new;
2253
2254 u64 extent_offset;
2255 u64 bytenr;
2256 u64 offset;
2257 u64 len;
2258 int count;
2259};
2260
2261struct new_sa_defrag_extent {
2262 struct rb_root root;
2263 struct list_head head;
2264 struct btrfs_path *path;
2265 struct inode *inode;
2266 u64 file_pos;
2267 u64 len;
2268 u64 bytenr;
2269 u64 disk_len;
2270 u8 compress_type;
2271};
2272
2273static int backref_comp(struct sa_defrag_extent_backref *b1,
2274 struct sa_defrag_extent_backref *b2)
2275{
2276 if (b1->root_id < b2->root_id)
2277 return -1;
2278 else if (b1->root_id > b2->root_id)
2279 return 1;
2280
2281 if (b1->inum < b2->inum)
2282 return -1;
2283 else if (b1->inum > b2->inum)
2284 return 1;
2285
2286 if (b1->file_pos < b2->file_pos)
2287 return -1;
2288 else if (b1->file_pos > b2->file_pos)
2289 return 1;
2290
2291 /*
2292 * [------------------------------] ===> (a range of space)
2293 * |<--->| |<---->| =============> (fs/file tree A)
2294 * |<---------------------------->| ===> (fs/file tree B)
2295 *
2296 * A range of space can refer to two file extents in one tree while
2297 * refer to only one file extent in another tree.
2298 *
2299 * So we may process a disk offset more than one time(two extents in A)
2300 * and locate at the same extent(one extent in B), then insert two same
2301 * backrefs(both refer to the extent in B).
2302 */
2303 return 0;
2304}
2305
2306static void backref_insert(struct rb_root *root,
2307 struct sa_defrag_extent_backref *backref)
2308{
2309 struct rb_node **p = &root->rb_node;
2310 struct rb_node *parent = NULL;
2311 struct sa_defrag_extent_backref *entry;
2312 int ret;
2313
2314 while (*p) {
2315 parent = *p;
2316 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2317
2318 ret = backref_comp(backref, entry);
2319 if (ret < 0)
2320 p = &(*p)->rb_left;
2321 else
2322 p = &(*p)->rb_right;
2323 }
2324
2325 rb_link_node(&backref->node, parent, p);
2326 rb_insert_color(&backref->node, root);
2327}
2328
2329/*
2330 * Note the backref might has changed, and in this case we just return 0.
2331 */
2332static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2333 void *ctx)
2334{
2335 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002336 struct old_sa_defrag_extent *old = ctx;
2337 struct new_sa_defrag_extent *new = old->new;
2338 struct btrfs_path *path = new->path;
2339 struct btrfs_key key;
2340 struct btrfs_root *root;
2341 struct sa_defrag_extent_backref *backref;
2342 struct extent_buffer *leaf;
2343 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002344 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002345 int slot;
2346 int ret;
2347 u64 extent_offset;
2348 u64 num_bytes;
2349
2350 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002351 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002352 return 0;
2353
2354 key.objectid = root_id;
2355 key.type = BTRFS_ROOT_ITEM_KEY;
2356 key.offset = (u64)-1;
2357
Liu Bo38c227d2013-01-29 03:18:40 +00002358 root = btrfs_read_fs_root_no_name(fs_info, &key);
2359 if (IS_ERR(root)) {
2360 if (PTR_ERR(root) == -ENOENT)
2361 return 0;
2362 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002363 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002364 inum, offset, root_id);
2365 return PTR_ERR(root);
2366 }
2367
2368 key.objectid = inum;
2369 key.type = BTRFS_EXTENT_DATA_KEY;
2370 if (offset > (u64)-1 << 32)
2371 key.offset = 0;
2372 else
2373 key.offset = offset;
2374
2375 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302376 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002377 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002378 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002379
2380 while (1) {
2381 cond_resched();
2382
2383 leaf = path->nodes[0];
2384 slot = path->slots[0];
2385
2386 if (slot >= btrfs_header_nritems(leaf)) {
2387 ret = btrfs_next_leaf(root, path);
2388 if (ret < 0) {
2389 goto out;
2390 } else if (ret > 0) {
2391 ret = 0;
2392 goto out;
2393 }
2394 continue;
2395 }
2396
2397 path->slots[0]++;
2398
2399 btrfs_item_key_to_cpu(leaf, &key, slot);
2400
2401 if (key.objectid > inum)
2402 goto out;
2403
2404 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2405 continue;
2406
2407 extent = btrfs_item_ptr(leaf, slot,
2408 struct btrfs_file_extent_item);
2409
2410 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2411 continue;
2412
Liu Boe68afa42013-07-01 22:13:26 +08002413 /*
2414 * 'offset' refers to the exact key.offset,
2415 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2416 * (key.offset - extent_offset).
2417 */
2418 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002419 continue;
2420
Liu Boe68afa42013-07-01 22:13:26 +08002421 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002422 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002423
Liu Bo38c227d2013-01-29 03:18:40 +00002424 if (extent_offset >= old->extent_offset + old->offset +
2425 old->len || extent_offset + num_bytes <=
2426 old->extent_offset + old->offset)
2427 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002428 break;
2429 }
2430
2431 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2432 if (!backref) {
2433 ret = -ENOENT;
2434 goto out;
2435 }
2436
2437 backref->root_id = root_id;
2438 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002439 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002440 backref->num_bytes = num_bytes;
2441 backref->extent_offset = extent_offset;
2442 backref->generation = btrfs_file_extent_generation(leaf, extent);
2443 backref->old = old;
2444 backref_insert(&new->root, backref);
2445 old->count++;
2446out:
2447 btrfs_release_path(path);
2448 WARN_ON(ret);
2449 return ret;
2450}
2451
2452static noinline bool record_extent_backrefs(struct btrfs_path *path,
2453 struct new_sa_defrag_extent *new)
2454{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002455 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002456 struct old_sa_defrag_extent *old, *tmp;
2457 int ret;
2458
2459 new->path = path;
2460
2461 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002462 ret = iterate_inodes_from_logical(old->bytenr +
2463 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002464 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002465 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002466 if (ret < 0 && ret != -ENOENT)
2467 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002468
2469 /* no backref to be processed for this extent */
2470 if (!old->count) {
2471 list_del(&old->list);
2472 kfree(old);
2473 }
2474 }
2475
2476 if (list_empty(&new->head))
2477 return false;
2478
2479 return true;
2480}
2481
2482static int relink_is_mergable(struct extent_buffer *leaf,
2483 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002484 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002485{
Liu Bo116e0022013-08-02 16:30:40 +08002486 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002487 return 0;
2488
2489 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2490 return 0;
2491
Liu Bo116e0022013-08-02 16:30:40 +08002492 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2493 return 0;
2494
2495 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002496 btrfs_file_extent_other_encoding(leaf, fi))
2497 return 0;
2498
2499 return 1;
2500}
2501
2502/*
2503 * Note the backref might has changed, and in this case we just return 0.
2504 */
2505static noinline int relink_extent_backref(struct btrfs_path *path,
2506 struct sa_defrag_extent_backref *prev,
2507 struct sa_defrag_extent_backref *backref)
2508{
2509 struct btrfs_file_extent_item *extent;
2510 struct btrfs_file_extent_item *item;
2511 struct btrfs_ordered_extent *ordered;
2512 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002513 struct btrfs_root *root;
2514 struct btrfs_key key;
2515 struct extent_buffer *leaf;
2516 struct old_sa_defrag_extent *old = backref->old;
2517 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002518 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002519 struct inode *inode;
2520 struct extent_state *cached = NULL;
2521 int ret = 0;
2522 u64 start;
2523 u64 len;
2524 u64 lock_start;
2525 u64 lock_end;
2526 bool merge = false;
2527 int index;
2528
2529 if (prev && prev->root_id == backref->root_id &&
2530 prev->inum == backref->inum &&
2531 prev->file_pos + prev->num_bytes == backref->file_pos)
2532 merge = true;
2533
2534 /* step 1: get root */
2535 key.objectid = backref->root_id;
2536 key.type = BTRFS_ROOT_ITEM_KEY;
2537 key.offset = (u64)-1;
2538
Liu Bo38c227d2013-01-29 03:18:40 +00002539 index = srcu_read_lock(&fs_info->subvol_srcu);
2540
2541 root = btrfs_read_fs_root_no_name(fs_info, &key);
2542 if (IS_ERR(root)) {
2543 srcu_read_unlock(&fs_info->subvol_srcu, index);
2544 if (PTR_ERR(root) == -ENOENT)
2545 return 0;
2546 return PTR_ERR(root);
2547 }
Liu Bo38c227d2013-01-29 03:18:40 +00002548
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002549 if (btrfs_root_readonly(root)) {
2550 srcu_read_unlock(&fs_info->subvol_srcu, index);
2551 return 0;
2552 }
2553
Liu Bo38c227d2013-01-29 03:18:40 +00002554 /* step 2: get inode */
2555 key.objectid = backref->inum;
2556 key.type = BTRFS_INODE_ITEM_KEY;
2557 key.offset = 0;
2558
2559 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2560 if (IS_ERR(inode)) {
2561 srcu_read_unlock(&fs_info->subvol_srcu, index);
2562 return 0;
2563 }
2564
2565 srcu_read_unlock(&fs_info->subvol_srcu, index);
2566
2567 /* step 3: relink backref */
2568 lock_start = backref->file_pos;
2569 lock_end = backref->file_pos + backref->num_bytes - 1;
2570 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002571 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002572
2573 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2574 if (ordered) {
2575 btrfs_put_ordered_extent(ordered);
2576 goto out_unlock;
2577 }
2578
2579 trans = btrfs_join_transaction(root);
2580 if (IS_ERR(trans)) {
2581 ret = PTR_ERR(trans);
2582 goto out_unlock;
2583 }
2584
2585 key.objectid = backref->inum;
2586 key.type = BTRFS_EXTENT_DATA_KEY;
2587 key.offset = backref->file_pos;
2588
2589 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2590 if (ret < 0) {
2591 goto out_free_path;
2592 } else if (ret > 0) {
2593 ret = 0;
2594 goto out_free_path;
2595 }
2596
2597 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2598 struct btrfs_file_extent_item);
2599
2600 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2601 backref->generation)
2602 goto out_free_path;
2603
2604 btrfs_release_path(path);
2605
2606 start = backref->file_pos;
2607 if (backref->extent_offset < old->extent_offset + old->offset)
2608 start += old->extent_offset + old->offset -
2609 backref->extent_offset;
2610
2611 len = min(backref->extent_offset + backref->num_bytes,
2612 old->extent_offset + old->offset + old->len);
2613 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2614
2615 ret = btrfs_drop_extents(trans, root, inode, start,
2616 start + len, 1);
2617 if (ret)
2618 goto out_free_path;
2619again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002620 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002621 key.type = BTRFS_EXTENT_DATA_KEY;
2622 key.offset = start;
2623
Liu Boa09a0a72013-03-11 09:20:58 +00002624 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002625 if (merge) {
2626 struct btrfs_file_extent_item *fi;
2627 u64 extent_len;
2628 struct btrfs_key found_key;
2629
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002630 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002631 if (ret < 0)
2632 goto out_free_path;
2633
2634 path->slots[0]--;
2635 leaf = path->nodes[0];
2636 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2637
2638 fi = btrfs_item_ptr(leaf, path->slots[0],
2639 struct btrfs_file_extent_item);
2640 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2641
Liu Bo116e0022013-08-02 16:30:40 +08002642 if (extent_len + found_key.offset == start &&
2643 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002644 btrfs_set_file_extent_num_bytes(leaf, fi,
2645 extent_len + len);
2646 btrfs_mark_buffer_dirty(leaf);
2647 inode_add_bytes(inode, len);
2648
2649 ret = 1;
2650 goto out_free_path;
2651 } else {
2652 merge = false;
2653 btrfs_release_path(path);
2654 goto again;
2655 }
2656 }
2657
2658 ret = btrfs_insert_empty_item(trans, root, path, &key,
2659 sizeof(*extent));
2660 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002661 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002662 goto out_free_path;
2663 }
2664
2665 leaf = path->nodes[0];
2666 item = btrfs_item_ptr(leaf, path->slots[0],
2667 struct btrfs_file_extent_item);
2668 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2669 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2670 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2671 btrfs_set_file_extent_num_bytes(leaf, item, len);
2672 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2673 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2674 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2675 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2676 btrfs_set_file_extent_encryption(leaf, item, 0);
2677 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2678
2679 btrfs_mark_buffer_dirty(leaf);
2680 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002681 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002682
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002683 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002684 new->disk_len, 0,
2685 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002686 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002687 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002688 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002689 goto out_free_path;
2690 }
2691
2692 ret = 1;
2693out_free_path:
2694 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002695 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002696 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002697out_unlock:
2698 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2699 &cached, GFP_NOFS);
2700 iput(inode);
2701 return ret;
2702}
2703
Liu Bo6f519562013-10-29 10:45:05 +08002704static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2705{
2706 struct old_sa_defrag_extent *old, *tmp;
2707
2708 if (!new)
2709 return;
2710
2711 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002712 kfree(old);
2713 }
2714 kfree(new);
2715}
2716
Liu Bo38c227d2013-01-29 03:18:40 +00002717static void relink_file_extents(struct new_sa_defrag_extent *new)
2718{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002719 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002720 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002721 struct sa_defrag_extent_backref *backref;
2722 struct sa_defrag_extent_backref *prev = NULL;
2723 struct inode *inode;
2724 struct btrfs_root *root;
2725 struct rb_node *node;
2726 int ret;
2727
2728 inode = new->inode;
2729 root = BTRFS_I(inode)->root;
2730
2731 path = btrfs_alloc_path();
2732 if (!path)
2733 return;
2734
2735 if (!record_extent_backrefs(path, new)) {
2736 btrfs_free_path(path);
2737 goto out;
2738 }
2739 btrfs_release_path(path);
2740
2741 while (1) {
2742 node = rb_first(&new->root);
2743 if (!node)
2744 break;
2745 rb_erase(node, &new->root);
2746
2747 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2748
2749 ret = relink_extent_backref(path, prev, backref);
2750 WARN_ON(ret < 0);
2751
2752 kfree(prev);
2753
2754 if (ret == 1)
2755 prev = backref;
2756 else
2757 prev = NULL;
2758 cond_resched();
2759 }
2760 kfree(prev);
2761
2762 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002763out:
Liu Bo6f519562013-10-29 10:45:05 +08002764 free_sa_defrag_extent(new);
2765
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002766 atomic_dec(&fs_info->defrag_running);
2767 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002768}
2769
2770static struct new_sa_defrag_extent *
2771record_old_file_extents(struct inode *inode,
2772 struct btrfs_ordered_extent *ordered)
2773{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002774 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002775 struct btrfs_root *root = BTRFS_I(inode)->root;
2776 struct btrfs_path *path;
2777 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002778 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002779 struct new_sa_defrag_extent *new;
2780 int ret;
2781
2782 new = kmalloc(sizeof(*new), GFP_NOFS);
2783 if (!new)
2784 return NULL;
2785
2786 new->inode = inode;
2787 new->file_pos = ordered->file_offset;
2788 new->len = ordered->len;
2789 new->bytenr = ordered->start;
2790 new->disk_len = ordered->disk_len;
2791 new->compress_type = ordered->compress_type;
2792 new->root = RB_ROOT;
2793 INIT_LIST_HEAD(&new->head);
2794
2795 path = btrfs_alloc_path();
2796 if (!path)
2797 goto out_kfree;
2798
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002799 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002800 key.type = BTRFS_EXTENT_DATA_KEY;
2801 key.offset = new->file_pos;
2802
2803 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2804 if (ret < 0)
2805 goto out_free_path;
2806 if (ret > 0 && path->slots[0] > 0)
2807 path->slots[0]--;
2808
2809 /* find out all the old extents for the file range */
2810 while (1) {
2811 struct btrfs_file_extent_item *extent;
2812 struct extent_buffer *l;
2813 int slot;
2814 u64 num_bytes;
2815 u64 offset;
2816 u64 end;
2817 u64 disk_bytenr;
2818 u64 extent_offset;
2819
2820 l = path->nodes[0];
2821 slot = path->slots[0];
2822
2823 if (slot >= btrfs_header_nritems(l)) {
2824 ret = btrfs_next_leaf(root, path);
2825 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002826 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002827 else if (ret > 0)
2828 break;
2829 continue;
2830 }
2831
2832 btrfs_item_key_to_cpu(l, &key, slot);
2833
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002834 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002835 break;
2836 if (key.type != BTRFS_EXTENT_DATA_KEY)
2837 break;
2838 if (key.offset >= new->file_pos + new->len)
2839 break;
2840
2841 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2842
2843 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2844 if (key.offset + num_bytes < new->file_pos)
2845 goto next;
2846
2847 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2848 if (!disk_bytenr)
2849 goto next;
2850
2851 extent_offset = btrfs_file_extent_offset(l, extent);
2852
2853 old = kmalloc(sizeof(*old), GFP_NOFS);
2854 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002855 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002856
2857 offset = max(new->file_pos, key.offset);
2858 end = min(new->file_pos + new->len, key.offset + num_bytes);
2859
2860 old->bytenr = disk_bytenr;
2861 old->extent_offset = extent_offset;
2862 old->offset = offset - key.offset;
2863 old->len = end - offset;
2864 old->new = new;
2865 old->count = 0;
2866 list_add_tail(&old->list, &new->head);
2867next:
2868 path->slots[0]++;
2869 cond_resched();
2870 }
2871
2872 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002873 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002874
2875 return new;
2876
Liu Bo38c227d2013-01-29 03:18:40 +00002877out_free_path:
2878 btrfs_free_path(path);
2879out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002880 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002881 return NULL;
2882}
2883
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002884static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002885 u64 start, u64 len)
2886{
2887 struct btrfs_block_group_cache *cache;
2888
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002889 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002890 ASSERT(cache);
2891
2892 spin_lock(&cache->lock);
2893 cache->delalloc_bytes -= len;
2894 spin_unlock(&cache->lock);
2895
2896 btrfs_put_block_group(cache);
2897}
2898
Chris Masond352ac62008-09-29 15:18:18 -04002899/* as ordered data IO finishes, this gets called so we can finish
2900 * an ordered extent if the range of bytes in the file it covers are
2901 * fully written.
2902 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002903static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002904{
Josef Bacik5fd02042012-05-02 14:00:54 -04002905 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002906 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002907 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002908 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002909 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002910 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002911 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002912 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002913 int ret = 0;
2914 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002915 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002916 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002917 bool range_locked = false;
2918 bool clear_new_delalloc_bytes = false;
2919
2920 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2921 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2922 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2923 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002924
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002925 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002926
Josef Bacik5fd02042012-05-02 14:00:54 -04002927 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2928 ret = -EIO;
2929 goto out;
2930 }
2931
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002932 btrfs_free_io_failure_record(BTRFS_I(inode),
2933 ordered_extent->file_offset,
2934 ordered_extent->file_offset +
2935 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002936
Josef Bacik77cef2e2013-08-29 13:57:21 -04002937 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2938 truncated = true;
2939 logical_len = ordered_extent->truncated_len;
2940 /* Truncated the entire extent, don't bother adding */
2941 if (!logical_len)
2942 goto out;
2943 }
2944
Yan, Zhengc2167752009-11-12 09:34:21 +00002945 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002946 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002947
2948 /*
2949 * For mwrite(mmap + memset to write) case, we still reserve
2950 * space for NOCOW range.
2951 * As NOCOW won't cause a new delayed ref, just free the space
2952 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002953 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002954 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002955 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2956 if (nolock)
2957 trans = btrfs_join_transaction_nolock(root);
2958 else
2959 trans = btrfs_join_transaction(root);
2960 if (IS_ERR(trans)) {
2961 ret = PTR_ERR(trans);
2962 trans = NULL;
2963 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002964 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002965 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002966 ret = btrfs_update_inode_fallback(trans, root, inode);
2967 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002968 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002969 goto out;
2970 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002971
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002972 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002973 lock_extent_bits(io_tree, ordered_extent->file_offset,
2974 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002975 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002976
Liu Bo38c227d2013-01-29 03:18:40 +00002977 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2978 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002979 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002980 if (ret) {
2981 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002982 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002983 /* the inode is shared */
2984 new = record_old_file_extents(inode, ordered_extent);
2985
2986 clear_extent_bit(io_tree, ordered_extent->file_offset,
2987 ordered_extent->file_offset + ordered_extent->len - 1,
2988 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2989 }
2990
Josef Bacik0cb59c92010-07-02 12:14:14 -04002991 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002992 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002993 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002994 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002995 if (IS_ERR(trans)) {
2996 ret = PTR_ERR(trans);
2997 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002998 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002999 }
Chris Masona79b7d42014-05-22 16:18:52 -07003000
Josef Bacik69fe2d72017-10-19 14:15:57 -04003001 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003002
Chris Masonc8b97812008-10-29 14:49:59 -04003003 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003004 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003005 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003006 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003007 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003008 ordered_extent->file_offset,
3009 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003010 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003011 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003012 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003013 ret = insert_reserved_file_extent(trans, inode,
3014 ordered_extent->file_offset,
3015 ordered_extent->start,
3016 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003017 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003018 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003019 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003020 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003021 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003022 ordered_extent->start,
3023 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003024 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003025 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3026 ordered_extent->file_offset, ordered_extent->len,
3027 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003028 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003029 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003030 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003031 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003032
David Sterbadf9f6282017-02-10 19:35:37 +01003033 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003034
Josef Bacik6c760c02012-11-09 10:53:21 -05003035 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3036 ret = btrfs_update_inode_fallback(trans, root, inode);
3037 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003038 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003039 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003040 }
3041 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003042out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003043 if (range_locked || clear_new_delalloc_bytes) {
3044 unsigned int clear_bits = 0;
3045
3046 if (range_locked)
3047 clear_bits |= EXTENT_LOCKED;
3048 if (clear_new_delalloc_bytes)
3049 clear_bits |= EXTENT_DELALLOC_NEW;
3050 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3051 ordered_extent->file_offset,
3052 ordered_extent->file_offset +
3053 ordered_extent->len - 1,
3054 clear_bits,
3055 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
3056 0, &cached_state, GFP_NOFS);
3057 }
3058
Miao Xiea698d0752012-09-20 01:51:59 -06003059 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003060 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003061
Josef Bacik77cef2e2013-08-29 13:57:21 -04003062 if (ret || truncated) {
3063 u64 start, end;
3064
3065 if (truncated)
3066 start = ordered_extent->file_offset + logical_len;
3067 else
3068 start = ordered_extent->file_offset;
3069 end = ordered_extent->file_offset + ordered_extent->len - 1;
3070 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3071
3072 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003073 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003074
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003075 /*
3076 * If the ordered extent had an IOERR or something else went
3077 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003078 * back to the allocator. We only free the extent in the
3079 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003080 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003081 if ((ret || !logical_len) &&
3082 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003083 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003084 btrfs_free_reserved_extent(fs_info,
3085 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003086 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003087 }
3088
3089
Josef Bacik5fd02042012-05-02 14:00:54 -04003090 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003091 * This needs to be done to make sure anybody waiting knows we are done
3092 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003093 */
3094 btrfs_remove_ordered_extent(inode, ordered_extent);
3095
Liu Bo38c227d2013-01-29 03:18:40 +00003096 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003097 if (new) {
3098 if (ret) {
3099 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003100 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003101 } else {
3102 relink_file_extents(new);
3103 }
3104 }
Liu Bo38c227d2013-01-29 03:18:40 +00003105
Chris Masone6dcd2d2008-07-17 12:53:50 -04003106 /* once for us */
3107 btrfs_put_ordered_extent(ordered_extent);
3108 /* once for the tree */
3109 btrfs_put_ordered_extent(ordered_extent);
3110
Josef Bacik5fd02042012-05-02 14:00:54 -04003111 return ret;
3112}
3113
3114static void finish_ordered_fn(struct btrfs_work *work)
3115{
3116 struct btrfs_ordered_extent *ordered_extent;
3117 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3118 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003119}
3120
David Sterbac3988d62017-02-17 15:18:32 +01003121static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003122 struct extent_state *state, int uptodate)
3123{
Josef Bacik5fd02042012-05-02 14:00:54 -04003124 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003125 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003126 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003127 struct btrfs_workqueue *wq;
3128 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003129
liubo1abe9b82011-03-24 11:18:59 +00003130 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3131
Chris Mason8b62b722009-09-02 16:53:46 -04003132 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003133 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3134 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003135 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003136
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003137 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003138 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003139 func = btrfs_freespace_write_helper;
3140 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003141 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003142 func = btrfs_endio_write_helper;
3143 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003144
Liu Bo9e0af232014-08-15 23:36:53 +08003145 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3146 NULL);
3147 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003148}
3149
Miao Xiedc380ae2014-09-12 18:43:55 +08003150static int __readpage_endio_check(struct inode *inode,
3151 struct btrfs_io_bio *io_bio,
3152 int icsum, struct page *page,
3153 int pgoff, u64 start, size_t len)
3154{
3155 char *kaddr;
3156 u32 csum_expected;
3157 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003158
3159 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3160
3161 kaddr = kmap_atomic(page);
3162 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003163 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003164 if (csum != csum_expected)
3165 goto zeroit;
3166
3167 kunmap_atomic(kaddr);
3168 return 0;
3169zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003170 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003171 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003172 memset(kaddr + pgoff, 1, len);
3173 flush_dcache_page(page);
3174 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003175 return -EIO;
3176}
3177
Chris Masond352ac62008-09-29 15:18:18 -04003178/*
Chris Masond352ac62008-09-29 15:18:18 -04003179 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003180 * if there's a match, we allow the bio to finish. If not, the code in
3181 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003182 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003183static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3184 u64 phy_offset, struct page *page,
3185 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003186{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003187 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003188 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003189 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003190 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003191
Chris Masond20f7042008-12-08 16:58:54 -05003192 if (PageChecked(page)) {
3193 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003194 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003195 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003196
3197 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003198 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003199
Yan Zheng17d217f2008-12-12 10:03:38 -05003200 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003201 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003202 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003203 return 0;
3204 }
3205
Miao Xiefacc8a222013-07-25 19:22:34 +08003206 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003207 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3208 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003209}
Chris Masonb888db22007-08-27 16:49:44 -04003210
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003211void btrfs_add_delayed_iput(struct inode *inode)
3212{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003213 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003214 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003215
3216 if (atomic_add_unless(&inode->i_count, -1, 1))
3217 return;
3218
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003219 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003220 if (binode->delayed_iput_count == 0) {
3221 ASSERT(list_empty(&binode->delayed_iput));
3222 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3223 } else {
3224 binode->delayed_iput_count++;
3225 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003226 spin_unlock(&fs_info->delayed_iput_lock);
3227}
3228
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003229void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003230{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003231
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003232 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003233 while (!list_empty(&fs_info->delayed_iputs)) {
3234 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003235
David Sterba8089fe62015-11-19 14:15:51 +01003236 inode = list_first_entry(&fs_info->delayed_iputs,
3237 struct btrfs_inode, delayed_iput);
3238 if (inode->delayed_iput_count) {
3239 inode->delayed_iput_count--;
3240 list_move_tail(&inode->delayed_iput,
3241 &fs_info->delayed_iputs);
3242 } else {
3243 list_del_init(&inode->delayed_iput);
3244 }
3245 spin_unlock(&fs_info->delayed_iput_lock);
3246 iput(&inode->vfs_inode);
3247 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003248 }
David Sterba8089fe62015-11-19 14:15:51 +01003249 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003250}
3251
Yan, Zhengd68fc572010-05-16 10:49:58 -04003252/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003253 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003254 * files in the subvolume, it removes orphan item and frees block_rsv
3255 * structure.
3256 */
3257void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3258 struct btrfs_root *root)
3259{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003260 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003261 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003262 int ret;
3263
Josef Bacik8a35d952012-05-23 14:26:42 -04003264 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003265 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3266 return;
3267
Josef Bacik90290e12011-12-02 15:44:12 -05003268 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003269 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003270 spin_unlock(&root->orphan_lock);
3271 return;
3272 }
3273
3274 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3275 spin_unlock(&root->orphan_lock);
3276 return;
3277 }
3278
3279 block_rsv = root->orphan_block_rsv;
3280 root->orphan_block_rsv = NULL;
3281 spin_unlock(&root->orphan_lock);
3282
Miao Xie27cdeb72014-04-02 19:51:05 +08003283 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003284 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003285 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003286 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003287 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003288 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003289 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003290 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3291 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003292 }
3293
Josef Bacik90290e12011-12-02 15:44:12 -05003294 if (block_rsv) {
3295 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003296 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003297 }
3298}
3299
3300/*
Josef Bacik7b128762008-07-24 12:17:14 -04003301 * This creates an orphan entry for the given inode in case something goes
3302 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003303 *
3304 * NOTE: caller of this function should reserve 5 units of metadata for
3305 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003306 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003307int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3308 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003309{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003310 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3311 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003312 struct btrfs_block_rsv *block_rsv = NULL;
3313 int reserve = 0;
3314 int insert = 0;
3315 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003316
Yan, Zhengd68fc572010-05-16 10:49:58 -04003317 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003318 block_rsv = btrfs_alloc_block_rsv(fs_info,
3319 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003320 if (!block_rsv)
3321 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003322 }
3323
Yan, Zhengd68fc572010-05-16 10:49:58 -04003324 spin_lock(&root->orphan_lock);
3325 if (!root->orphan_block_rsv) {
3326 root->orphan_block_rsv = block_rsv;
3327 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003328 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003329 block_rsv = NULL;
3330 }
Josef Bacik7b128762008-07-24 12:17:14 -04003331
Josef Bacik8a35d952012-05-23 14:26:42 -04003332 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003333 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003334#if 0
3335 /*
3336 * For proper ENOSPC handling, we should do orphan
3337 * cleanup when mounting. But this introduces backward
3338 * compatibility issue.
3339 */
3340 if (!xchg(&root->orphan_item_inserted, 1))
3341 insert = 2;
3342 else
3343 insert = 1;
3344#endif
3345 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003346 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003347 }
Josef Bacik7b128762008-07-24 12:17:14 -04003348
Josef Bacik72ac3c02012-05-23 14:13:11 -04003349 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003350 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003351 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003352 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003353
Yan, Zhengd68fc572010-05-16 10:49:58 -04003354 /* grab metadata reservation from transaction handle */
3355 if (reserve) {
3356 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003357 ASSERT(!ret);
3358 if (ret) {
3359 atomic_dec(&root->orphan_inodes);
3360 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003361 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003362 if (insert)
3363 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003364 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003365 return ret;
3366 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003367 }
3368
3369 /* insert an orphan item to track this unlinked/truncated file */
3370 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003371 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003372 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003373 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003374 if (reserve) {
3375 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003376 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003377 btrfs_orphan_release_metadata(inode);
3378 }
3379 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003380 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003381 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003382 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003383 return ret;
3384 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003385 }
3386 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003387 }
3388
3389 /* insert an orphan item to track subvolume contains orphan files */
3390 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003391 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003392 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003393 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003394 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003395 return ret;
3396 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003397 }
3398 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003399}
3400
3401/*
3402 * We have done the truncate/delete so we can go ahead and remove the orphan
3403 * item for this particular inode.
3404 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003405static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003406 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003407{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003408 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003409 int delete_item = 0;
3410 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003411 int ret = 0;
3412
Yan, Zhengd68fc572010-05-16 10:49:58 -04003413 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003414 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003415 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003416 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003417
Josef Bacik72ac3c02012-05-23 14:13:11 -04003418 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003419 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003420 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003421 spin_unlock(&root->orphan_lock);
3422
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003423 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003424 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003425 if (trans)
3426 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003427 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003428 }
Josef Bacik7b128762008-07-24 12:17:14 -04003429
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003430 if (release_rsv)
3431 btrfs_orphan_release_metadata(inode);
3432
Josef Bacik4ef31a42013-08-13 14:10:08 -04003433 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003434}
3435
3436/*
3437 * this cleans up any orphans that may be left on the list from the last use
3438 * of this root.
3439 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003440int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003441{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003442 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003443 struct btrfs_path *path;
3444 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003445 struct btrfs_key key, found_key;
3446 struct btrfs_trans_handle *trans;
3447 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003448 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003449 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3450
Yan, Zhengd68fc572010-05-16 10:49:58 -04003451 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003452 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003453
3454 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003455 if (!path) {
3456 ret = -ENOMEM;
3457 goto out;
3458 }
David Sterbae4058b52015-11-27 16:31:35 +01003459 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003460
3461 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003462 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003463 key.offset = (u64)-1;
3464
Josef Bacik7b128762008-07-24 12:17:14 -04003465 while (1) {
3466 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003467 if (ret < 0)
3468 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003469
3470 /*
3471 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003472 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003473 * find the key and see if we have stuff that matches
3474 */
3475 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003476 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003477 if (path->slots[0] == 0)
3478 break;
3479 path->slots[0]--;
3480 }
3481
3482 /* pull out the item */
3483 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003484 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3485
3486 /* make sure the item matches what we want */
3487 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3488 break;
David Sterba962a2982014-06-04 18:41:45 +02003489 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003490 break;
3491
3492 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003493 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003494
3495 /*
3496 * this is where we are basically btrfs_lookup, without the
3497 * crossing root thing. we store the inode number in the
3498 * offset of the orphan item.
3499 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003500
3501 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003502 btrfs_err(fs_info,
3503 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003504 ret = -EINVAL;
3505 goto out;
3506 }
3507
3508 last_objectid = found_key.offset;
3509
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003510 found_key.objectid = found_key.offset;
3511 found_key.type = BTRFS_INODE_ITEM_KEY;
3512 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003513 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303514 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003515 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003516 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003517
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003518 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003519 struct btrfs_root *dead_root;
3520 struct btrfs_fs_info *fs_info = root->fs_info;
3521 int is_dead_root = 0;
3522
3523 /*
3524 * this is an orphan in the tree root. Currently these
3525 * could come from 2 sources:
3526 * a) a snapshot deletion in progress
3527 * b) a free space cache inode
3528 * We need to distinguish those two, as the snapshot
3529 * orphan must not get deleted.
3530 * find_dead_roots already ran before us, so if this
3531 * is a snapshot deletion, we should find the root
3532 * in the dead_roots list
3533 */
3534 spin_lock(&fs_info->trans_lock);
3535 list_for_each_entry(dead_root, &fs_info->dead_roots,
3536 root_list) {
3537 if (dead_root->root_key.objectid ==
3538 found_key.objectid) {
3539 is_dead_root = 1;
3540 break;
3541 }
3542 }
3543 spin_unlock(&fs_info->trans_lock);
3544 if (is_dead_root) {
3545 /* prevent this orphan from being found again */
3546 key.offset = found_key.objectid - 1;
3547 continue;
3548 }
3549 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003550 /*
3551 * Inode is already gone but the orphan item is still there,
3552 * kill the orphan item.
3553 */
Filipe Manana67710892016-06-06 11:51:25 +01003554 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003555 trans = btrfs_start_transaction(root, 1);
3556 if (IS_ERR(trans)) {
3557 ret = PTR_ERR(trans);
3558 goto out;
3559 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003560 btrfs_debug(fs_info, "auto deleting %Lu",
3561 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003562 ret = btrfs_del_orphan_item(trans, root,
3563 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003564 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003565 if (ret)
3566 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003567 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003568 }
Josef Bacik7b128762008-07-24 12:17:14 -04003569
Josef Bacik7b128762008-07-24 12:17:14 -04003570 /*
3571 * add this inode to the orphan list so btrfs_orphan_del does
3572 * the proper thing when we hit it
3573 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003574 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3575 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003576 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003577
Josef Bacik7b128762008-07-24 12:17:14 -04003578 /* if we have links, this was a truncate, lets do that */
3579 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303580 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003581 iput(inode);
3582 continue;
3583 }
Josef Bacik7b128762008-07-24 12:17:14 -04003584 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003585
3586 /* 1 for the orphan item deletion. */
3587 trans = btrfs_start_transaction(root, 1);
3588 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003589 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003590 ret = PTR_ERR(trans);
3591 goto out;
3592 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003593 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003594 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003595 if (ret) {
3596 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003597 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003598 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003599
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003600 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003601 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003602 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003603 } else {
3604 nr_unlink++;
3605 }
3606
3607 /* this will do delete_inode and everything for us */
3608 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003609 if (ret)
3610 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003611 }
Miao Xie3254c872011-11-10 20:45:05 -05003612 /* release the path since we're done with it */
3613 btrfs_release_path(path);
3614
Yan, Zhengd68fc572010-05-16 10:49:58 -04003615 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3616
3617 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003618 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003619 (u64)-1);
3620
Miao Xie27cdeb72014-04-02 19:51:05 +08003621 if (root->orphan_block_rsv ||
3622 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003623 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003624 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003625 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003626 }
Josef Bacik7b128762008-07-24 12:17:14 -04003627
3628 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003629 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003630 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003631 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003632
3633out:
3634 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003635 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003636 btrfs_free_path(path);
3637 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003638}
3639
Chris Masond352ac62008-09-29 15:18:18 -04003640/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003641 * very simple check to peek ahead in the leaf looking for xattrs. If we
3642 * don't find any xattrs, we know there can't be any acls.
3643 *
3644 * slot is the slot the inode is in, objectid is the objectid of the inode
3645 */
3646static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003647 int slot, u64 objectid,
3648 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003649{
3650 u32 nritems = btrfs_header_nritems(leaf);
3651 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003652 static u64 xattr_access = 0;
3653 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003654 int scanned = 0;
3655
Josef Bacikf23b5a52013-06-19 10:16:26 -04003656 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003657 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3658 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3659 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3660 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003661 }
3662
Chris Mason46a53cc2009-04-27 11:47:50 -04003663 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003664 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003665 while (slot < nritems) {
3666 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3667
3668 /* we found a different objectid, there must not be acls */
3669 if (found_key.objectid != objectid)
3670 return 0;
3671
3672 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003673 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003674 if (*first_xattr_slot == -1)
3675 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003676 if (found_key.offset == xattr_access ||
3677 found_key.offset == xattr_default)
3678 return 1;
3679 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003680
3681 /*
3682 * we found a key greater than an xattr key, there can't
3683 * be any acls later on
3684 */
3685 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3686 return 0;
3687
3688 slot++;
3689 scanned++;
3690
3691 /*
3692 * it goes inode, inode backrefs, xattrs, extents,
3693 * so if there are a ton of hard links to an inode there can
3694 * be a lot of backrefs. Don't waste time searching too hard,
3695 * this is just an optimization
3696 */
3697 if (scanned >= 8)
3698 break;
3699 }
3700 /* we hit the end of the leaf before we found an xattr or
3701 * something larger than an xattr. We have to assume the inode
3702 * has acls
3703 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003704 if (*first_xattr_slot == -1)
3705 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003706 return 1;
3707}
3708
3709/*
Chris Masond352ac62008-09-29 15:18:18 -04003710 * read an inode from the btree into the in-memory inode
3711 */
Filipe Manana67710892016-06-06 11:51:25 +01003712static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003713{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003714 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003715 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003716 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003717 struct btrfs_inode_item *inode_item;
3718 struct btrfs_root *root = BTRFS_I(inode)->root;
3719 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003720 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003721 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003722 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003723 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003724 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003725 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003726
3727 ret = btrfs_fill_inode(inode, &rdev);
3728 if (!ret)
3729 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003730
3731 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003732 if (!path) {
3733 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003734 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003735 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003736
Chris Mason39279cc2007-06-12 06:35:45 -04003737 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003738
Chris Mason39279cc2007-06-12 06:35:45 -04003739 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003740 if (ret) {
3741 if (ret > 0)
3742 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003743 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003744 }
Chris Mason39279cc2007-06-12 06:35:45 -04003745
Chris Mason5f39d392007-10-15 16:14:19 -04003746 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003747
3748 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003749 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003750
Chris Mason5f39d392007-10-15 16:14:19 -04003751 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3752 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003753 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003754 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003755 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3756 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003757 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003758
David Sterbaa937b972014-12-12 17:39:12 +01003759 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3760 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003761
David Sterbaa937b972014-12-12 17:39:12 +01003762 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3763 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003764
David Sterbaa937b972014-12-12 17:39:12 +01003765 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3766 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003767
chandan r9cc97d62012-07-04 12:48:07 +05303768 BTRFS_I(inode)->i_otime.tv_sec =
3769 btrfs_timespec_sec(leaf, &inode_item->otime);
3770 BTRFS_I(inode)->i_otime.tv_nsec =
3771 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003772
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003773 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003774 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003775 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3776
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003777 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003778 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003779 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003780 rdev = btrfs_inode_rdev(leaf, inode_item);
3781
Josef Bacikaec74772008-07-24 12:12:38 -04003782 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003783 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003784
3785cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003786 /*
3787 * If we were modified in the current generation and evicted from memory
3788 * and then re-read we need to do a full sync since we don't have any
3789 * idea about which extents were modified before we were evicted from
3790 * cache.
3791 *
3792 * This is required for both inode re-read from disk and delayed inode
3793 * in delayed_nodes_tree.
3794 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003795 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003796 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3797 &BTRFS_I(inode)->runtime_flags);
3798
Filipe Mananabde6c242015-07-24 00:00:19 +01003799 /*
3800 * We don't persist the id of the transaction where an unlink operation
3801 * against the inode was last made. So here we assume the inode might
3802 * have been evicted, and therefore the exact value of last_unlink_trans
3803 * lost, and set it to last_trans to avoid metadata inconsistencies
3804 * between the inode and its parent if the inode is fsync'ed and the log
3805 * replayed. For example, in the scenario:
3806 *
3807 * touch mydir/foo
3808 * ln mydir/foo mydir/bar
3809 * sync
3810 * unlink mydir/bar
3811 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3812 * xfs_io -c fsync mydir/foo
3813 * <power failure>
3814 * mount fs, triggers fsync log replay
3815 *
3816 * We must make sure that when we fsync our inode foo we also log its
3817 * parent inode, otherwise after log replay the parent still has the
3818 * dentry with the "bar" name but our inode foo has a link count of 1
3819 * and doesn't have an inode ref with the name "bar" anymore.
3820 *
3821 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003822 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003823 * transaction commits on fsync if our inode is a directory, or if our
3824 * inode is not a directory, logging its parent unnecessarily.
3825 */
3826 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3827
Miao Xie67de1172013-12-26 13:07:06 +08003828 path->slots[0]++;
3829 if (inode->i_nlink != 1 ||
3830 path->slots[0] >= btrfs_header_nritems(leaf))
3831 goto cache_acl;
3832
3833 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003834 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003835 goto cache_acl;
3836
3837 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3838 if (location.type == BTRFS_INODE_REF_KEY) {
3839 struct btrfs_inode_ref *ref;
3840
3841 ref = (struct btrfs_inode_ref *)ptr;
3842 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3843 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3844 struct btrfs_inode_extref *extref;
3845
3846 extref = (struct btrfs_inode_extref *)ptr;
3847 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3848 extref);
3849 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003850cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003851 /*
3852 * try to precache a NULL acl entry for files that don't have
3853 * any xattrs or acls
3854 */
Li Zefan33345d012011-04-20 10:31:50 +08003855 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003856 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003857 if (first_xattr_slot != -1) {
3858 path->slots[0] = first_xattr_slot;
3859 ret = btrfs_load_inode_props(inode, path);
3860 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003861 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003862 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003863 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003864 root->root_key.objectid, ret);
3865 }
3866 btrfs_free_path(path);
3867
Al Viro72c04902009-06-24 16:58:48 -04003868 if (!maybe_acls)
3869 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003870
Chris Mason39279cc2007-06-12 06:35:45 -04003871 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003872 case S_IFREG:
3873 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003874 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003875 inode->i_fop = &btrfs_file_operations;
3876 inode->i_op = &btrfs_file_inode_operations;
3877 break;
3878 case S_IFDIR:
3879 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003880 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003881 break;
3882 case S_IFLNK:
3883 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003884 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003885 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3886 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003887 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003888 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003889 init_special_inode(inode, inode->i_mode, rdev);
3890 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003891 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003892
3893 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003894 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003895
3896make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003897 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003898 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003899 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003900}
3901
Chris Masond352ac62008-09-29 15:18:18 -04003902/*
3903 * given a leaf and an inode, copy the inode fields into the leaf
3904 */
Chris Masone02119d2008-09-05 16:13:11 -04003905static void fill_inode_item(struct btrfs_trans_handle *trans,
3906 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003907 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003908 struct inode *inode)
3909{
Liu Bo51fab692012-12-27 09:01:21 +00003910 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003911
Liu Bo51fab692012-12-27 09:01:21 +00003912 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003913
Liu Bo51fab692012-12-27 09:01:21 +00003914 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3915 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3916 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3917 &token);
3918 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3919 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003920
David Sterbaa937b972014-12-12 17:39:12 +01003921 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003922 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003923 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003924 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003925
David Sterbaa937b972014-12-12 17:39:12 +01003926 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003927 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003928 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003929 inode->i_mtime.tv_nsec, &token);
3930
David Sterbaa937b972014-12-12 17:39:12 +01003931 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003932 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003933 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003934 inode->i_ctime.tv_nsec, &token);
3935
chandan r9cc97d62012-07-04 12:48:07 +05303936 btrfs_set_token_timespec_sec(leaf, &item->otime,
3937 BTRFS_I(inode)->i_otime.tv_sec, &token);
3938 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3939 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3940
Liu Bo51fab692012-12-27 09:01:21 +00003941 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3942 &token);
3943 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3944 &token);
3945 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3946 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3947 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3948 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3949 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003950}
3951
Chris Masond352ac62008-09-29 15:18:18 -04003952/*
3953 * copy everything in the in-memory inode into the btree.
3954 */
Chris Mason21151332011-11-10 20:39:08 -05003955static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003956 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003957{
3958 struct btrfs_inode_item *inode_item;
3959 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003960 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003961 int ret;
3962
3963 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003964 if (!path)
3965 return -ENOMEM;
3966
Chris Masonb9473432009-03-13 11:00:37 -04003967 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003968 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3969 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003970 if (ret) {
3971 if (ret > 0)
3972 ret = -ENOENT;
3973 goto failed;
3974 }
3975
Chris Mason5f39d392007-10-15 16:14:19 -04003976 leaf = path->nodes[0];
3977 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003978 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003979
Chris Masone02119d2008-09-05 16:13:11 -04003980 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003981 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003982 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003983 ret = 0;
3984failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003985 btrfs_free_path(path);
3986 return ret;
3987}
3988
Chris Masond352ac62008-09-29 15:18:18 -04003989/*
Chris Mason21151332011-11-10 20:39:08 -05003990 * copy everything in the in-memory inode into the btree.
3991 */
3992noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3993 struct btrfs_root *root, struct inode *inode)
3994{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003995 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003996 int ret;
3997
3998 /*
3999 * If the inode is a free space inode, we can deadlock during commit
4000 * if we put it into the delayed code.
4001 *
4002 * The data relocation inode should also be directly updated
4003 * without delay
4004 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004005 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004006 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004007 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004008 btrfs_update_root_times(trans, root);
4009
Chris Mason21151332011-11-10 20:39:08 -05004010 ret = btrfs_delayed_update_inode(trans, root, inode);
4011 if (!ret)
4012 btrfs_set_inode_last_trans(trans, inode);
4013 return ret;
4014 }
4015
4016 return btrfs_update_inode_item(trans, root, inode);
4017}
4018
Josef Bacikbe6aef62012-10-22 15:43:12 -04004019noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4020 struct btrfs_root *root,
4021 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004022{
4023 int ret;
4024
4025 ret = btrfs_update_inode(trans, root, inode);
4026 if (ret == -ENOSPC)
4027 return btrfs_update_inode_item(trans, root, inode);
4028 return ret;
4029}
4030
4031/*
Chris Masond352ac62008-09-29 15:18:18 -04004032 * unlink helper that gets used here in inode.c and in the tree logging
4033 * recovery code. It remove a link in a directory with a given name, and
4034 * also drops the back refs in the inode to the directory
4035 */
Al Viro92986792011-03-04 17:14:37 +00004036static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4037 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004038 struct btrfs_inode *dir,
4039 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004040 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004041{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004042 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004043 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004044 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004045 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004046 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004047 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004048 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004049 u64 ino = btrfs_ino(inode);
4050 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004051
4052 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004053 if (!path) {
4054 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004055 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004056 }
4057
Chris Masonb9473432009-03-13 11:00:37 -04004058 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004059 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004060 name, name_len, -1);
4061 if (IS_ERR(di)) {
4062 ret = PTR_ERR(di);
4063 goto err;
4064 }
4065 if (!di) {
4066 ret = -ENOENT;
4067 goto err;
4068 }
Chris Mason5f39d392007-10-15 16:14:19 -04004069 leaf = path->nodes[0];
4070 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004071 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004072 if (ret)
4073 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004074 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004075
Miao Xie67de1172013-12-26 13:07:06 +08004076 /*
4077 * If we don't have dir index, we have to get it by looking up
4078 * the inode ref, since we get the inode ref, remove it directly,
4079 * it is unnecessary to do delayed deletion.
4080 *
4081 * But if we have dir index, needn't search inode ref to get it.
4082 * Since the inode ref is close to the inode item, it is better
4083 * that we delay to delete it, and just do this deletion when
4084 * we update the inode item.
4085 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004086 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004087 ret = btrfs_delayed_delete_inode_ref(inode);
4088 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004089 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004090 goto skip_backref;
4091 }
4092 }
4093
Li Zefan33345d012011-04-20 10:31:50 +08004094 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4095 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004096 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004097 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004098 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004099 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004100 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004101 goto err;
4102 }
Miao Xie67de1172013-12-26 13:07:06 +08004103skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004104 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004105 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004106 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004107 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004108 }
Chris Mason39279cc2007-06-12 06:35:45 -04004109
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004110 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4111 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004112 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004113 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004114 goto err;
4115 }
Chris Masone02119d2008-09-05 16:13:11 -04004116
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004117 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4118 index);
Chris Mason6418c962010-10-30 07:34:24 -04004119 if (ret == -ENOENT)
4120 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004121 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004122 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004123err:
4124 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004125 if (ret)
4126 goto out;
4127
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004128 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004129 inode_inc_iversion(&inode->vfs_inode);
4130 inode_inc_iversion(&dir->vfs_inode);
4131 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4132 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4133 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004134out:
Chris Mason39279cc2007-06-12 06:35:45 -04004135 return ret;
4136}
4137
Al Viro92986792011-03-04 17:14:37 +00004138int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4139 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004140 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004141 const char *name, int name_len)
4142{
4143 int ret;
4144 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4145 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004146 drop_nlink(&inode->vfs_inode);
4147 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004148 }
4149 return ret;
4150}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004151
4152/*
4153 * helper to start transaction for unlink and rmdir.
4154 *
Josef Bacikd52be812013-05-29 14:54:47 -04004155 * unlink and rmdir are special in btrfs, they do not always free space, so
4156 * if we cannot make our reservations the normal way try and see if there is
4157 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4158 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004159 */
Josef Bacikd52be812013-05-29 14:54:47 -04004160static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004161{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004162 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004163
Josef Bacike70bea52011-10-11 14:18:24 -04004164 /*
4165 * 1 for the possible orphan item
4166 * 1 for the dir item
4167 * 1 for the dir index
4168 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004169 * 1 for the inode
4170 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004171 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004172}
4173
Chris Mason39279cc2007-06-12 06:35:45 -04004174static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4175{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004176 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004177 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004178 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004179 int ret;
4180
Josef Bacikd52be812013-05-29 14:54:47 -04004181 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004182 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004183 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004184
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004185 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4186 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004187
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004188 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4189 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4190 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004191 if (ret)
4192 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004193
Yan, Zhenga22285a2010-05-16 10:48:46 -04004194 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004195 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004196 if (ret)
4197 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004198 }
Josef Bacik7b128762008-07-24 12:17:14 -04004199
Tsutomu Itohb5324022011-07-19 07:27:20 +00004200out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004201 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004202 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004203 return ret;
4204}
4205
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004206int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4207 struct btrfs_root *root,
4208 struct inode *dir, u64 objectid,
4209 const char *name, int name_len)
4210{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004211 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004212 struct btrfs_path *path;
4213 struct extent_buffer *leaf;
4214 struct btrfs_dir_item *di;
4215 struct btrfs_key key;
4216 u64 index;
4217 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004218 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004219
4220 path = btrfs_alloc_path();
4221 if (!path)
4222 return -ENOMEM;
4223
Li Zefan33345d012011-04-20 10:31:50 +08004224 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004225 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004226 if (IS_ERR_OR_NULL(di)) {
4227 if (!di)
4228 ret = -ENOENT;
4229 else
4230 ret = PTR_ERR(di);
4231 goto out;
4232 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004233
4234 leaf = path->nodes[0];
4235 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4236 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4237 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004238 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004239 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004240 goto out;
4241 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004242 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004243
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004244 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4245 root->root_key.objectid, dir_ino,
4246 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004247 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004248 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004249 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004250 goto out;
4251 }
Li Zefan33345d012011-04-20 10:31:50 +08004252 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004253 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004254 if (IS_ERR_OR_NULL(di)) {
4255 if (!di)
4256 ret = -ENOENT;
4257 else
4258 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004259 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004260 goto out;
4261 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004262
4263 leaf = path->nodes[0];
4264 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004265 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004266 index = key.offset;
4267 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004268 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004269
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004270 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004271 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004272 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004273 goto out;
4274 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004275
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004276 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004277 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004278 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004279 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004280 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004281 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004282out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004283 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004284 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004285}
4286
Chris Mason39279cc2007-06-12 06:35:45 -04004287static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4288{
David Howells2b0143b2015-03-17 22:25:59 +00004289 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004290 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004291 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004292 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004293 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004294
David Sterbab3ae2442012-09-13 16:04:34 -06004295 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004296 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004297 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004298 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004299
Josef Bacikd52be812013-05-29 14:54:47 -04004300 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004301 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004302 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004303
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004304 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004305 err = btrfs_unlink_subvol(trans, root, dir,
4306 BTRFS_I(inode)->location.objectid,
4307 dentry->d_name.name,
4308 dentry->d_name.len);
4309 goto out;
4310 }
4311
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004312 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004313 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004314 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004315
Filipe Manana44f714d2016-06-06 16:11:13 +01004316 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4317
Chris Mason39279cc2007-06-12 06:35:45 -04004318 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004319 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4320 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4321 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004322 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004323 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004324 /*
4325 * Propagate the last_unlink_trans value of the deleted dir to
4326 * its parent directory. This is to prevent an unrecoverable
4327 * log tree in the case we do something like this:
4328 * 1) create dir foo
4329 * 2) create snapshot under dir foo
4330 * 3) delete the snapshot
4331 * 4) rmdir foo
4332 * 5) mkdir foo
4333 * 6) fsync foo or some file inside foo
4334 */
4335 if (last_unlink_trans >= trans->transid)
4336 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4337 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004338out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004339 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004340 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004341
Chris Mason39279cc2007-06-12 06:35:45 -04004342 return err;
4343}
4344
Chris Mason28f75a02015-02-04 06:59:29 -08004345static int truncate_space_check(struct btrfs_trans_handle *trans,
4346 struct btrfs_root *root,
4347 u64 bytes_deleted)
4348{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004349 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004350 int ret;
4351
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004352 /*
4353 * This is only used to apply pressure to the enospc system, we don't
4354 * intend to use this reservation at all.
4355 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004356 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004357 bytes_deleted *= fs_info->nodesize;
4358 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004359 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004360 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004361 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004362 trans->transid,
4363 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004364 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004365 }
Chris Mason28f75a02015-02-04 06:59:29 -08004366 return ret;
4367
4368}
4369
Josef Bacikddfae632017-10-19 14:16:02 -04004370/*
4371 * Return this if we need to call truncate_block for the last bit of the
4372 * truncate.
4373 */
4374#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004375
Chris Mason323ac952008-10-01 19:05:46 -04004376/*
Chris Mason39279cc2007-06-12 06:35:45 -04004377 * this can truncate away extent items, csum items and directory items.
4378 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004379 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004380 *
4381 * csum items that cross the new i_size are truncated to the new size
4382 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004383 *
4384 * min_type is the minimum key type to truncate down to. If set to 0, this
4385 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004386 */
Yan, Zheng80825102009-11-12 09:35:36 +00004387int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4388 struct btrfs_root *root,
4389 struct inode *inode,
4390 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004391{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004392 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004393 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004394 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004395 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004396 struct btrfs_key key;
4397 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004398 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004399 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004400 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004401 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004402 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004403 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004404 int found_extent;
4405 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004406 int pending_del_nr = 0;
4407 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004408 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004409 int ret;
4410 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004411 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004412 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004413 bool be_nice = false;
4414 bool should_throttle = false;
4415 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004416
4417 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004418
Chris Mason28ed1342014-12-17 09:41:04 -08004419 /*
4420 * for non-free space inodes and ref cows, we want to back off from
4421 * time to time
4422 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004423 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004424 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004425 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004426
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004427 path = btrfs_alloc_path();
4428 if (!path)
4429 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004430 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004431
Josef Bacik5dc562c2012-08-17 13:14:17 -04004432 /*
4433 * We want to drop from the next block forward in case this new size is
4434 * not block aligned since we will be keeping the last block of the
4435 * extent just the way it is.
4436 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004437 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004438 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004439 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004440 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004441 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004442
Miao Xie16cdcec2011-04-22 18:12:22 +08004443 /*
4444 * This function is also used to drop the items in the log tree before
4445 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4446 * it is used to drop the loged items. So we shouldn't kill the delayed
4447 * items.
4448 */
4449 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004450 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004451
Li Zefan33345d012011-04-20 10:31:50 +08004452 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004453 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004454 key.type = (u8)-1;
4455
Chris Mason85e21ba2008-01-29 15:11:36 -05004456search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004457 /*
4458 * with a 16K leaf size and 128MB extents, you can actually queue
4459 * up a huge file in a single leaf. Most of the time that
4460 * bytes_deleted is > 0, it will be huge by the time we get here
4461 */
Byongho Leeee221842015-12-15 01:42:10 +09004462 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004463 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004464 err = -EAGAIN;
4465 goto error;
4466 }
4467 }
4468
4469
Chris Masonb9473432009-03-13 11:00:37 -04004470 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004471 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004472 if (ret < 0) {
4473 err = ret;
4474 goto out;
4475 }
Chris Masond3977122009-01-05 21:25:51 -05004476
Chris Mason85e21ba2008-01-29 15:11:36 -05004477 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004478 /* there are no items in the tree for us to truncate, we're
4479 * done
4480 */
Yan, Zheng80825102009-11-12 09:35:36 +00004481 if (path->slots[0] == 0)
4482 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004483 path->slots[0]--;
4484 }
4485
Chris Masond3977122009-01-05 21:25:51 -05004486 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004487 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004488 leaf = path->nodes[0];
4489 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004490 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004491
Li Zefan33345d012011-04-20 10:31:50 +08004492 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004493 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004494
Chris Mason85e21ba2008-01-29 15:11:36 -05004495 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004496 break;
4497
Chris Mason5f39d392007-10-15 16:14:19 -04004498 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004499 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004500 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004501 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004502 extent_type = btrfs_file_extent_type(leaf, fi);
4503 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004504 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004505 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004506
4507 trace_btrfs_truncate_show_fi_regular(
4508 BTRFS_I(inode), leaf, fi,
4509 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004510 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004511 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004512 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004513
4514 trace_btrfs_truncate_show_fi_inline(
4515 BTRFS_I(inode), leaf, fi, path->slots[0],
4516 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004517 }
Yan008630c2007-11-07 13:31:09 -05004518 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004519 }
Yan, Zheng80825102009-11-12 09:35:36 +00004520 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004521 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004522 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004523 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004524 break;
4525 if (found_key.offset >= new_size)
4526 del_item = 1;
4527 else
4528 del_item = 0;
4529 }
Chris Mason39279cc2007-06-12 06:35:45 -04004530 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004531 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004532 if (found_type != BTRFS_EXTENT_DATA_KEY)
4533 goto delete;
4534
4535 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004536 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004537 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004538 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004539 u64 orig_num_bytes =
4540 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004541 extent_num_bytes = ALIGN(new_size -
4542 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004543 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004544 btrfs_set_file_extent_num_bytes(leaf, fi,
4545 extent_num_bytes);
4546 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004547 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004548 if (test_bit(BTRFS_ROOT_REF_COWS,
4549 &root->state) &&
4550 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004551 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004552 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004553 } else {
Chris Masondb945352007-10-15 16:15:53 -04004554 extent_num_bytes =
4555 btrfs_file_extent_disk_num_bytes(leaf,
4556 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004557 extent_offset = found_key.offset -
4558 btrfs_file_extent_offset(leaf, fi);
4559
Chris Mason39279cc2007-06-12 06:35:45 -04004560 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004561 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004562 if (extent_start != 0) {
4563 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004564 if (test_bit(BTRFS_ROOT_REF_COWS,
4565 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004566 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004567 }
4568 }
Chris Mason90692182008-02-08 13:49:28 -05004569 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004570 /*
4571 * we can't truncate inline items that have had
4572 * special encodings
4573 */
4574 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004575 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004576 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4577 btrfs_file_extent_compression(leaf, fi) == 0) {
4578 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004579
Josef Bacikddfae632017-10-19 14:16:02 -04004580 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4581 size = btrfs_file_extent_calc_inline_size(size);
4582 btrfs_truncate_item(root->fs_info, path, size, 1);
4583 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004584 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004585 * We have to bail so the last_size is set to
4586 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004587 */
Josef Bacikddfae632017-10-19 14:16:02 -04004588 err = NEED_TRUNCATE_BLOCK;
4589 break;
Chris Mason90692182008-02-08 13:49:28 -05004590 }
Josef Bacikddfae632017-10-19 14:16:02 -04004591
4592 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4593 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004594 }
Chris Mason179e29e2007-11-01 11:28:41 -04004595delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004596 if (del_item)
4597 last_size = found_key.offset;
4598 else
4599 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004600 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004601 if (!pending_del_nr) {
4602 /* no pending yet, add ourselves */
4603 pending_del_slot = path->slots[0];
4604 pending_del_nr = 1;
4605 } else if (pending_del_nr &&
4606 path->slots[0] + 1 == pending_del_slot) {
4607 /* hop on the pending chunk */
4608 pending_del_nr++;
4609 pending_del_slot = path->slots[0];
4610 } else {
Chris Masond3977122009-01-05 21:25:51 -05004611 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004612 }
Chris Mason39279cc2007-06-12 06:35:45 -04004613 } else {
4614 break;
4615 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004616 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004617
Miao Xie27cdeb72014-04-02 19:51:05 +08004618 if (found_extent &&
4619 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004620 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004621 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004622 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004623 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004624 extent_num_bytes, 0,
4625 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004626 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004627 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004628 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4629 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004630 trans->delayed_ref_updates * 2,
4631 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004632 if (be_nice) {
4633 if (truncate_space_check(trans, root,
4634 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004635 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004636 }
4637 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004638 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004639 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004640 }
Chris Mason39279cc2007-06-12 06:35:45 -04004641 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004642
Yan, Zheng80825102009-11-12 09:35:36 +00004643 if (found_type == BTRFS_INODE_ITEM_KEY)
4644 break;
4645
4646 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004647 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004648 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004649 if (pending_del_nr) {
4650 ret = btrfs_del_items(trans, root, path,
4651 pending_del_slot,
4652 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004653 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004654 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004655 goto error;
4656 }
Yan, Zheng80825102009-11-12 09:35:36 +00004657 pending_del_nr = 0;
4658 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004659 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004660 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004661 unsigned long updates = trans->delayed_ref_updates;
4662 if (updates) {
4663 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004664 ret = btrfs_run_delayed_refs(trans,
4665 fs_info,
4666 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004667 if (ret && !err)
4668 err = ret;
4669 }
4670 }
Chris Mason28f75a02015-02-04 06:59:29 -08004671 /*
4672 * if we failed to refill our space rsv, bail out
4673 * and let the transaction restart
4674 */
4675 if (should_end) {
4676 err = -EAGAIN;
4677 goto error;
4678 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004679 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004680 } else {
4681 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004682 }
Chris Mason39279cc2007-06-12 06:35:45 -04004683 }
Yan, Zheng80825102009-11-12 09:35:36 +00004684out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004685 if (pending_del_nr) {
4686 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4687 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004688 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004689 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004690 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004691error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004692 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4693 ASSERT(last_size >= new_size);
4694 if (!err && last_size > new_size)
4695 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004696 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004697 }
Chris Mason28ed1342014-12-17 09:41:04 -08004698
Chris Mason39279cc2007-06-12 06:35:45 -04004699 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004700
Byongho Leeee221842015-12-15 01:42:10 +09004701 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004702 unsigned long updates = trans->delayed_ref_updates;
4703 if (updates) {
4704 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004705 ret = btrfs_run_delayed_refs(trans, fs_info,
4706 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004707 if (ret && !err)
4708 err = ret;
4709 }
4710 }
Yan, Zheng80825102009-11-12 09:35:36 +00004711 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004712}
4713
Chris Masona52d9a82007-08-27 16:49:44 -04004714/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304715 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004716 * @inode - inode that we're zeroing
4717 * @from - the offset to start zeroing
4718 * @len - the length to zero, 0 to zero the entire range respective to the
4719 * offset
4720 * @front - zero up to the offset instead of from the offset on
4721 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304722 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004723 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004724 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304725int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004726 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004727{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004728 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004729 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004730 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4731 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004732 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004733 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004734 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004735 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004736 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304737 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004738 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004739 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004740 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304741 u64 block_start;
4742 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004743
Josef Bacik2aaa6652012-08-29 14:27:18 -04004744 if ((offset & (blocksize - 1)) == 0 &&
4745 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004746 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304747
Josef Bacik8b62f872017-10-19 14:15:55 -04004748 block_start = round_down(from, blocksize);
4749 block_end = block_start + blocksize - 1;
4750
Qu Wenruo364ecf32017-02-27 15:10:38 +08004751 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004752 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004753 if (ret)
4754 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004755
Chris Mason211c17f2008-05-15 09:13:45 -04004756again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004757 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004758 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004759 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004760 block_start, blocksize);
4761 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004762 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004763 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004764 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004765
Chris Masona52d9a82007-08-27 16:49:44 -04004766 if (!PageUptodate(page)) {
4767 ret = btrfs_readpage(NULL, page);
4768 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004769 if (page->mapping != mapping) {
4770 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004771 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004772 goto again;
4773 }
Chris Masona52d9a82007-08-27 16:49:44 -04004774 if (!PageUptodate(page)) {
4775 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004776 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004777 }
4778 }
Chris Mason211c17f2008-05-15 09:13:45 -04004779 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004780
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304781 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004782 set_page_extent_mapped(page);
4783
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304784 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004785 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304786 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004787 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004788 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004789 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004790 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004791 btrfs_put_ordered_extent(ordered);
4792 goto again;
4793 }
4794
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304795 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004796 EXTENT_DIRTY | EXTENT_DELALLOC |
4797 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004798 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004799
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304800 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004801 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004802 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304803 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004804 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004805 goto out_unlock;
4806 }
4807
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304808 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004809 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304810 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004811 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004812 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304813 memset(kaddr + (block_start - page_offset(page)),
4814 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004815 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304816 memset(kaddr + (block_start - page_offset(page)) + offset,
4817 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004818 flush_dcache_page(page);
4819 kunmap(page);
4820 }
Chris Mason247e7432008-07-17 12:53:51 -04004821 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004822 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304823 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004824 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004825
Chris Mason89642222008-07-24 09:41:53 -04004826out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004827 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004828 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304829 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004830 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004831 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004832 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004833out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004834 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004835 return ret;
4836}
4837
Josef Bacik16e75492013-10-22 12:18:51 -04004838static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4839 u64 offset, u64 len)
4840{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004841 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004842 struct btrfs_trans_handle *trans;
4843 int ret;
4844
4845 /*
4846 * Still need to make sure the inode looks like it's been updated so
4847 * that any holes get logged if we fsync.
4848 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004849 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4850 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004851 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4852 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4853 return 0;
4854 }
4855
4856 /*
4857 * 1 - for the one we're dropping
4858 * 1 - for the one we're adding
4859 * 1 - for updating the inode.
4860 */
4861 trans = btrfs_start_transaction(root, 3);
4862 if (IS_ERR(trans))
4863 return PTR_ERR(trans);
4864
4865 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4866 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004867 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004868 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004869 return ret;
4870 }
4871
David Sterbaf85b7372017-01-20 14:54:07 +01004872 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4873 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004874 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004875 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004876 else
4877 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004878 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004879 return ret;
4880}
4881
Josef Bacik695a0d02011-03-04 15:46:53 -05004882/*
4883 * This function puts in dummy file extents for the area we're creating a hole
4884 * for. So if we are truncating this file to a larger size we need to insert
4885 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4886 * the range between oldsize and size
4887 */
Josef Bacika41ad392011-01-31 15:30:16 -05004888int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004889{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004890 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004891 struct btrfs_root *root = BTRFS_I(inode)->root;
4892 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004893 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004894 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004895 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004896 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4897 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004898 u64 last_byte;
4899 u64 cur_offset;
4900 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004901 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004902
Josef Bacika71754f2013-06-17 17:14:39 -04004903 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304904 * If our size started in the middle of a block we need to zero out the
4905 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004906 * expose stale data.
4907 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304908 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004909 if (err)
4910 return err;
4911
Yan Zheng9036c102008-10-30 14:19:41 -04004912 if (size <= hole_start)
4913 return 0;
4914
Yan Zheng9036c102008-10-30 14:19:41 -04004915 while (1) {
4916 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004917
David Sterbaff13db42015-12-03 14:30:40 +01004918 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004919 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004920 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004921 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004922 if (!ordered)
4923 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004924 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4925 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004926 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004927 btrfs_put_ordered_extent(ordered);
4928 }
4929
Yan Zheng9036c102008-10-30 14:19:41 -04004930 cur_offset = hole_start;
4931 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004932 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004933 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004934 if (IS_ERR(em)) {
4935 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004936 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004937 break;
4938 }
Yan Zheng9036c102008-10-30 14:19:41 -04004939 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004940 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004941 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004942 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004943 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004944
Josef Bacik16e75492013-10-22 12:18:51 -04004945 err = maybe_insert_hole(root, inode, cur_offset,
4946 hole_size);
4947 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004948 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004949 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004950 cur_offset + hole_size - 1, 0);
4951 hole_em = alloc_extent_map();
4952 if (!hole_em) {
4953 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4954 &BTRFS_I(inode)->runtime_flags);
4955 goto next;
4956 }
4957 hole_em->start = cur_offset;
4958 hole_em->len = hole_size;
4959 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004960
Josef Bacik5dc562c2012-08-17 13:14:17 -04004961 hole_em->block_start = EXTENT_MAP_HOLE;
4962 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004963 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004964 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004965 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004966 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004967 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004968
4969 while (1) {
4970 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004971 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004972 write_unlock(&em_tree->lock);
4973 if (err != -EEXIST)
4974 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004975 btrfs_drop_extent_cache(BTRFS_I(inode),
4976 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004977 cur_offset +
4978 hole_size - 1, 0);
4979 }
4980 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004981 }
Josef Bacik16e75492013-10-22 12:18:51 -04004982next:
Yan Zheng9036c102008-10-30 14:19:41 -04004983 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004984 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004985 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004986 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004987 break;
4988 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004989 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004990 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4991 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004992 return err;
4993}
4994
Eric Sandeen3972f262013-01-12 02:57:22 +00004995static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004996{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004997 struct btrfs_root *root = BTRFS_I(inode)->root;
4998 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004999 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005000 loff_t newsize = attr->ia_size;
5001 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005002 int ret;
5003
Eric Sandeen3972f262013-01-12 02:57:22 +00005004 /*
5005 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5006 * special case where we need to update the times despite not having
5007 * these flags set. For all other operations the VFS set these flags
5008 * explicitly if it wants a timestamp update.
5009 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005010 if (newsize != oldsize) {
5011 inode_inc_iversion(inode);
5012 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5013 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005014 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005015 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005016
Josef Bacika41ad392011-01-31 15:30:16 -05005017 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005018 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005019 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005020 * This is to ensure the snapshot captures a fully consistent
5021 * state of this file - if the snapshot captures this expanding
5022 * truncation, it must capture all writes that happened before
5023 * this truncation.
5024 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005025 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005026 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005027 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005028 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005029 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005030 }
Yan, Zheng80825102009-11-12 09:35:36 +00005031
Miao Xief4a2f4c2011-12-14 20:12:01 -05005032 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005033 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005034 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005035 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005036 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005037
5038 i_size_write(inode, newsize);
5039 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305040 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005041 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005042 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005043 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005044 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005045
Josef Bacika41ad392011-01-31 15:30:16 -05005046 /*
5047 * We're truncating a file that used to have good data down to
5048 * zero. Make sure it gets into the ordered flush list so that
5049 * any new writes get down to disk quickly.
5050 */
5051 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005052 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5053 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005054
Josef Bacikf3fe8202013-01-07 17:03:21 -05005055 /*
5056 * 1 for the orphan item we're going to add
5057 * 1 for the orphan item deletion.
5058 */
5059 trans = btrfs_start_transaction(root, 2);
5060 if (IS_ERR(trans))
5061 return PTR_ERR(trans);
5062
5063 /*
5064 * We need to do this in case we fail at _any_ point during the
5065 * actual truncate. Once we do the truncate_setsize we could
5066 * invalidate pages which forces any outstanding ordered io to
5067 * be instantly completed which will give us extents that need
5068 * to be truncated. If we fail to get an orphan inode down we
5069 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005070 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005071 * will be consistent.
5072 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005073 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005074 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005075 if (ret)
5076 return ret;
5077
Josef Bacika41ad392011-01-31 15:30:16 -05005078 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5079 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005080
5081 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005082 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005083 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005084 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005085
Josef Bacika41ad392011-01-31 15:30:16 -05005086 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005087 if (ret && inode->i_nlink) {
5088 int err;
5089
Liu Bo19fd2df2016-12-01 13:01:02 -08005090 /* To get a stable disk_i_size */
5091 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5092 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005093 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005094 return err;
5095 }
5096
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005097 /*
5098 * failed to truncate, disk_i_size is only adjusted down
5099 * as we remove extents, so it should represent the true
5100 * size of the inode, so reset the in memory size and
5101 * delete our orphan entry.
5102 */
5103 trans = btrfs_join_transaction(root);
5104 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005105 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005106 return ret;
5107 }
5108 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005109 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005110 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005111 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005112 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005113 }
Yan, Zheng80825102009-11-12 09:35:36 +00005114 }
5115
Josef Bacika41ad392011-01-31 15:30:16 -05005116 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005117}
5118
Chris Mason39279cc2007-06-12 06:35:45 -04005119static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5120{
David Howells2b0143b2015-03-17 22:25:59 +00005121 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005122 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005123 int err;
5124
Li Zefanb83cc962010-12-20 16:04:08 +08005125 if (btrfs_root_readonly(root))
5126 return -EROFS;
5127
Jan Kara31051c82016-05-26 16:55:18 +02005128 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005129 if (err)
5130 return err;
5131
Chris Mason5a3f23d2009-03-31 13:27:11 -04005132 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005133 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005134 if (err)
5135 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005136 }
Yan Zheng9036c102008-10-30 14:19:41 -04005137
Christoph Hellwig10257742010-06-04 11:30:02 +02005138 if (attr->ia_valid) {
5139 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005140 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005141 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005142
Josef Bacik22c44fe2011-11-30 10:45:38 -05005143 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005144 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005145 }
5146
Chris Mason39279cc2007-06-12 06:35:45 -04005147 return err;
5148}
Chris Mason61295eb2008-01-14 16:24:38 -05005149
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005150/*
5151 * While truncating the inode pages during eviction, we get the VFS calling
5152 * btrfs_invalidatepage() against each page of the inode. This is slow because
5153 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5154 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5155 * extent_state structures over and over, wasting lots of time.
5156 *
5157 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5158 * those expensive operations on a per page basis and do only the ordered io
5159 * finishing, while we release here the extent_map and extent_state structures,
5160 * without the excessive merging and splitting.
5161 */
5162static void evict_inode_truncate_pages(struct inode *inode)
5163{
5164 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5165 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5166 struct rb_node *node;
5167
5168 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005169 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005170
5171 write_lock(&map_tree->lock);
5172 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5173 struct extent_map *em;
5174
5175 node = rb_first(&map_tree->map);
5176 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005177 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5178 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005179 remove_extent_mapping(map_tree, em);
5180 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005181 if (need_resched()) {
5182 write_unlock(&map_tree->lock);
5183 cond_resched();
5184 write_lock(&map_tree->lock);
5185 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005186 }
5187 write_unlock(&map_tree->lock);
5188
Filipe Manana6ca07092015-05-26 00:55:42 +01005189 /*
5190 * Keep looping until we have no more ranges in the io tree.
5191 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005192 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5193 * still in progress (unlocked the pages in the bio but did not yet
5194 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005195 * ranges can still be locked and eviction started because before
5196 * submitting those bios, which are executed by a separate task (work
5197 * queue kthread), inode references (inode->i_count) were not taken
5198 * (which would be dropped in the end io callback of each bio).
5199 * Therefore here we effectively end up waiting for those bios and
5200 * anyone else holding locked ranges without having bumped the inode's
5201 * reference count - if we don't do it, when they access the inode's
5202 * io_tree to unlock a range it may be too late, leading to an
5203 * use-after-free issue.
5204 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005205 spin_lock(&io_tree->lock);
5206 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5207 struct extent_state *state;
5208 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005209 u64 start;
5210 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005211
5212 node = rb_first(&io_tree->state);
5213 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005214 start = state->start;
5215 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005216 spin_unlock(&io_tree->lock);
5217
David Sterbaff13db42015-12-03 14:30:40 +01005218 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005219
5220 /*
5221 * If still has DELALLOC flag, the extent didn't reach disk,
5222 * and its reserved space won't be freed by delayed_ref.
5223 * So we need to free its reserved space here.
5224 * (Refer to comment in btrfs_invalidatepage, case 2)
5225 *
5226 * Note, end is the bytenr of last byte, so we need + 1 here.
5227 */
5228 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005229 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005230
Filipe Manana6ca07092015-05-26 00:55:42 +01005231 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005232 EXTENT_LOCKED | EXTENT_DIRTY |
5233 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5234 EXTENT_DEFRAG, 1, 1,
5235 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005236
Filipe Manana7064dd52014-08-08 02:47:05 +01005237 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005238 spin_lock(&io_tree->lock);
5239 }
5240 spin_unlock(&io_tree->lock);
5241}
5242
Al Virobd555972010-06-07 11:35:40 -04005243void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005244{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005245 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005246 struct btrfs_trans_handle *trans;
5247 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005248 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005249 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005250 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005251 int ret;
5252
liubo1abe9b82011-03-24 11:18:59 +00005253 trace_btrfs_inode_evict(inode);
5254
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005255 if (!root) {
5256 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5257 return;
5258 }
5259
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005260 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005261
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005262 evict_inode_truncate_pages(inode);
5263
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005264 if (inode->i_nlink &&
5265 ((btrfs_root_refs(&root->root_item) != 0 &&
5266 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005267 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005268 goto no_delete;
5269
Chris Mason39279cc2007-06-12 06:35:45 -04005270 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005271 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005272 goto no_delete;
5273 }
Al Virobd555972010-06-07 11:35:40 -04005274 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005275 if (!special_file(inode->i_mode))
5276 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005277
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005278 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005279
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005280 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005281 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005282 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005283 goto no_delete;
5284 }
5285
Yan, Zheng76dda932009-09-21 16:00:26 -04005286 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005287 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5288 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005289 goto no_delete;
5290 }
5291
Nikolay Borisovaa790212017-01-10 20:35:40 +02005292 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005293 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005294 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005295 goto no_delete;
5296 }
5297
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005298 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005299 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005300 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005301 goto no_delete;
5302 }
Josef Bacik4a338542011-08-29 11:01:31 -04005303 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005304 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005305 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005306
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005307 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005308
Josef Bacik4289a662011-08-05 13:22:24 -04005309 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005310 * This is a bit simpler than btrfs_truncate since we've already
5311 * reserved our space for our orphan item in the unlink, so we just
5312 * need to reserve some slack space in case we add bytes and update
5313 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005314 */
Yan, Zheng80825102009-11-12 09:35:36 +00005315 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005316 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5317 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005318
Josef Bacik726c35f2011-09-26 15:46:06 -04005319 /*
5320 * Try and steal from the global reserve since we will
5321 * likely not use this space anyway, we want to try as
5322 * hard as possible to get this to work.
5323 */
5324 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005325 steal_from_global++;
5326 else
5327 steal_from_global = 0;
5328 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005329
Josef Bacik3bce8762015-02-24 12:35:51 -08005330 /*
5331 * steal_from_global == 0: we reserved stuff, hooray!
5332 * steal_from_global == 1: we didn't reserve stuff, boo!
5333 * steal_from_global == 2: we've committed, still not a lot of
5334 * room but maybe we'll have room in the global reserve this
5335 * time.
5336 * steal_from_global == 3: abandon all hope!
5337 */
5338 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005339 btrfs_warn(fs_info,
5340 "Could not get space for a delete, will truncate on mount %d",
5341 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005342 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005343 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005344 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005345 }
5346
Miao Xie0e8c36a2012-12-19 06:59:51 +00005347 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005348 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005349 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005350 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005351 goto no_delete;
5352 }
5353
Josef Bacik3bce8762015-02-24 12:35:51 -08005354 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005355 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005356 * sure there is room to do it, if not we need to commit and try
5357 * again.
5358 */
5359 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005360 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005361 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005362 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005363 else
5364 ret = -ENOSPC;
5365 }
5366
5367 /*
5368 * Couldn't steal from the global reserve, we have too much
5369 * pending stuff built up, commit the transaction and try it
5370 * again.
5371 */
5372 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005373 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005374 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005375 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005376 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005377 goto no_delete;
5378 }
5379 continue;
5380 } else {
5381 steal_from_global = 0;
5382 }
5383
Josef Bacik4289a662011-08-05 13:22:24 -04005384 trans->block_rsv = rsv;
5385
Yan, Zhengd68fc572010-05-16 10:49:58 -04005386 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005387 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005388 break;
5389
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005390 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005391 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005392 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005393 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005394 }
5395
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005396 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005397
Josef Bacik4ef31a42013-08-13 14:10:08 -04005398 /*
5399 * Errors here aren't a big deal, it just means we leave orphan items
5400 * in the tree. They will be cleaned up on the next mount.
5401 */
Yan, Zheng80825102009-11-12 09:35:36 +00005402 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005403 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005404 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005405 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005406 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005407 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005408
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005409 trans->block_rsv = &fs_info->trans_block_rsv;
5410 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005411 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005412 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005413
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005414 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005415 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005416no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005417 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005418 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005419}
5420
5421/*
5422 * this returns the key found in the dir entry in the location pointer.
5423 * If no dir entries were found, location->objectid is 0.
5424 */
5425static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5426 struct btrfs_key *location)
5427{
5428 const char *name = dentry->d_name.name;
5429 int namelen = dentry->d_name.len;
5430 struct btrfs_dir_item *di;
5431 struct btrfs_path *path;
5432 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005433 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005434
5435 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005436 if (!path)
5437 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005438
David Sterbaf85b7372017-01-20 14:54:07 +01005439 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5440 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005441 if (IS_ERR(di))
5442 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005443
David Sterbac7040052011-04-19 18:00:01 +02005444 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005445 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005446
Chris Mason5f39d392007-10-15 16:14:19 -04005447 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005448 if (location->type != BTRFS_INODE_ITEM_KEY &&
5449 location->type != BTRFS_ROOT_ITEM_KEY) {
5450 btrfs_warn(root->fs_info,
5451"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5452 __func__, name, btrfs_ino(BTRFS_I(dir)),
5453 location->objectid, location->type, location->offset);
5454 goto out_err;
5455 }
Chris Mason39279cc2007-06-12 06:35:45 -04005456out:
Chris Mason39279cc2007-06-12 06:35:45 -04005457 btrfs_free_path(path);
5458 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005459out_err:
5460 location->objectid = 0;
5461 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005462}
5463
5464/*
5465 * when we hit a tree root in a directory, the btrfs part of the inode
5466 * needs to be changed to reflect the root directory of the tree root. This
5467 * is kind of like crossing a mount point.
5468 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005469static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005470 struct inode *dir,
5471 struct dentry *dentry,
5472 struct btrfs_key *location,
5473 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005474{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005475 struct btrfs_path *path;
5476 struct btrfs_root *new_root;
5477 struct btrfs_root_ref *ref;
5478 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005479 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005480 int ret;
5481 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005482
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005483 path = btrfs_alloc_path();
5484 if (!path) {
5485 err = -ENOMEM;
5486 goto out;
5487 }
Chris Mason39279cc2007-06-12 06:35:45 -04005488
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005489 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005490 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5491 key.type = BTRFS_ROOT_REF_KEY;
5492 key.offset = location->objectid;
5493
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005494 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005495 if (ret) {
5496 if (ret < 0)
5497 err = ret;
5498 goto out;
5499 }
Chris Mason39279cc2007-06-12 06:35:45 -04005500
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005501 leaf = path->nodes[0];
5502 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005503 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005504 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5505 goto out;
5506
5507 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5508 (unsigned long)(ref + 1),
5509 dentry->d_name.len);
5510 if (ret)
5511 goto out;
5512
David Sterbab3b4aa72011-04-21 01:20:15 +02005513 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005515 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005516 if (IS_ERR(new_root)) {
5517 err = PTR_ERR(new_root);
5518 goto out;
5519 }
5520
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005521 *sub_root = new_root;
5522 location->objectid = btrfs_root_dirid(&new_root->root_item);
5523 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005524 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005525 err = 0;
5526out:
5527 btrfs_free_path(path);
5528 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005529}
5530
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005531static void inode_tree_add(struct inode *inode)
5532{
5533 struct btrfs_root *root = BTRFS_I(inode)->root;
5534 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005535 struct rb_node **p;
5536 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005537 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005538 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005539
Al Viro1d3382cb2010-10-23 15:19:20 -04005540 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005541 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005542 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005543 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005544 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005545 while (*p) {
5546 parent = *p;
5547 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5548
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005549 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005550 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005551 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005552 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005553 else {
5554 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005555 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005556 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005557 RB_CLEAR_NODE(parent);
5558 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005559 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005560 }
5561 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005562 rb_link_node(new, parent, p);
5563 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005564 spin_unlock(&root->inode_lock);
5565}
5566
5567static void inode_tree_del(struct inode *inode)
5568{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005569 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005570 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005571 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005572
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005573 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005574 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005575 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005576 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005577 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005578 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005579 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005580
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005581 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005582 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005583 spin_lock(&root->inode_lock);
5584 empty = RB_EMPTY_ROOT(&root->inode_tree);
5585 spin_unlock(&root->inode_lock);
5586 if (empty)
5587 btrfs_add_dead_root(root);
5588 }
5589}
5590
Jeff Mahoney143bede2012-03-01 14:56:26 +01005591void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005592{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005593 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005594 struct rb_node *node;
5595 struct rb_node *prev;
5596 struct btrfs_inode *entry;
5597 struct inode *inode;
5598 u64 objectid = 0;
5599
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005600 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005601 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005602
5603 spin_lock(&root->inode_lock);
5604again:
5605 node = root->inode_tree.rb_node;
5606 prev = NULL;
5607 while (node) {
5608 prev = node;
5609 entry = rb_entry(node, struct btrfs_inode, rb_node);
5610
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005611 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005612 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005613 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005614 node = node->rb_right;
5615 else
5616 break;
5617 }
5618 if (!node) {
5619 while (prev) {
5620 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005621 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005622 node = prev;
5623 break;
5624 }
5625 prev = rb_next(prev);
5626 }
5627 }
5628 while (node) {
5629 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005630 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005631 inode = igrab(&entry->vfs_inode);
5632 if (inode) {
5633 spin_unlock(&root->inode_lock);
5634 if (atomic_read(&inode->i_count) > 1)
5635 d_prune_aliases(inode);
5636 /*
Al Viro45321ac2010-06-07 13:43:19 -04005637 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005638 * the inode cache when its usage count
5639 * hits zero.
5640 */
5641 iput(inode);
5642 cond_resched();
5643 spin_lock(&root->inode_lock);
5644 goto again;
5645 }
5646
5647 if (cond_resched_lock(&root->inode_lock))
5648 goto again;
5649
5650 node = rb_next(node);
5651 }
5652 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005653}
5654
Chris Masone02119d2008-09-05 16:13:11 -04005655static int btrfs_init_locked_inode(struct inode *inode, void *p)
5656{
5657 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005658 inode->i_ino = args->location->objectid;
5659 memcpy(&BTRFS_I(inode)->location, args->location,
5660 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005661 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005662 return 0;
5663}
5664
5665static int btrfs_find_actor(struct inode *inode, void *opaque)
5666{
5667 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005668 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005669 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005670}
5671
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005672static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005673 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005674 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005675{
5676 struct inode *inode;
5677 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005678 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005679
Chris Mason90d3e592014-01-09 17:28:00 -08005680 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005681 args.root = root;
5682
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005683 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005684 btrfs_init_locked_inode,
5685 (void *)&args);
5686 return inode;
5687}
5688
Balaji Rao1a54ef82008-07-21 02:01:04 +05305689/* Get an inode object given its location and corresponding root.
5690 * Returns in *is_new if the inode was read from disk
5691 */
5692struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005693 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305694{
5695 struct inode *inode;
5696
Chris Mason90d3e592014-01-09 17:28:00 -08005697 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305698 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005699 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305700
5701 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005702 int ret;
5703
5704 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005705 if (!is_bad_inode(inode)) {
5706 inode_tree_add(inode);
5707 unlock_new_inode(inode);
5708 if (new)
5709 *new = 1;
5710 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005711 unlock_new_inode(inode);
5712 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005713 ASSERT(ret < 0);
5714 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005715 }
5716 }
5717
Balaji Rao1a54ef82008-07-21 02:01:04 +05305718 return inode;
5719}
5720
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005721static struct inode *new_simple_dir(struct super_block *s,
5722 struct btrfs_key *key,
5723 struct btrfs_root *root)
5724{
5725 struct inode *inode = new_inode(s);
5726
5727 if (!inode)
5728 return ERR_PTR(-ENOMEM);
5729
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005730 BTRFS_I(inode)->root = root;
5731 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005732 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005733
5734 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005735 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005736 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005737 inode->i_fop = &simple_dir_operations;
5738 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005739 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305740 inode->i_atime = inode->i_mtime;
5741 inode->i_ctime = inode->i_mtime;
5742 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005743
5744 return inode;
5745}
5746
Chris Mason3de45862008-11-17 21:02:50 -05005747struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005748{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005749 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005750 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005751 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005752 struct btrfs_root *sub_root = root;
5753 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005754 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005755 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005756
5757 if (dentry->d_name.len > BTRFS_NAME_LEN)
5758 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005759
Jeff Layton39e3c952012-11-28 11:30:53 -05005760 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005761 if (ret < 0)
5762 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005763
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005764 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005765 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005766
5767 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005768 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005769 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005770 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005771
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005772 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005773 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005774 &location, &sub_root);
5775 if (ret < 0) {
5776 if (ret != -ENOENT)
5777 inode = ERR_PTR(ret);
5778 else
5779 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5780 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005781 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005782 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005783 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005784
Julia Lawall34d19ba2011-01-24 19:55:19 +00005785 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005786 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005787 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005788 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005789 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005790 if (ret) {
5791 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005792 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005793 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005794 }
5795
Chris Mason3de45862008-11-17 21:02:50 -05005796 return inode;
5797}
5798
Nick Pigginfe15ce42011-01-07 17:49:23 +11005799static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005800{
5801 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005802 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005803
Li Zefan848cce02012-02-21 17:04:28 +08005804 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005805 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005806
Li Zefan848cce02012-02-21 17:04:28 +08005807 if (inode) {
5808 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005809 if (btrfs_root_refs(&root->root_item) == 0)
5810 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005811
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005812 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005813 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005814 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005815 return 0;
5816}
5817
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005818static void btrfs_dentry_release(struct dentry *dentry)
5819{
Daeseok Youn944a4512014-04-14 15:37:02 +09005820 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005821}
5822
Chris Mason3de45862008-11-17 21:02:50 -05005823static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005824 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005825{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005826 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005827
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005828 inode = btrfs_lookup_dentry(dir, dentry);
5829 if (IS_ERR(inode)) {
5830 if (PTR_ERR(inode) == -ENOENT)
5831 inode = NULL;
5832 else
5833 return ERR_CAST(inode);
5834 }
5835
Al Viro41d28bc2014-10-12 22:24:21 -04005836 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005837}
5838
Miao Xie16cdcec2011-04-22 18:12:22 +08005839unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005840 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5841};
5842
Josef Bacik23b5ec72017-07-24 15:14:25 -04005843/*
5844 * All this infrastructure exists because dir_emit can fault, and we are holding
5845 * the tree lock when doing readdir. For now just allocate a buffer and copy
5846 * our information into that, and then dir_emit from the buffer. This is
5847 * similar to what NFS does, only we don't keep the buffer around in pagecache
5848 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5849 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5850 * tree lock.
5851 */
5852static int btrfs_opendir(struct inode *inode, struct file *file)
5853{
5854 struct btrfs_file_private *private;
5855
5856 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5857 if (!private)
5858 return -ENOMEM;
5859 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5860 if (!private->filldir_buf) {
5861 kfree(private);
5862 return -ENOMEM;
5863 }
5864 file->private_data = private;
5865 return 0;
5866}
5867
5868struct dir_entry {
5869 u64 ino;
5870 u64 offset;
5871 unsigned type;
5872 int name_len;
5873};
5874
5875static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5876{
5877 while (entries--) {
5878 struct dir_entry *entry = addr;
5879 char *name = (char *)(entry + 1);
5880
5881 ctx->pos = entry->offset;
5882 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5883 entry->type))
5884 return 1;
5885 addr += sizeof(struct dir_entry) + entry->name_len;
5886 ctx->pos++;
5887 }
5888 return 0;
5889}
5890
Al Viro9cdda8d2013-05-22 16:48:09 -04005891static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005892{
Al Viro9cdda8d2013-05-22 16:48:09 -04005893 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005894 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005895 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005896 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005897 struct btrfs_dir_item *di;
5898 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005899 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005900 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005901 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005902 struct list_head ins_list;
5903 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005904 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005905 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005906 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005907 char *name_ptr;
5908 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005909 int entries = 0;
5910 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005911 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005912 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005913
Al Viro9cdda8d2013-05-22 16:48:09 -04005914 if (!dir_emit_dots(file, ctx))
5915 return 0;
5916
David Woodhouse49593bf2008-08-17 17:08:36 +01005917 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005918 if (!path)
5919 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005920
Josef Bacik23b5ec72017-07-24 15:14:25 -04005921 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005922 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005923
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005924 INIT_LIST_HEAD(&ins_list);
5925 INIT_LIST_HEAD(&del_list);
5926 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005927
Josef Bacik23b5ec72017-07-24 15:14:25 -04005928again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005929 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005930 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005931 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005932
Chris Mason39279cc2007-06-12 06:35:45 -04005933 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5934 if (ret < 0)
5935 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005936
5937 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005938 struct dir_entry *entry;
5939
Chris Mason5f39d392007-10-15 16:14:19 -04005940 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005941 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005942 if (slot >= btrfs_header_nritems(leaf)) {
5943 ret = btrfs_next_leaf(root, path);
5944 if (ret < 0)
5945 goto err;
5946 else if (ret > 0)
5947 break;
5948 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005949 }
Chris Mason3de45862008-11-17 21:02:50 -05005950
Chris Mason5f39d392007-10-15 16:14:19 -04005951 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5952
5953 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005954 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005955 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005956 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005957 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005958 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005959 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005960 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005961 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Su Yuee79a3322017-06-06 17:57:01 +08005962 if (verify_dir_item(fs_info, leaf, slot, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005963 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005964
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005965 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005966 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5967 PAGE_SIZE) {
5968 btrfs_release_path(path);
5969 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5970 if (ret)
5971 goto nopos;
5972 addr = private->filldir_buf;
5973 entries = 0;
5974 total_len = 0;
5975 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005976 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005977
5978 entry = addr;
5979 entry->name_len = name_len;
5980 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005981 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5982 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005983 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005984 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005985 entry->ino = location.objectid;
5986 entry->offset = found_key.offset;
5987 entries++;
5988 addr += sizeof(struct dir_entry) + name_len;
5989 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005990next:
5991 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005992 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005993 btrfs_release_path(path);
5994
5995 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5996 if (ret)
5997 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005998
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005999 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006000 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006001 goto nopos;
6002
Zach Browndb62efb2013-07-11 16:19:42 -07006003 /*
6004 * Stop new entries from being returned after we return the last
6005 * entry.
6006 *
6007 * New directory entries are assigned a strictly increasing
6008 * offset. This means that new entries created during readdir
6009 * are *guaranteed* to be seen in the future by that readdir.
6010 * This has broken buggy programs which operate on names as
6011 * they're returned by readdir. Until we re-use freed offsets
6012 * we have this hack to stop new entries from being returned
6013 * under the assumption that they'll never reach this huge
6014 * offset.
6015 *
6016 * This is being careful not to overflow 32bit loff_t unless the
6017 * last entry requires it because doing so has broken 32bit apps
6018 * in the past.
6019 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006020 if (ctx->pos >= INT_MAX)
6021 ctx->pos = LLONG_MAX;
6022 else
6023 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006024nopos:
6025 ret = 0;
6026err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006027 if (put)
6028 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006029 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006030 return ret;
6031}
6032
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006033int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006034{
6035 struct btrfs_root *root = BTRFS_I(inode)->root;
6036 struct btrfs_trans_handle *trans;
6037 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006038 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006039
Josef Bacik72ac3c02012-05-23 14:13:11 -04006040 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006041 return 0;
6042
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006043 if (btrfs_fs_closing(root->fs_info) &&
6044 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006045 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006046
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006047 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006048 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006049 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006050 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006051 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006052 if (IS_ERR(trans))
6053 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006054 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006055 }
6056 return ret;
6057}
6058
6059/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006060 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006061 * inode changes. But, it is most likely to find the inode in cache.
6062 * FIXME, needs more benchmarking...there are no reasons other than performance
6063 * to keep or drop this code.
6064 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006065static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006066{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006067 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006068 struct btrfs_root *root = BTRFS_I(inode)->root;
6069 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006070 int ret;
6071
Josef Bacik72ac3c02012-05-23 14:13:11 -04006072 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006073 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006074
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006075 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006076 if (IS_ERR(trans))
6077 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006078
6079 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006080 if (ret && ret == -ENOSPC) {
6081 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006082 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006083 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006084 if (IS_ERR(trans))
6085 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006086
Chris Mason94b60442010-05-26 11:02:00 -04006087 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006088 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006089 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006090 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006091 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006092
6093 return ret;
6094}
6095
6096/*
6097 * This is a copy of file_update_time. We need this so we can return error on
6098 * ENOSPC for updating the inode in the case of file write and mmap writes.
6099 */
Josef Bacike41f9412012-03-26 09:46:47 -04006100static int btrfs_update_time(struct inode *inode, struct timespec *now,
6101 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006102{
Alexander Block2bc5565282012-06-15 09:49:33 +02006103 struct btrfs_root *root = BTRFS_I(inode)->root;
6104
6105 if (btrfs_root_readonly(root))
6106 return -EROFS;
6107
Josef Bacike41f9412012-03-26 09:46:47 -04006108 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006109 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006110 if (flags & S_CTIME)
6111 inode->i_ctime = *now;
6112 if (flags & S_MTIME)
6113 inode->i_mtime = *now;
6114 if (flags & S_ATIME)
6115 inode->i_atime = *now;
6116 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006117}
6118
Chris Masond352ac62008-09-29 15:18:18 -04006119/*
6120 * find the highest existing sequence number in a directory
6121 * and then set the in-memory index_cnt variable to reflect
6122 * free sequence numbers
6123 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006124static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006125{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006126 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006127 struct btrfs_key key, found_key;
6128 struct btrfs_path *path;
6129 struct extent_buffer *leaf;
6130 int ret;
6131
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006132 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006133 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006134 key.offset = (u64)-1;
6135
6136 path = btrfs_alloc_path();
6137 if (!path)
6138 return -ENOMEM;
6139
6140 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6141 if (ret < 0)
6142 goto out;
6143 /* FIXME: we should be able to handle this */
6144 if (ret == 0)
6145 goto out;
6146 ret = 0;
6147
6148 /*
6149 * MAGIC NUMBER EXPLANATION:
6150 * since we search a directory based on f_pos we have to start at 2
6151 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6152 * else has to start at 2
6153 */
6154 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006155 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006156 goto out;
6157 }
6158
6159 path->slots[0]--;
6160
6161 leaf = path->nodes[0];
6162 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6163
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006164 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006165 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006166 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006167 goto out;
6168 }
6169
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006170 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006171out:
6172 btrfs_free_path(path);
6173 return ret;
6174}
6175
Chris Masond352ac62008-09-29 15:18:18 -04006176/*
6177 * helper to find a free sequence number in a given directory. This current
6178 * code is very simple, later versions will do smarter things in the btree
6179 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006180int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006181{
6182 int ret = 0;
6183
Nikolay Borisov877574e2017-02-20 13:50:33 +02006184 if (dir->index_cnt == (u64)-1) {
6185 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006186 if (ret) {
6187 ret = btrfs_set_inode_index_count(dir);
6188 if (ret)
6189 return ret;
6190 }
Josef Bacikaec74772008-07-24 12:12:38 -04006191 }
6192
Nikolay Borisov877574e2017-02-20 13:50:33 +02006193 *index = dir->index_cnt;
6194 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006195
6196 return ret;
6197}
6198
Chris Masonb0d5d102014-09-08 13:08:51 -07006199static int btrfs_insert_inode_locked(struct inode *inode)
6200{
6201 struct btrfs_iget_args args;
6202 args.location = &BTRFS_I(inode)->location;
6203 args.root = BTRFS_I(inode)->root;
6204
6205 return insert_inode_locked4(inode,
6206 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6207 btrfs_find_actor, &args);
6208}
6209
Anand Jain19aee8d2017-07-18 17:37:05 +08006210/*
6211 * Inherit flags from the parent inode.
6212 *
6213 * Currently only the compression flags and the cow flags are inherited.
6214 */
6215static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6216{
6217 unsigned int flags;
6218
6219 if (!dir)
6220 return;
6221
6222 flags = BTRFS_I(dir)->flags;
6223
6224 if (flags & BTRFS_INODE_NOCOMPRESS) {
6225 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6226 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6227 } else if (flags & BTRFS_INODE_COMPRESS) {
6228 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6229 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6230 }
6231
6232 if (flags & BTRFS_INODE_NODATACOW) {
6233 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6234 if (S_ISREG(inode->i_mode))
6235 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6236 }
6237
6238 btrfs_update_iflags(inode);
6239}
6240
Chris Mason39279cc2007-06-12 06:35:45 -04006241static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6242 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006243 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006244 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006245 u64 ref_objectid, u64 objectid,
6246 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006247{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006248 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006249 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006250 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006251 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006252 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006253 struct btrfs_inode_ref *ref;
6254 struct btrfs_key key[2];
6255 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006256 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006257 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006258 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006259
Chris Mason5f39d392007-10-15 16:14:19 -04006260 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006261 if (!path)
6262 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006263
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006264 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006265 if (!inode) {
6266 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006267 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006268 }
Chris Mason39279cc2007-06-12 06:35:45 -04006269
Li Zefan581bb052011-04-20 10:06:11 +08006270 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006271 * O_TMPFILE, set link count to 0, so that after this point,
6272 * we fill in an inode item with the correct link count.
6273 */
6274 if (!name)
6275 set_nlink(inode, 0);
6276
6277 /*
Li Zefan581bb052011-04-20 10:06:11 +08006278 * we have to initialize this early, so we can reclaim the inode
6279 * number if we fail afterwards in this function.
6280 */
6281 inode->i_ino = objectid;
6282
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006283 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006284 trace_btrfs_inode_request(dir);
6285
Nikolay Borisov877574e2017-02-20 13:50:33 +02006286 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006287 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006288 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006289 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006290 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006291 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006292 } else if (dir) {
6293 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006294 }
6295 /*
6296 * index_cnt is ignored for everything but a dir,
6297 * btrfs_get_inode_index_count has an explanation for the magic
6298 * number
6299 */
6300 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006301 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006302 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006303 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006304 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006305
Josef Bacik5dc562c2012-08-17 13:14:17 -04006306 /*
6307 * We could have gotten an inode number from somebody who was fsynced
6308 * and then removed in this same transaction, so let's just set full
6309 * sync since it will be a full sync anyway and this will blow away the
6310 * old info in the log.
6311 */
6312 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6313
Chris Mason9c583092008-01-29 15:15:18 -05006314 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006315 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006316 key[0].offset = 0;
6317
Chris Mason9c583092008-01-29 15:15:18 -05006318 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006319
6320 if (name) {
6321 /*
6322 * Start new inodes with an inode_ref. This is slightly more
6323 * efficient for small numbers of hard links since they will
6324 * be packed into one item. Extended refs will kick in if we
6325 * add more hard links than can fit in the ref item.
6326 */
6327 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006328 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006329 key[1].offset = ref_objectid;
6330
6331 sizes[1] = name_len + sizeof(*ref);
6332 }
Chris Mason9c583092008-01-29 15:15:18 -05006333
Chris Masonb0d5d102014-09-08 13:08:51 -07006334 location = &BTRFS_I(inode)->location;
6335 location->objectid = objectid;
6336 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006337 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006338
6339 ret = btrfs_insert_inode_locked(inode);
6340 if (ret < 0)
6341 goto fail;
6342
Chris Masonb9473432009-03-13 11:00:37 -04006343 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006344 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006345 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006346 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006347
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006348 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006349 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306350
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006351 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306352 inode->i_atime = inode->i_mtime;
6353 inode->i_ctime = inode->i_mtime;
6354 BTRFS_I(inode)->i_otime = inode->i_mtime;
6355
Chris Mason5f39d392007-10-15 16:14:19 -04006356 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6357 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006358 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006359 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006360 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006361
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006362 if (name) {
6363 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6364 struct btrfs_inode_ref);
6365 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6366 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6367 ptr = (unsigned long)(ref + 1);
6368 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6369 }
Chris Mason9c583092008-01-29 15:15:18 -05006370
Chris Mason5f39d392007-10-15 16:14:19 -04006371 btrfs_mark_buffer_dirty(path->nodes[0]);
6372 btrfs_free_path(path);
6373
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006374 btrfs_inherit_iflags(inode, dir);
6375
Al Viro569254b2011-07-24 17:08:40 -04006376 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006377 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006378 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006379 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006380 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6381 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006382 }
6383
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006384 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006385
6386 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006387 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006388
Alexander Block8ea05e32012-07-25 17:35:53 +02006389 btrfs_update_root_times(trans, root);
6390
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006391 ret = btrfs_inode_inherit_props(trans, inode, dir);
6392 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006393 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006394 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006395 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006396
Chris Mason39279cc2007-06-12 06:35:45 -04006397 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006398
6399fail_unlock:
6400 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006401fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006402 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006403 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006404 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006405 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006406 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006407}
6408
6409static inline u8 btrfs_inode_type(struct inode *inode)
6410{
6411 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6412}
6413
Chris Masond352ac62008-09-29 15:18:18 -04006414/*
6415 * utility function to add 'inode' into 'parent_inode' with
6416 * a give name and a given sequence number.
6417 * if 'add_backref' is true, also insert a backref from the
6418 * inode to the parent directory.
6419 */
Chris Masone02119d2008-09-05 16:13:11 -04006420int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006421 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006422 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006423{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006424 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006425 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006426 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006427 struct btrfs_root *root = parent_inode->root;
6428 u64 ino = btrfs_ino(inode);
6429 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006430
Li Zefan33345d012011-04-20 10:31:50 +08006431 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006432 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006433 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006434 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006435 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006436 key.offset = 0;
6437 }
Chris Mason39279cc2007-06-12 06:35:45 -04006438
Li Zefan33345d012011-04-20 10:31:50 +08006439 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006440 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6441 root->root_key.objectid, parent_ino,
6442 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006443 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006444 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6445 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006446 }
6447
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006448 /* Nothing to clean up yet */
6449 if (ret)
6450 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006451
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006452 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6453 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006454 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006455 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006456 goto fail_dir_item;
6457 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006458 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006459 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006460 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006461
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006462 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006463 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006464 inode_inc_iversion(&parent_inode->vfs_inode);
6465 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6466 current_time(&parent_inode->vfs_inode);
6467 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006468 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006469 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006470 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006471
6472fail_dir_item:
6473 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6474 u64 local_index;
6475 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006476 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6477 root->root_key.objectid, parent_ino,
6478 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006479
6480 } else if (add_backref) {
6481 u64 local_index;
6482 int err;
6483
6484 err = btrfs_del_inode_ref(trans, root, name, name_len,
6485 ino, parent_ino, &local_index);
6486 }
6487 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006488}
6489
6490static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006491 struct btrfs_inode *dir, struct dentry *dentry,
6492 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006493{
Josef Bacika1b075d2010-11-19 20:36:11 +00006494 int err = btrfs_add_link(trans, dir, inode,
6495 dentry->d_name.name, dentry->d_name.len,
6496 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006497 if (err > 0)
6498 err = -EEXIST;
6499 return err;
6500}
6501
Josef Bacik618e21d2007-07-11 10:18:17 -04006502static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006503 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006504{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006505 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006506 struct btrfs_trans_handle *trans;
6507 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006508 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006509 int err;
6510 int drop_inode = 0;
6511 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006512 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006513
Josef Bacik9ed74f22009-09-11 16:12:44 -04006514 /*
6515 * 2 for inode item and ref
6516 * 2 for dir items
6517 * 1 for xattr if selinux is on
6518 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006519 trans = btrfs_start_transaction(root, 5);
6520 if (IS_ERR(trans))
6521 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006522
Li Zefan581bb052011-04-20 10:06:11 +08006523 err = btrfs_find_free_ino(root, &objectid);
6524 if (err)
6525 goto out_unlock;
6526
Josef Bacikaec74772008-07-24 12:12:38 -04006527 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006528 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6529 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006530 if (IS_ERR(inode)) {
6531 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006532 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006533 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006534
Casey Schauflerad19db72011-12-15 10:09:07 -05006535 /*
6536 * If the active LSM wants to access the inode during
6537 * d_instantiate it needs these. Smack checks to see
6538 * if the filesystem supports xattrs by looking at the
6539 * ops vector.
6540 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006541 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006542 init_special_inode(inode, inode->i_mode, rdev);
6543
6544 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006545 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006546 goto out_unlock_inode;
6547
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006548 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6549 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006550 if (err) {
6551 goto out_unlock_inode;
6552 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006553 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006554 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006555 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006556 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006557
Josef Bacik618e21d2007-07-11 10:18:17 -04006558out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006559 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006560 btrfs_balance_delayed_items(fs_info);
6561 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006562 if (drop_inode) {
6563 inode_dec_link_count(inode);
6564 iput(inode);
6565 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006566 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006567
6568out_unlock_inode:
6569 drop_inode = 1;
6570 unlock_new_inode(inode);
6571 goto out_unlock;
6572
Josef Bacik618e21d2007-07-11 10:18:17 -04006573}
6574
Chris Mason39279cc2007-06-12 06:35:45 -04006575static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006576 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006577{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006578 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006579 struct btrfs_trans_handle *trans;
6580 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006581 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006582 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006583 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006584 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006585 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006586
Josef Bacik9ed74f22009-09-11 16:12:44 -04006587 /*
6588 * 2 for inode item and ref
6589 * 2 for dir items
6590 * 1 for xattr if selinux is on
6591 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006592 trans = btrfs_start_transaction(root, 5);
6593 if (IS_ERR(trans))
6594 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006595
Li Zefan581bb052011-04-20 10:06:11 +08006596 err = btrfs_find_free_ino(root, &objectid);
6597 if (err)
6598 goto out_unlock;
6599
Josef Bacikaec74772008-07-24 12:12:38 -04006600 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006601 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6602 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006603 if (IS_ERR(inode)) {
6604 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006605 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006606 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006607 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006608 /*
6609 * If the active LSM wants to access the inode during
6610 * d_instantiate it needs these. Smack checks to see
6611 * if the filesystem supports xattrs by looking at the
6612 * ops vector.
6613 */
6614 inode->i_fop = &btrfs_file_operations;
6615 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006616 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006617
6618 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6619 if (err)
6620 goto out_unlock_inode;
6621
6622 err = btrfs_update_inode(trans, root, inode);
6623 if (err)
6624 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006625
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006626 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6627 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006628 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006629 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006630
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006631 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006632 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006633 d_instantiate(dentry, inode);
6634
Chris Mason39279cc2007-06-12 06:35:45 -04006635out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006636 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006637 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006638 inode_dec_link_count(inode);
6639 iput(inode);
6640 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006641 btrfs_balance_delayed_items(fs_info);
6642 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006643 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006644
6645out_unlock_inode:
6646 unlock_new_inode(inode);
6647 goto out_unlock;
6648
Chris Mason39279cc2007-06-12 06:35:45 -04006649}
6650
6651static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6652 struct dentry *dentry)
6653{
Filipe Manana271dba452016-01-05 16:24:05 +00006654 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006655 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006656 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006657 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006658 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006659 int err;
6660 int drop_inode = 0;
6661
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006662 /* do not allow sys_link's with other subvols of the same device */
6663 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006664 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006665
Mark Fashehf1863732012-08-08 11:32:27 -07006666 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006667 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006668
Nikolay Borisov877574e2017-02-20 13:50:33 +02006669 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006670 if (err)
6671 goto fail;
6672
Yan, Zhenga22285a2010-05-16 10:48:46 -04006673 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006674 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006675 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006676 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006677 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006678 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006679 if (IS_ERR(trans)) {
6680 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006681 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006682 goto fail;
6683 }
Chris Mason5f39d392007-10-15 16:14:19 -04006684
Miao Xie67de1172013-12-26 13:07:06 +08006685 /* There are several dir indexes for this inode, clear the cache. */
6686 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006687 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006688 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006689 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006690 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006691 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006692
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006693 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6694 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006695
Yan, Zhenga5719522009-09-24 09:17:31 -04006696 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006697 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006698 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006699 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006700 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006701 if (err)
6702 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006703 if (inode->i_nlink == 1) {
6704 /*
6705 * If new hard link count is 1, it's a file created
6706 * with open(2) O_TMPFILE flag.
6707 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006708 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006709 if (err)
6710 goto fail;
6711 }
Al Viro08c422c2011-12-23 07:58:13 -05006712 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006713 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006714 }
Chris Mason39279cc2007-06-12 06:35:45 -04006715
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006716 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006717fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006718 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006719 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006720 if (drop_inode) {
6721 inode_dec_link_count(inode);
6722 iput(inode);
6723 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006724 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006725 return err;
6726}
6727
Al Viro18bb1db2011-07-26 01:41:39 -04006728static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006729{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006730 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006731 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006732 struct btrfs_trans_handle *trans;
6733 struct btrfs_root *root = BTRFS_I(dir)->root;
6734 int err = 0;
6735 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006736 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006737 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006738
Josef Bacik9ed74f22009-09-11 16:12:44 -04006739 /*
6740 * 2 items for inode and ref
6741 * 2 items for dir items
6742 * 1 for xattr if selinux is on
6743 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006744 trans = btrfs_start_transaction(root, 5);
6745 if (IS_ERR(trans))
6746 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006747
Li Zefan581bb052011-04-20 10:06:11 +08006748 err = btrfs_find_free_ino(root, &objectid);
6749 if (err)
6750 goto out_fail;
6751
Josef Bacikaec74772008-07-24 12:12:38 -04006752 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006753 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6754 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006755 if (IS_ERR(inode)) {
6756 err = PTR_ERR(inode);
6757 goto out_fail;
6758 }
Chris Mason5f39d392007-10-15 16:14:19 -04006759
Chris Mason39279cc2007-06-12 06:35:45 -04006760 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006761 /* these must be set before we unlock the inode */
6762 inode->i_op = &btrfs_dir_inode_operations;
6763 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006764
Eric Paris2a7dba32011-02-01 11:05:39 -05006765 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006766 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006767 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006768
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006769 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006770 err = btrfs_update_inode(trans, root, inode);
6771 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006772 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006773
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006774 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6775 dentry->d_name.name,
6776 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006777 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006778 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006779
Chris Mason39279cc2007-06-12 06:35:45 -04006780 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006781 /*
6782 * mkdir is special. We're unlocking after we call d_instantiate
6783 * to avoid a race with nfsd calling d_instantiate.
6784 */
6785 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006786 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006787
6788out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006789 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006790 if (drop_on_err) {
6791 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006792 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006793 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006794 btrfs_balance_delayed_items(fs_info);
6795 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006796 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006797
6798out_fail_inode:
6799 unlock_new_inode(inode);
6800 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006801}
6802
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006803/* Find next extent map of a given extent map, caller needs to ensure locks */
6804static struct extent_map *next_extent_map(struct extent_map *em)
6805{
6806 struct rb_node *next;
6807
6808 next = rb_next(&em->rb_node);
6809 if (!next)
6810 return NULL;
6811 return container_of(next, struct extent_map, rb_node);
6812}
6813
6814static struct extent_map *prev_extent_map(struct extent_map *em)
6815{
6816 struct rb_node *prev;
6817
6818 prev = rb_prev(&em->rb_node);
6819 if (!prev)
6820 return NULL;
6821 return container_of(prev, struct extent_map, rb_node);
6822}
6823
Chris Masond352ac62008-09-29 15:18:18 -04006824/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006825 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006826 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006827 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006828 */
Chris Mason3b951512008-04-17 11:29:12 -04006829static int merge_extent_mapping(struct extent_map_tree *em_tree,
6830 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006831 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006832 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006833{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006834 struct extent_map *prev;
6835 struct extent_map *next;
6836 u64 start;
6837 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006838 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006839
Chris Masone6dcd2d2008-07-17 12:53:50 -04006840 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006841
6842 if (existing->start > map_start) {
6843 next = existing;
6844 prev = prev_extent_map(next);
6845 } else {
6846 prev = existing;
6847 next = next_extent_map(prev);
6848 }
6849
6850 start = prev ? extent_map_end(prev) : em->start;
6851 start = max_t(u64, start, em->start);
6852 end = next ? next->start : extent_map_end(em);
6853 end = min_t(u64, end, extent_map_end(em));
6854 start_diff = start - em->start;
6855 em->start = start;
6856 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006857 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6858 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006859 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006860 em->block_len -= start_diff;
6861 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006862 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006863}
6864
Chris Masonc8b97812008-10-29 14:49:59 -04006865static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006866 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006867 size_t pg_offset, u64 extent_offset,
6868 struct btrfs_file_extent_item *item)
6869{
6870 int ret;
6871 struct extent_buffer *leaf = path->nodes[0];
6872 char *tmp;
6873 size_t max_size;
6874 unsigned long inline_size;
6875 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006876 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006877
6878 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006879 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006880 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6881 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006882 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006883 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006884 if (!tmp)
6885 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006886 ptr = btrfs_file_extent_inline_start(item);
6887
6888 read_extent_buffer(leaf, tmp, ptr, inline_size);
6889
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006890 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006891 ret = btrfs_decompress(compress_type, tmp, page,
6892 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006893
6894 /*
6895 * decompression code contains a memset to fill in any space between the end
6896 * of the uncompressed data and the end of max_size in case the decompressed
6897 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6898 * the end of an inline extent and the beginning of the next block, so we
6899 * cover that region here.
6900 */
6901
6902 if (max_size + pg_offset < PAGE_SIZE) {
6903 char *map = kmap(page);
6904 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6905 kunmap(page);
6906 }
Chris Masonc8b97812008-10-29 14:49:59 -04006907 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006908 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006909}
6910
Chris Masond352ac62008-09-29 15:18:18 -04006911/*
6912 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006913 * the ugly parts come from merging extents from the disk with the in-ram
6914 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006915 * where the in-ram extents might be locked pending data=ordered completion.
6916 *
6917 * This also copies inline extents directly into the page.
6918 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006919struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6920 struct page *page,
6921 size_t pg_offset, u64 start, u64 len,
6922 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006923{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006924 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006925 int ret;
6926 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006927 u64 extent_start = 0;
6928 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006929 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006930 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006931 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006932 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006933 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006934 struct extent_buffer *leaf;
6935 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006936 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006937 struct extent_map_tree *em_tree = &inode->extent_tree;
6938 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006939 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006940 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006941
Chris Masona52d9a82007-08-27 16:49:44 -04006942again:
Chris Mason890871b2009-09-02 16:24:52 -04006943 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006944 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006945 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006946 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006947 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006948
Chris Masona52d9a82007-08-27 16:49:44 -04006949 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006950 if (em->start > start || em->start + em->len <= start)
6951 free_extent_map(em);
6952 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006953 free_extent_map(em);
6954 else
6955 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006956 }
David Sterba172ddd62011-04-21 00:48:27 +02006957 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006958 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006959 err = -ENOMEM;
6960 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006961 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006962 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006963 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006964 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006965 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006966 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006967
6968 if (!path) {
6969 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006970 if (!path) {
6971 err = -ENOMEM;
6972 goto out;
6973 }
6974 /*
6975 * Chances are we'll be called again, so go ahead and do
6976 * readahead
6977 */
David Sterbae4058b52015-11-27 16:31:35 +01006978 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006979 }
6980
Chris Mason179e29e2007-11-01 11:28:41 -04006981 ret = btrfs_lookup_file_extent(trans, root, path,
6982 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006983 if (ret < 0) {
6984 err = ret;
6985 goto out;
6986 }
6987
6988 if (ret != 0) {
6989 if (path->slots[0] == 0)
6990 goto not_found;
6991 path->slots[0]--;
6992 }
6993
Chris Mason5f39d392007-10-15 16:14:19 -04006994 leaf = path->nodes[0];
6995 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006996 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006997 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006998 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006999 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04007000 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04007001 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007002 /*
7003 * If we backup past the first extent we want to move forward
7004 * and see if there is an extent in front of us, otherwise we'll
7005 * say there is a hole for our whole search range which can
7006 * cause problems.
7007 */
7008 extent_end = start;
7009 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007010 }
7011
Chris Mason5f39d392007-10-15 16:14:19 -04007012 found_type = btrfs_file_extent_type(leaf, item);
7013 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007014 if (found_type == BTRFS_FILE_EXTENT_REG ||
7015 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007016 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007017 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007018
7019 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7020 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007021 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7022 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007023 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007024 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007025 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007026
7027 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7028 path->slots[0],
7029 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007030 }
Josef Bacik25a50342013-10-14 12:08:38 -04007031next:
Yan Zheng9036c102008-10-30 14:19:41 -04007032 if (start >= extent_end) {
7033 path->slots[0]++;
7034 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7035 ret = btrfs_next_leaf(root, path);
7036 if (ret < 0) {
7037 err = ret;
7038 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007039 }
Yan Zheng9036c102008-10-30 14:19:41 -04007040 if (ret > 0)
7041 goto not_found;
7042 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007043 }
Yan Zheng9036c102008-10-30 14:19:41 -04007044 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7045 if (found_key.objectid != objectid ||
7046 found_key.type != BTRFS_EXTENT_DATA_KEY)
7047 goto not_found;
7048 if (start + len <= found_key.offset)
7049 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007050 if (start > found_key.offset)
7051 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007052 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007053 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007054 em->len = found_key.offset - start;
7055 goto not_found_em;
7056 }
7057
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007058 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007059 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007060
Yan Zhengd899e052008-10-30 14:25:28 -04007061 if (found_type == BTRFS_FILE_EXTENT_REG ||
7062 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007063 goto insert;
7064 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007065 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007066 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007067 size_t size;
7068 size_t extent_offset;
7069 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007070
Filipe Manana7ffbb592014-06-09 03:48:05 +01007071 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007072 goto out;
Yan689f9342007-10-29 11:41:07 -04007073
Chris Mason514ac8a2014-01-03 21:07:00 -08007074 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007075 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007076 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7077 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007078 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007079 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007080 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007081 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007082 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007083 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007084 if (btrfs_file_extent_compression(leaf, item) !=
7085 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007086 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007087 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007088 if (ret) {
7089 err = ret;
7090 goto out;
7091 }
Chris Masonc8b97812008-10-29 14:49:59 -04007092 } else {
7093 map = kmap(page);
7094 read_extent_buffer(leaf, map + pg_offset, ptr,
7095 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007096 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007097 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007098 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007099 copy_size);
7100 }
Chris Masonc8b97812008-10-29 14:49:59 -04007101 kunmap(page);
7102 }
Chris Mason179e29e2007-11-01 11:28:41 -04007103 flush_dcache_page(page);
7104 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007105 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007106 if (!trans) {
7107 kunmap(page);
7108 free_extent_map(em);
7109 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007110
David Sterbab3b4aa72011-04-21 01:20:15 +02007111 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007112 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007113
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007114 if (IS_ERR(trans))
7115 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007116 goto again;
7117 }
Chris Masonc8b97812008-10-29 14:49:59 -04007118 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007119 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007120 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007121 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007122 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007123 }
Chris Masond1310b22008-01-24 16:13:08 -05007124 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007125 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007126 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007127 }
7128not_found:
7129 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007130 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007131 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007132not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007133 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007134 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007135insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007136 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007137 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007138 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007139 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7140 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007141 err = -EIO;
7142 goto out;
7143 }
Chris Masond1310b22008-01-24 16:13:08 -05007144
7145 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007146 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007147 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007148 /* it is possible that someone inserted the extent into the tree
7149 * while we had the lock dropped. It is also possible that
7150 * an overlapping map exists in the tree
7151 */
Chris Masona52d9a82007-08-27 16:49:44 -04007152 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007153 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007154
7155 ret = 0;
7156
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007157 existing = search_extent_mapping(em_tree, start, len);
7158 /*
7159 * existing will always be non-NULL, since there must be
7160 * extent causing the -EEXIST.
7161 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007162 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007163 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007164 em->block_start == existing->block_start) {
7165 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007166 * The existing extent map already encompasses the
7167 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007168 */
7169 free_extent_map(em);
7170 em = existing;
7171 err = 0;
7172
7173 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007174 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007175 /*
7176 * The existing extent map is the one nearest to
7177 * the [start, start + len) range which overlaps
7178 */
7179 err = merge_extent_mapping(em_tree, existing,
7180 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007181 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007182 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007183 free_extent_map(em);
7184 em = NULL;
7185 }
7186 } else {
7187 free_extent_map(em);
7188 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007189 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007190 }
Chris Masona52d9a82007-08-27 16:49:44 -04007191 }
Chris Mason890871b2009-09-02 16:24:52 -04007192 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007193out:
liubo1abe9b82011-03-24 11:18:59 +00007194
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007195 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007196
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007197 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007198 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007199 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007200 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007201 err = ret;
7202 }
Chris Masona52d9a82007-08-27 16:49:44 -04007203 if (err) {
7204 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007205 return ERR_PTR(err);
7206 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007207 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007208 return em;
7209}
7210
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007211struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7212 struct page *page,
7213 size_t pg_offset, u64 start, u64 len,
7214 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007215{
7216 struct extent_map *em;
7217 struct extent_map *hole_em = NULL;
7218 u64 range_start = start;
7219 u64 end;
7220 u64 found;
7221 u64 found_end;
7222 int err = 0;
7223
7224 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7225 if (IS_ERR(em))
7226 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007227 /*
7228 * If our em maps to:
7229 * - a hole or
7230 * - a pre-alloc extent,
7231 * there might actually be delalloc bytes behind it.
7232 */
7233 if (em->block_start != EXTENT_MAP_HOLE &&
7234 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7235 return em;
7236 else
7237 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007238
7239 /* check to see if we've wrapped (len == -1 or similar) */
7240 end = start + len;
7241 if (end < start)
7242 end = (u64)-1;
7243 else
7244 end -= 1;
7245
7246 em = NULL;
7247
7248 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007249 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007250 end, len, EXTENT_DELALLOC, 1);
7251 found_end = range_start + found;
7252 if (found_end < range_start)
7253 found_end = (u64)-1;
7254
7255 /*
7256 * we didn't find anything useful, return
7257 * the original results from get_extent()
7258 */
7259 if (range_start > end || found_end <= start) {
7260 em = hole_em;
7261 hole_em = NULL;
7262 goto out;
7263 }
7264
7265 /* adjust the range_start to make sure it doesn't
7266 * go backwards from the start they passed in
7267 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307268 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007269 found = found_end - range_start;
7270
7271 if (found > 0) {
7272 u64 hole_start = start;
7273 u64 hole_len = len;
7274
David Sterba172ddd62011-04-21 00:48:27 +02007275 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007276 if (!em) {
7277 err = -ENOMEM;
7278 goto out;
7279 }
7280 /*
7281 * when btrfs_get_extent can't find anything it
7282 * returns one huge hole
7283 *
7284 * make sure what it found really fits our range, and
7285 * adjust to make sure it is based on the start from
7286 * the caller
7287 */
7288 if (hole_em) {
7289 u64 calc_end = extent_map_end(hole_em);
7290
7291 if (calc_end <= start || (hole_em->start > end)) {
7292 free_extent_map(hole_em);
7293 hole_em = NULL;
7294 } else {
7295 hole_start = max(hole_em->start, start);
7296 hole_len = calc_end - hole_start;
7297 }
7298 }
7299 em->bdev = NULL;
7300 if (hole_em && range_start > hole_start) {
7301 /* our hole starts before our delalloc, so we
7302 * have to return just the parts of the hole
7303 * that go until the delalloc starts
7304 */
7305 em->len = min(hole_len,
7306 range_start - hole_start);
7307 em->start = hole_start;
7308 em->orig_start = hole_start;
7309 /*
7310 * don't adjust block start at all,
7311 * it is fixed at EXTENT_MAP_HOLE
7312 */
7313 em->block_start = hole_em->block_start;
7314 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007315 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7316 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007317 } else {
7318 em->start = range_start;
7319 em->len = found;
7320 em->orig_start = range_start;
7321 em->block_start = EXTENT_MAP_DELALLOC;
7322 em->block_len = found;
7323 }
7324 } else if (hole_em) {
7325 return hole_em;
7326 }
7327out:
7328
7329 free_extent_map(hole_em);
7330 if (err) {
7331 free_extent_map(em);
7332 return ERR_PTR(err);
7333 }
7334 return em;
7335}
7336
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007337static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7338 const u64 start,
7339 const u64 len,
7340 const u64 orig_start,
7341 const u64 block_start,
7342 const u64 block_len,
7343 const u64 orig_block_len,
7344 const u64 ram_bytes,
7345 const int type)
7346{
7347 struct extent_map *em = NULL;
7348 int ret;
7349
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007350 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007351 em = create_io_em(inode, start, len, orig_start,
7352 block_start, block_len, orig_block_len,
7353 ram_bytes,
7354 BTRFS_COMPRESS_NONE, /* compress_type */
7355 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007356 if (IS_ERR(em))
7357 goto out;
7358 }
7359 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7360 len, block_len, type);
7361 if (ret) {
7362 if (em) {
7363 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007364 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007365 start + len - 1, 0);
7366 }
7367 em = ERR_PTR(ret);
7368 }
7369 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007370
7371 return em;
7372}
7373
Josef Bacik4b46fce2010-05-23 11:00:55 -04007374static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7375 u64 start, u64 len)
7376{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007377 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007378 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007379 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007380 struct btrfs_key ins;
7381 u64 alloc_hint;
7382 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007383
Josef Bacik4b46fce2010-05-23 11:00:55 -04007384 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007385 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007386 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007387 if (ret)
7388 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007389
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007390 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7391 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007392 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007393 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007394 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007395 btrfs_free_reserved_extent(fs_info, ins.objectid,
7396 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007397
Josef Bacik4b46fce2010-05-23 11:00:55 -04007398 return em;
7399}
7400
Chris Mason46bfbb52010-05-26 11:04:10 -04007401/*
7402 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7403 * block must be cow'd
7404 */
Josef Bacik00361582013-08-14 14:02:47 -04007405noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007406 u64 *orig_start, u64 *orig_block_len,
7407 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007408{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007409 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007410 struct btrfs_path *path;
7411 int ret;
7412 struct extent_buffer *leaf;
7413 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007414 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007415 struct btrfs_file_extent_item *fi;
7416 struct btrfs_key key;
7417 u64 disk_bytenr;
7418 u64 backref_offset;
7419 u64 extent_end;
7420 u64 num_bytes;
7421 int slot;
7422 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007423 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007424
Chris Mason46bfbb52010-05-26 11:04:10 -04007425 path = btrfs_alloc_path();
7426 if (!path)
7427 return -ENOMEM;
7428
David Sterbaf85b7372017-01-20 14:54:07 +01007429 ret = btrfs_lookup_file_extent(NULL, root, path,
7430 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007431 if (ret < 0)
7432 goto out;
7433
7434 slot = path->slots[0];
7435 if (ret == 1) {
7436 if (slot == 0) {
7437 /* can't find the item, must cow */
7438 ret = 0;
7439 goto out;
7440 }
7441 slot--;
7442 }
7443 ret = 0;
7444 leaf = path->nodes[0];
7445 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007446 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007447 key.type != BTRFS_EXTENT_DATA_KEY) {
7448 /* not our file or wrong item type, must cow */
7449 goto out;
7450 }
7451
7452 if (key.offset > offset) {
7453 /* Wrong offset, must cow */
7454 goto out;
7455 }
7456
7457 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7458 found_type = btrfs_file_extent_type(leaf, fi);
7459 if (found_type != BTRFS_FILE_EXTENT_REG &&
7460 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7461 /* not a regular extent, must cow */
7462 goto out;
7463 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007464
7465 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7466 goto out;
7467
Miao Xiee77751a2013-12-27 21:11:50 +08007468 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7469 if (extent_end <= offset)
7470 goto out;
7471
Chris Mason46bfbb52010-05-26 11:04:10 -04007472 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007473 if (disk_bytenr == 0)
7474 goto out;
7475
7476 if (btrfs_file_extent_compression(leaf, fi) ||
7477 btrfs_file_extent_encryption(leaf, fi) ||
7478 btrfs_file_extent_other_encoding(leaf, fi))
7479 goto out;
7480
Chris Mason46bfbb52010-05-26 11:04:10 -04007481 backref_offset = btrfs_file_extent_offset(leaf, fi);
7482
Josef Bacik7ee9e442013-06-21 16:37:03 -04007483 if (orig_start) {
7484 *orig_start = key.offset - backref_offset;
7485 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7486 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7487 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007488
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007489 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007490 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007491
7492 num_bytes = min(offset + *len, extent_end) - offset;
7493 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7494 u64 range_end;
7495
Jeff Mahoneyda170662016-06-15 09:22:56 -04007496 range_end = round_up(offset + num_bytes,
7497 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007498 ret = test_range_bit(io_tree, offset, range_end,
7499 EXTENT_DELALLOC, 0, NULL);
7500 if (ret) {
7501 ret = -EAGAIN;
7502 goto out;
7503 }
7504 }
7505
Josef Bacik1bda19e2013-10-18 12:10:36 -04007506 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007507
7508 /*
7509 * look for other files referencing this extent, if we
7510 * find any we must cow
7511 */
Josef Bacik00361582013-08-14 14:02:47 -04007512
Liu Boe4c3b2d2017-01-30 12:25:28 -08007513 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007514 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007515 if (ret) {
7516 ret = 0;
7517 goto out;
7518 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007519
7520 /*
7521 * adjust disk_bytenr and num_bytes to cover just the bytes
7522 * in this extent we are about to write. If there
7523 * are any csums in that range we have to cow in order
7524 * to keep the csums correct
7525 */
7526 disk_bytenr += backref_offset;
7527 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007528 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7529 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007530 /*
7531 * all of the above have passed, it is safe to overwrite this extent
7532 * without cow
7533 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007534 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007535 ret = 1;
7536out:
7537 btrfs_free_path(path);
7538 return ret;
7539}
7540
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007541bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7542{
7543 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007544 bool found = false;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007545 void **pagep = NULL;
7546 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007547 unsigned long start_idx;
7548 unsigned long end_idx;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007549
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007550 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007551
7552 /*
7553 * end is the last byte in the last page. end == start is legal
7554 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007555 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007556
7557 rcu_read_lock();
7558
7559 /* Most of the code in this while loop is lifted from
7560 * find_get_page. It's been modified to begin searching from a
7561 * page and return just the first page found in that range. If the
7562 * found idx is less than or equal to the end idx then we know that
7563 * a page exists. If no pages are found or if those pages are
7564 * outside of the range then we're fine (yay!) */
7565 while (page == NULL &&
7566 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7567 page = radix_tree_deref_slot(pagep);
7568 if (unlikely(!page))
7569 break;
7570
7571 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007572 if (radix_tree_deref_retry(page)) {
7573 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007574 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007575 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007576 /*
7577 * Otherwise, shmem/tmpfs must be storing a swap entry
7578 * here as an exceptional entry: so return it without
7579 * attempting to raise page count.
7580 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007581 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007582 break; /* TODO: Is this relevant for this use case? */
7583 }
7584
Filipe Manana91405152014-06-05 13:22:24 +01007585 if (!page_cache_get_speculative(page)) {
7586 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007587 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007588 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007589
7590 /*
7591 * Has the page moved?
7592 * This is part of the lockless pagecache protocol. See
7593 * include/linux/pagemap.h for details.
7594 */
7595 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007596 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007597 page = NULL;
7598 }
7599 }
7600
7601 if (page) {
7602 if (page->index <= end_idx)
7603 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007604 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007605 }
7606
7607 rcu_read_unlock();
7608 return found;
7609}
7610
Josef Bacikeb838e72012-07-31 16:28:48 -04007611static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7612 struct extent_state **cached_state, int writing)
7613{
7614 struct btrfs_ordered_extent *ordered;
7615 int ret = 0;
7616
7617 while (1) {
7618 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007619 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007620 /*
7621 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007622 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007623 * extents in this range.
7624 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007625 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007626 lockend - lockstart + 1);
7627
7628 /*
7629 * We need to make sure there are no buffered pages in this
7630 * range either, we could have raced between the invalidate in
7631 * generic_file_direct_write and locking the extent. The
7632 * invalidate needs to happen so that reads after a write do not
7633 * get stale data.
7634 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007635 if (!ordered &&
7636 (!writing ||
7637 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007638 break;
7639
7640 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7641 cached_state, GFP_NOFS);
7642
7643 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007644 /*
7645 * If we are doing a DIO read and the ordered extent we
7646 * found is for a buffered write, we can not wait for it
7647 * to complete and retry, because if we do so we can
7648 * deadlock with concurrent buffered writes on page
7649 * locks. This happens only if our DIO read covers more
7650 * than one extent map, if at this point has already
7651 * created an ordered extent for a previous extent map
7652 * and locked its range in the inode's io tree, and a
7653 * concurrent write against that previous extent map's
7654 * range and this range started (we unlock the ranges
7655 * in the io tree only when the bios complete and
7656 * buffered writes always lock pages before attempting
7657 * to lock range in the io tree).
7658 */
7659 if (writing ||
7660 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7661 btrfs_start_ordered_extent(inode, ordered, 1);
7662 else
7663 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007664 btrfs_put_ordered_extent(ordered);
7665 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007666 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007667 * We could trigger writeback for this range (and wait
7668 * for it to complete) and then invalidate the pages for
7669 * this range (through invalidate_inode_pages2_range()),
7670 * but that can lead us to a deadlock with a concurrent
7671 * call to readpages() (a buffered read or a defrag call
7672 * triggered a readahead) on a page lock due to an
7673 * ordered dio extent we created before but did not have
7674 * yet a corresponding bio submitted (whence it can not
7675 * complete), which makes readpages() wait for that
7676 * ordered extent to complete while holding a lock on
7677 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007678 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007679 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007680 }
7681
Filipe Mananaade77022016-02-18 14:28:55 +00007682 if (ret)
7683 break;
7684
Josef Bacikeb838e72012-07-31 16:28:48 -04007685 cond_resched();
7686 }
7687
7688 return ret;
7689}
7690
Liu Bo6f9994d2017-01-31 07:50:22 -08007691/* The callers of this must take lock_extent() */
7692static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7693 u64 orig_start, u64 block_start,
7694 u64 block_len, u64 orig_block_len,
7695 u64 ram_bytes, int compress_type,
7696 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007697{
7698 struct extent_map_tree *em_tree;
7699 struct extent_map *em;
7700 struct btrfs_root *root = BTRFS_I(inode)->root;
7701 int ret;
7702
Liu Bo6f9994d2017-01-31 07:50:22 -08007703 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7704 type == BTRFS_ORDERED_COMPRESSED ||
7705 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007706 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007707
Josef Bacik69ffb542012-09-11 15:40:07 -04007708 em_tree = &BTRFS_I(inode)->extent_tree;
7709 em = alloc_extent_map();
7710 if (!em)
7711 return ERR_PTR(-ENOMEM);
7712
7713 em->start = start;
7714 em->orig_start = orig_start;
7715 em->len = len;
7716 em->block_len = block_len;
7717 em->block_start = block_start;
7718 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007719 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007720 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007721 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007722 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007723 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007724 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007725 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007726 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7727 em->compress_type = compress_type;
7728 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007729
7730 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007731 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007732 em->start + em->len - 1, 0);
7733 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007734 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007735 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007736 /*
7737 * The caller has taken lock_extent(), who could race with us
7738 * to add em?
7739 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007740 } while (ret == -EEXIST);
7741
7742 if (ret) {
7743 free_extent_map(em);
7744 return ERR_PTR(ret);
7745 }
7746
Liu Bo6f9994d2017-01-31 07:50:22 -08007747 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007748 return em;
7749}
7750
Josef Bacik4b46fce2010-05-23 11:00:55 -04007751static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7752 struct buffer_head *bh_result, int create)
7753{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007754 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007755 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007756 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307757 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007758 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007759 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007760 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007761 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007762 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007763
Miao Xie172a5042013-02-21 02:48:22 -07007764 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007765 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007766 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007767 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007768
Josef Bacikc3298612012-08-03 16:49:19 -04007769 lockstart = start;
7770 lockend = start + len - 1;
7771
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007772 if (current->journal_info) {
7773 /*
7774 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007775 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007776 * confused.
7777 */
chandan50745b02015-08-28 21:10:13 +05307778 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007779 current->journal_info = NULL;
7780 }
7781
Josef Bacikeb838e72012-07-31 16:28:48 -04007782 /*
7783 * If this errors out it's because we couldn't invalidate pagecache for
7784 * this range and we need to fallback to buffered.
7785 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007786 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7787 create)) {
7788 ret = -ENOTBLK;
7789 goto err;
7790 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007791
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007792 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007793 if (IS_ERR(em)) {
7794 ret = PTR_ERR(em);
7795 goto unlock_err;
7796 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007797
7798 /*
7799 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7800 * io. INLINE is special, and we could probably kludge it in here, but
7801 * it's still buffered so for safety lets just fall back to the generic
7802 * buffered path.
7803 *
7804 * For COMPRESSED we _have_ to read the entire extent in so we can
7805 * decompress it, so there will be buffering required no matter what we
7806 * do, so go ahead and fallback to buffered.
7807 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007808 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007809 * to buffered IO. Don't blame me, this is the price we pay for using
7810 * the generic code.
7811 */
7812 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7813 em->block_start == EXTENT_MAP_INLINE) {
7814 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007815 ret = -ENOTBLK;
7816 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007817 }
7818
7819 /* Just a good old fashioned hole, return */
7820 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7821 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7822 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007823 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007824 }
7825
7826 /*
7827 * We don't allocate a new extent in the following cases
7828 *
7829 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7830 * existing extent.
7831 * 2) The extent is marked as PREALLOC. We're good to go here and can
7832 * just use the extent.
7833 *
7834 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007835 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007836 len = min(len, em->len - (start - em->start));
7837 lockstart = start + len;
7838 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007839 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007840
7841 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7842 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7843 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007844 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007845 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007846
7847 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7848 type = BTRFS_ORDERED_PREALLOC;
7849 else
7850 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007851 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007852 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007853
Josef Bacik00361582013-08-14 14:02:47 -04007854 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007855 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007856 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007857 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007858
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007859 em2 = btrfs_create_dio_extent(inode, start, len,
7860 orig_start, block_start,
7861 len, orig_block_len,
7862 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007863 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007864 if (type == BTRFS_ORDERED_PREALLOC) {
7865 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007866 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007867 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007868 if (em2 && IS_ERR(em2)) {
7869 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007870 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007871 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007872 /*
7873 * For inode marked NODATACOW or extent marked PREALLOC,
7874 * use the existing or preallocated extent, so does not
7875 * need to adjust btrfs_space_info's bytes_may_use.
7876 */
7877 btrfs_free_reserved_data_space_noquota(inode,
7878 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007879 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007880 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007881 }
Josef Bacik00361582013-08-14 14:02:47 -04007882
Chris Mason46bfbb52010-05-26 11:04:10 -04007883 /*
7884 * this will cow the extent, reset the len in case we changed
7885 * it above
7886 */
7887 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007888 free_extent_map(em);
7889 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007890 if (IS_ERR(em)) {
7891 ret = PTR_ERR(em);
7892 goto unlock_err;
7893 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007894 len = min(len, em->len - (start - em->start));
7895unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007896 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7897 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007898 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007899 bh_result->b_bdev = em->bdev;
7900 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007901 if (create) {
7902 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7903 set_buffer_new(bh_result);
7904
7905 /*
7906 * Need to update the i_size under the extent lock so buffered
7907 * readers will get the updated i_size when we unlock.
7908 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007909 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007910 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007911
chandan50745b02015-08-28 21:10:13 +05307912 WARN_ON(dio_data->reserve < len);
7913 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007914 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307915 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007916 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007917
Josef Bacikeb838e72012-07-31 16:28:48 -04007918 /*
7919 * In the case of write we need to clear and unlock the entire range,
7920 * in the case of read we need to unlock only the end area that we
7921 * aren't using if there is any left over space.
7922 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007923 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007924 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7925 lockend, unlock_bits, 1, 0,
7926 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007927 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007928 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007929 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007930
Josef Bacik4b46fce2010-05-23 11:00:55 -04007931 free_extent_map(em);
7932
7933 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007934
7935unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007936 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7937 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007938err:
chandan50745b02015-08-28 21:10:13 +05307939 if (dio_data)
7940 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007941 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007942}
7943
Omar Sandoval58efbc92017-08-22 23:45:59 -07007944static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7945 struct bio *bio,
7946 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007947{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007948 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007949 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007950
Mike Christie37226b22016-06-05 14:31:52 -05007951 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007952
7953 bio_get(bio);
7954
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007955 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007956 if (ret)
7957 goto err;
7958
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007959 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007960err:
7961 bio_put(bio);
7962 return ret;
7963}
7964
7965static int btrfs_check_dio_repairable(struct inode *inode,
7966 struct bio *failed_bio,
7967 struct io_failure_record *failrec,
7968 int failed_mirror)
7969{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007970 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007971 int num_copies;
7972
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007973 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007974 if (num_copies == 1) {
7975 /*
7976 * we only have a single copy of the data, so don't bother with
7977 * all the retry and error correction code that follows. no
7978 * matter what the error is, it is very likely to persist.
7979 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007980 btrfs_debug(fs_info,
7981 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7982 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007983 return 0;
7984 }
7985
7986 failrec->failed_mirror = failed_mirror;
7987 failrec->this_mirror++;
7988 if (failrec->this_mirror == failed_mirror)
7989 failrec->this_mirror++;
7990
7991 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007992 btrfs_debug(fs_info,
7993 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7994 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007995 return 0;
7996 }
7997
7998 return 1;
7999}
8000
Omar Sandoval58efbc92017-08-22 23:45:59 -07008001static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
8002 struct page *page, unsigned int pgoff,
8003 u64 start, u64 end, int failed_mirror,
8004 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08008005{
8006 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04008007 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8008 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008009 struct bio *bio;
8010 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02008011 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07008012 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08008013 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008014 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008015
Mike Christie37226b22016-06-05 14:31:52 -05008016 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008017
8018 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8019 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07008020 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08008021
8022 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8023 failed_mirror);
8024 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008025 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008026 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08008027 }
8028
Liu Bo17347ce2017-05-15 15:33:27 -07008029 segs = bio_segments(failed_bio);
8030 if (segs > 1 ||
8031 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008032 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008033
8034 isector = start - btrfs_io_bio(failed_bio)->logical;
8035 isector >>= inode->i_sb->s_blocksize_bits;
8036 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308037 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05008038 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008039
8040 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02008041 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08008042 read_mode, failrec->this_mirror, failrec->in_validation);
8043
Omar Sandoval58efbc92017-08-22 23:45:59 -07008044 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
8045 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04008046 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008047 bio_put(bio);
8048 }
8049
Omar Sandoval58efbc92017-08-22 23:45:59 -07008050 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08008051}
8052
8053struct btrfs_retry_complete {
8054 struct completion done;
8055 struct inode *inode;
8056 u64 start;
8057 int uptodate;
8058};
8059
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008060static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008061{
8062 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008063 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008064 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008065 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008066 int i;
8067
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008068 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008069 goto end;
8070
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308071 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008072 io_tree = &BTRFS_I(inode)->io_tree;
8073 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8074 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308075
Miao Xie8b110e32014-09-12 18:44:03 +08008076 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008077 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008078 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008079 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8080 io_tree, done->start, bvec->bv_page,
8081 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008082end:
8083 complete(&done->done);
8084 bio_put(bio);
8085}
8086
Omar Sandoval58efbc92017-08-22 23:45:59 -07008087static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8088 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008089{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308090 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008091 struct bio_vec bvec;
8092 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008093 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008094 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308095 unsigned int pgoff;
8096 u32 sectorsize;
8097 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008098 blk_status_t ret;
8099 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008100
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308101 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008102 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308103
Miao Xiec1dc0892014-09-12 18:43:56 +08008104 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008105 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008106 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008107
Liu Bo17347ce2017-05-15 15:33:27 -07008108 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8109 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8110 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308111
8112next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008113 done.uptodate = 0;
8114 done.start = start;
8115 init_completion(&done.done);
8116
Liu Bo17347ce2017-05-15 15:33:27 -07008117 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308118 pgoff, start, start + sectorsize - 1,
8119 io_bio->mirror_num,
8120 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008121 if (ret) {
8122 err = ret;
8123 goto next;
8124 }
Miao Xie8b110e32014-09-12 18:44:03 +08008125
David Sterba9c17f6c2017-07-19 19:26:45 +02008126 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008127
8128 if (!done.uptodate) {
8129 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308130 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008131 }
8132
Liu Bo629ebf42017-05-15 17:20:07 -07008133next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308134 start += sectorsize;
8135
Liu Bo97bf5a52017-04-07 13:11:10 -07008136 nr_sectors--;
8137 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308138 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008139 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308140 goto next_block_or_try_again;
8141 }
Miao Xie8b110e32014-09-12 18:44:03 +08008142 }
8143
Liu Bo629ebf42017-05-15 17:20:07 -07008144 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008145}
8146
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008147static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008148{
8149 struct btrfs_retry_complete *done = bio->bi_private;
8150 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008151 struct extent_io_tree *io_tree, *failure_tree;
8152 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008153 struct bio_vec *bvec;
8154 int uptodate;
8155 int ret;
8156 int i;
8157
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008158 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008159 goto end;
8160
8161 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308162
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308163 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008164 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308165
Josef Bacik7870d082017-05-05 11:57:15 -04008166 io_tree = &BTRFS_I(inode)->io_tree;
8167 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8168
David Sterbac09abff2017-07-13 18:10:07 +02008169 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008170 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008171 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8172 bvec->bv_offset, done->start,
8173 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008174 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008175 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8176 failure_tree, io_tree, done->start,
8177 bvec->bv_page,
8178 btrfs_ino(BTRFS_I(inode)),
8179 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008180 else
8181 uptodate = 0;
8182 }
8183
8184 done->uptodate = uptodate;
8185end:
8186 complete(&done->done);
8187 bio_put(bio);
8188}
8189
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008190static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8191 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008192{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308193 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008194 struct bio_vec bvec;
8195 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008196 struct btrfs_retry_complete done;
8197 u64 start;
8198 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308199 u32 sectorsize;
8200 int nr_sectors;
8201 unsigned int pgoff;
8202 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008203 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008204 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008205 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008206
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308207 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008208 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308209
Omar Sandoval58efbc92017-08-22 23:45:59 -07008210 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008211 start = io_bio->logical;
8212 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008213 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008214
Liu Bo17347ce2017-05-15 15:33:27 -07008215 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8216 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308217
Liu Bo17347ce2017-05-15 15:33:27 -07008218 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308219next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008220 if (uptodate) {
8221 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8222 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8223 bvec.bv_page, pgoff, start, sectorsize);
8224 if (likely(!ret))
8225 goto next;
8226 }
Miao Xie8b110e32014-09-12 18:44:03 +08008227try_again:
8228 done.uptodate = 0;
8229 done.start = start;
8230 init_completion(&done.done);
8231
Omar Sandoval58efbc92017-08-22 23:45:59 -07008232 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8233 pgoff, start, start + sectorsize - 1,
8234 io_bio->mirror_num, btrfs_retry_endio,
8235 &done);
8236 if (status) {
8237 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008238 goto next;
8239 }
8240
David Sterba9c17f6c2017-07-19 19:26:45 +02008241 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008242
8243 if (!done.uptodate) {
8244 /* We might have another mirror, so try again */
8245 goto try_again;
8246 }
8247next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308248 offset += sectorsize;
8249 start += sectorsize;
8250
8251 ASSERT(nr_sectors);
8252
Liu Bo97bf5a52017-04-07 13:11:10 -07008253 nr_sectors--;
8254 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308255 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008256 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308257 goto next_block;
8258 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008259 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008260
8261 return err;
8262}
8263
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008264static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8265 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008266{
8267 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8268
8269 if (skip_csum) {
8270 if (unlikely(err))
8271 return __btrfs_correct_data_nocsum(inode, io_bio);
8272 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008273 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008274 } else {
8275 return __btrfs_subio_endio_read(inode, io_bio, err);
8276 }
8277}
8278
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008279static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008280{
8281 struct btrfs_dio_private *dip = bio->bi_private;
8282 struct inode *inode = dip->inode;
8283 struct bio *dio_bio;
8284 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008285 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008286
Liu Bo99c4e3b92017-09-15 15:06:51 -06008287 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008288 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008289
Josef Bacik4b46fce2010-05-23 11:00:55 -04008290 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008291 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008292 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008293
Josef Bacik4b46fce2010-05-23 11:00:55 -04008294 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008295
Liu Bo99c4e3b92017-09-15 15:06:51 -06008296 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008297 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008298
8299 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008300 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008301 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008302}
8303
Qu Wenruo524272602017-03-08 10:25:52 +08008304static void __endio_write_update_ordered(struct inode *inode,
8305 const u64 offset, const u64 bytes,
8306 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008307{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008308 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008309 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008310 struct btrfs_workqueue *wq;
8311 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008312 u64 ordered_offset = offset;
8313 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008314 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008315 int ret;
8316
Qu Wenruo524272602017-03-08 10:25:52 +08008317 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8318 wq = fs_info->endio_freespace_worker;
8319 func = btrfs_freespace_write_helper;
8320 } else {
8321 wq = fs_info->endio_write_workers;
8322 func = btrfs_endio_write_helper;
8323 }
8324
Chris Mason163cf092010-11-28 19:56:33 -05008325again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008326 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008327 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8328 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008329 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008330 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008331 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008332 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008333
Qu Wenruo524272602017-03-08 10:25:52 +08008334 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8335 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008336out_test:
8337 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008338 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8339 * in the range, we can exit.
8340 */
8341 if (ordered_offset == last_offset)
8342 return;
8343 /*
Chris Mason163cf092010-11-28 19:56:33 -05008344 * our bio might span multiple ordered extents. If we haven't
8345 * completed the accounting for the whole dio, go back and try again
8346 */
Filipe Manana14543772015-11-24 16:23:54 +00008347 if (ordered_offset < offset + bytes) {
8348 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008349 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008350 goto again;
8351 }
Filipe Manana14543772015-11-24 16:23:54 +00008352}
8353
8354static void btrfs_endio_direct_write(struct bio *bio)
8355{
8356 struct btrfs_dio_private *dip = bio->bi_private;
8357 struct bio *dio_bio = dip->dio_bio;
8358
Qu Wenruo524272602017-03-08 10:25:52 +08008359 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008360 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008361
Josef Bacik4b46fce2010-05-23 11:00:55 -04008362 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008363
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008364 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008365 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008366 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008367}
8368
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008369static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008370 struct bio *bio, int mirror_num,
8371 unsigned long bio_flags, u64 offset)
8372{
Josef Bacikc6100a42017-05-05 11:57:13 -04008373 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008374 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008375 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008376 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008377 return 0;
8378}
8379
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008380static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008381{
8382 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008383 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008384
Miao Xie8b110e32014-09-12 18:44:03 +08008385 if (err)
8386 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008387 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008388 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8389 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008390 (unsigned long long)bio->bi_iter.bi_sector,
8391 bio->bi_iter.bi_size, err);
8392
8393 if (dip->subio_endio)
8394 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008395
8396 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008397 dip->errors = 1;
8398
8399 /*
8400 * before atomic variable goto zero, we must make sure
8401 * dip->errors is perceived to be set.
8402 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008403 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008404 }
8405
8406 /* if there are more bios still pending for this dio, just exit */
8407 if (!atomic_dec_and_test(&dip->pending_bios))
8408 goto out;
8409
Chris Mason9be33952013-05-17 18:30:14 -04008410 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008411 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008412 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008413 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008414 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008415 }
8416out:
8417 bio_put(bio);
8418}
8419
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008420static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008421 struct btrfs_dio_private *dip,
8422 struct bio *bio,
8423 u64 file_offset)
8424{
8425 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8426 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008427 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008428
8429 /*
8430 * We load all the csum data we need when we submit
8431 * the first bio to reduce the csum tree search and
8432 * contention.
8433 */
8434 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008435 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008436 file_offset);
8437 if (ret)
8438 return ret;
8439 }
8440
8441 if (bio == dip->orig_bio)
8442 return 0;
8443
8444 file_offset -= dip->logical_offset;
8445 file_offset >>= inode->i_sb->s_blocksize_bits;
8446 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8447
8448 return 0;
8449}
8450
Omar Sandoval58efbc92017-08-22 23:45:59 -07008451static inline blk_status_t
8452__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008453 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008454{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008455 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008456 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008457 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008458 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008459
Josef Bacikb812ce22012-11-16 13:56:32 -05008460 if (async_submit)
8461 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8462
Miao Xiee65e1532010-11-22 03:04:43 +00008463 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008464
8465 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008466 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008467 if (ret)
8468 goto err;
8469 }
Miao Xiee65e1532010-11-22 03:04:43 +00008470
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008471 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008472 goto map;
8473
8474 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008475 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8476 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008477 __btrfs_submit_bio_start_direct_io,
8478 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008479 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008480 } else if (write) {
8481 /*
8482 * If we aren't doing async submit, calculate the csum of the
8483 * bio now.
8484 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008485 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008486 if (ret)
8487 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008488 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008489 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008490 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008491 if (ret)
8492 goto err;
8493 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008494map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008495 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008496err:
8497 bio_put(bio);
8498 return ret;
8499}
8500
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008501static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008502{
8503 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008504 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008505 struct bio *bio;
8506 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008507 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008508 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008509 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008510 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008511 u64 submit_len;
8512 int clone_offset = 0;
8513 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308514 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008515 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008516
Kent Overstreet4f024f32013-10-11 15:44:27 -07008517 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008518 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008519 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8520 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008521 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008522 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008523
Liu Bo725130b2017-05-16 09:51:39 -07008524 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008525 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008526 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008527 goto submit;
8528 }
8529
David Woodhouse53b381b2013-01-29 18:40:14 -05008530 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008531 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008532 async_submit = 0;
8533 else
8534 async_submit = 1;
8535
Liu Bo725130b2017-05-16 09:51:39 -07008536 /* bio split */
8537 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008538 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008539 do {
Liu Bo725130b2017-05-16 09:51:39 -07008540 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008541
Liu Bo725130b2017-05-16 09:51:39 -07008542 /*
8543 * This will never fail as it's passing GPF_NOFS and
8544 * the allocation is backed by btrfs_bioset.
8545 */
Liu Boe4770942017-05-16 10:57:14 -07008546 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008547 clone_len);
8548 bio->bi_private = dip;
8549 bio->bi_end_io = btrfs_end_dio_bio;
8550 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008551
Liu Bo725130b2017-05-16 09:51:39 -07008552 ASSERT(submit_len >= clone_len);
8553 submit_len -= clone_len;
8554 if (submit_len == 0)
8555 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008556
Liu Bo725130b2017-05-16 09:51:39 -07008557 /*
8558 * Increase the count before we submit the bio so we know
8559 * the end IO handler won't happen before we increase the
8560 * count. Otherwise, the dip might get freed before we're
8561 * done setting it up.
8562 */
8563 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008564
Linus Torvalds66ba7722017-09-09 13:27:51 -07008565 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008566 async_submit);
8567 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008568 bio_put(bio);
8569 atomic_dec(&dip->pending_bios);
8570 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008571 }
Liu Bo725130b2017-05-16 09:51:39 -07008572
8573 clone_offset += clone_len;
8574 start_sector += clone_len >> 9;
8575 file_offset += clone_len;
8576
8577 map_length = submit_len;
8578 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8579 start_sector << 9, &map_length, NULL, 0);
8580 if (ret)
8581 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008582 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008583
Josef Bacik02f57c72011-04-06 14:25:44 -04008584submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008585 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008586 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008587 return 0;
8588
8589 bio_put(bio);
8590out_err:
8591 dip->errors = 1;
8592 /*
8593 * before atomic variable goto zero, we must
8594 * make sure dip->errors is perceived to be set.
8595 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008596 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008597 if (atomic_dec_and_test(&dip->pending_bios))
8598 bio_io_error(dip->orig_bio);
8599
8600 /* bio_end_io() will handle error, so we needn't return it */
8601 return 0;
8602}
8603
Mike Christie8a4c1e42016-06-05 14:31:50 -05008604static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8605 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008606{
Filipe Manana61de7182015-07-01 12:13:10 +01008607 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008608 struct bio *bio = NULL;
8609 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008610 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008611 int ret = 0;
8612
David Sterba8b6c1d52017-06-02 17:48:13 +02008613 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008614
Miao Xiec1dc0892014-09-12 18:43:56 +08008615 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008616 if (!dip) {
8617 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008618 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008619 }
8620
8621 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008622 dip->inode = inode;
8623 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008624 dip->bytes = dio_bio->bi_iter.bi_size;
8625 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008626 bio->bi_private = dip;
8627 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008628 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008629 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008630 io_bio = btrfs_io_bio(bio);
8631 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008632
Miao Xiec1dc0892014-09-12 18:43:56 +08008633 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008634 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008635 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008636 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008637 dip->subio_endio = btrfs_subio_endio_read;
8638 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008639
Filipe Mananaf28a4922015-12-08 19:23:20 +00008640 /*
8641 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8642 * even if we fail to submit a bio, because in such case we do the
8643 * corresponding error handling below and it must not be done a second
8644 * time by btrfs_direct_IO().
8645 */
8646 if (write) {
8647 struct btrfs_dio_data *dio_data = current->journal_info;
8648
8649 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8650 dip->bytes;
8651 dio_data->unsubmitted_oe_range_start =
8652 dio_data->unsubmitted_oe_range_end;
8653 }
8654
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008655 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008656 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008657 return;
Chris Mason9be33952013-05-17 18:30:14 -04008658
Liu Bo3892ac92017-04-17 15:00:28 -07008659 if (io_bio->end_io)
8660 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008661
Josef Bacik4b46fce2010-05-23 11:00:55 -04008662free_ordered:
8663 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008664 * If we arrived here it means either we failed to submit the dip
8665 * or we either failed to clone the dio_bio or failed to allocate the
8666 * dip. If we cloned the dio_bio and allocated the dip, we can just
8667 * call bio_endio against our io_bio so that we get proper resource
8668 * cleanup if we fail to submit the dip, otherwise, we must do the
8669 * same as btrfs_endio_direct_[write|read] because we can't call these
8670 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008671 */
Liu Bo3892ac92017-04-17 15:00:28 -07008672 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008673 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008674 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008675 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008676 * and all the cleanup and final put for dio_bio (through
8677 * dio_end_io()).
8678 */
8679 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008680 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008681 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008682 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008683 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008684 file_offset,
8685 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008686 false);
Filipe Manana14543772015-11-24 16:23:54 +00008687 else
Filipe Manana61de7182015-07-01 12:13:10 +01008688 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8689 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008690
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008691 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008692 /*
8693 * Releases and cleans up our dio_bio, no need to bio_put()
8694 * nor bio_endio()/bio_io_error() against dio_bio.
8695 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008696 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008697 }
Liu Bo3892ac92017-04-17 15:00:28 -07008698 if (bio)
8699 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008700 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008701}
8702
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008703static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008704 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008705{
8706 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008707 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008708 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008709 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008710
8711 if (offset & blocksize_mask)
8712 goto out;
8713
Al Viro28060d52014-03-22 05:15:17 -04008714 if (iov_iter_alignment(iter) & blocksize_mask)
8715 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008716
Al Viro28060d52014-03-22 05:15:17 -04008717 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008718 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008719 return 0;
8720 /*
8721 * Check to make sure we don't have duplicate iov_base's in this
8722 * iovec, if so return EINVAL, otherwise we'll get csum errors
8723 * when reading back.
8724 */
8725 for (seg = 0; seg < iter->nr_segs; seg++) {
8726 for (i = seg + 1; i < iter->nr_segs; i++) {
8727 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008728 goto out;
8729 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008730 }
8731 retval = 0;
8732out:
8733 return retval;
8734}
Josef Bacikeb838e72012-07-31 16:28:48 -04008735
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008736static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008737{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008738 struct file *file = iocb->ki_filp;
8739 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008740 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308741 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008742 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008743 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008744 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008745 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008746 bool wakeup = true;
8747 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008748 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008749
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008750 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008751 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008752
Jens Axboefe0f07d2015-04-15 17:05:48 -06008753 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008754
Josef Bacik0e267c42013-07-02 10:38:02 -04008755 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008756 * The generic stuff only does filemap_write_and_wait_range, which
8757 * isn't enough if we've written compressed pages to this area, so
8758 * we need to flush the dirty pages again to make absolutely sure
8759 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008760 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008761 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008762 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8763 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008764 filemap_fdatawrite_range(inode->i_mapping, offset,
8765 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008766
Omar Sandoval6f673762015-03-16 04:33:52 -07008767 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008768 /*
8769 * If the write DIO is beyond the EOF, we need update
8770 * the isize, but it is protected by i_mutex. So we can
8771 * not unlock the i_mutex at this case.
8772 */
8773 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008774 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008775 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008776 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008777 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8778 ret = -EAGAIN;
8779 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008780 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008781 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8782 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008783 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008784 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008785
8786 /*
8787 * We need to know how many extents we reserved so that we can
8788 * do the accounting properly if we go over the number we
8789 * originally calculated. Abuse current->journal_info for this.
8790 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008791 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008792 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008793 dio_data.unsubmitted_oe_range_start = (u64)offset;
8794 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308795 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308796 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008797 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8798 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008799 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008800 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8801 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008802 }
8803
Omar Sandoval17f8c842015-03-16 04:33:50 -07008804 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008805 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008806 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008807 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008808 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308809 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008810 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008811 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308812 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008813 btrfs_delalloc_release_space(inode, data_reserved,
8814 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008815 /*
8816 * On error we might have left some ordered extents
8817 * without submitting corresponding bios for them, so
8818 * cleanup them up to avoid other tasks getting them
8819 * and waiting for them to complete forever.
8820 */
8821 if (dio_data.unsubmitted_oe_range_start <
8822 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008823 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008824 dio_data.unsubmitted_oe_range_start,
8825 dio_data.unsubmitted_oe_range_end -
8826 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008827 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008828 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008829 btrfs_delalloc_release_space(inode, data_reserved,
8830 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008831 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008832 }
Miao Xie38851cc2013-02-08 07:04:11 +00008833out:
Miao Xie2e60a512013-02-08 07:01:08 +00008834 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008835 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008836 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008837 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008838
Qu Wenruo364ecf32017-02-27 15:10:38 +08008839 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008840 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008841}
8842
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008843#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8844
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008845static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8846 __u64 start, __u64 len)
8847{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008848 int ret;
8849
8850 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8851 if (ret)
8852 return ret;
8853
Chris Masonec29ed52011-02-23 16:23:20 -05008854 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008855}
8856
Chris Mason9ebefb182007-06-15 13:50:00 -04008857int btrfs_readpage(struct file *file, struct page *page)
8858{
Chris Masond1310b22008-01-24 16:13:08 -05008859 struct extent_io_tree *tree;
8860 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008861 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008862}
Chris Mason1832a6d2007-12-21 16:27:21 -05008863
Chris Mason39279cc2007-06-12 06:35:45 -04008864static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8865{
Chris Masond1310b22008-01-24 16:13:08 -05008866 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008867 struct inode *inode = page->mapping->host;
8868 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008869
8870 if (current->flags & PF_MEMALLOC) {
8871 redirty_page_for_writepage(wbc, page);
8872 unlock_page(page);
8873 return 0;
8874 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008875
8876 /*
8877 * If we are under memory pressure we will call this directly from the
8878 * VM, we need to make sure we have the inode referenced for the ordered
8879 * extent. If not just return like we didn't do anything.
8880 */
8881 if (!igrab(inode)) {
8882 redirty_page_for_writepage(wbc, page);
8883 return AOP_WRITEPAGE_ACTIVATE;
8884 }
Chris Masond1310b22008-01-24 16:13:08 -05008885 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008886 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8887 btrfs_add_delayed_iput(inode);
8888 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008889}
Chris Mason39279cc2007-06-12 06:35:45 -04008890
Eric Sandeen48a3b632013-04-25 20:41:01 +00008891static int btrfs_writepages(struct address_space *mapping,
8892 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008893{
Chris Masond1310b22008-01-24 16:13:08 -05008894 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008895
Chris Masond1310b22008-01-24 16:13:08 -05008896 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008897 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8898}
8899
Chris Mason3ab2fb52007-11-08 10:59:22 -05008900static int
8901btrfs_readpages(struct file *file, struct address_space *mapping,
8902 struct list_head *pages, unsigned nr_pages)
8903{
Chris Masond1310b22008-01-24 16:13:08 -05008904 struct extent_io_tree *tree;
8905 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008906 return extent_readpages(tree, mapping, pages, nr_pages,
8907 btrfs_get_extent);
8908}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008909static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008910{
Chris Masond1310b22008-01-24 16:13:08 -05008911 struct extent_io_tree *tree;
8912 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008913 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008914
Chris Masond1310b22008-01-24 16:13:08 -05008915 tree = &BTRFS_I(page->mapping->host)->io_tree;
8916 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008917 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008918 if (ret == 1) {
8919 ClearPagePrivate(page);
8920 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008921 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008922 }
8923 return ret;
8924}
Chris Mason39279cc2007-06-12 06:35:45 -04008925
Chris Masone6dcd2d2008-07-17 12:53:50 -04008926static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8927{
Chris Mason98509cf2008-09-11 15:51:43 -04008928 if (PageWriteback(page) || PageDirty(page))
8929 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008930 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008931}
8932
Lukas Czernerd47992f2013-05-21 23:17:23 -04008933static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8934 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008935{
Josef Bacik5fd02042012-05-02 14:00:54 -04008936 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008937 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008938 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008939 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008940 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008941 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308942 u64 start;
8943 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008944 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008945
Chris Mason8b62b722009-09-02 16:53:46 -04008946 /*
8947 * we have the page locked, so new writeback can't start,
8948 * and the dirty bit won't be cleared while we are here.
8949 *
8950 * Wait for IO on this page so that we can safely clear
8951 * the PagePrivate2 bit and do ordered accounting
8952 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008953 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008954
Josef Bacik5fd02042012-05-02 14:00:54 -04008955 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008956 if (offset) {
8957 btrfs_releasepage(page, GFP_NOFS);
8958 return;
8959 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008960
8961 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008962 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308963again:
8964 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008965 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308966 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008967 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308968 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008969 /*
8970 * IO on this page will never be started, so we need
8971 * to account for any ordered extents now
8972 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008973 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308974 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008975 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008976 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008977 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8978 EXTENT_DEFRAG, 1, 0, &cached_state,
8979 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008980 /*
8981 * whoever cleared the private bit is responsible
8982 * for the finish_ordered_io
8983 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008984 if (TestClearPagePrivate2(page)) {
8985 struct btrfs_ordered_inode_tree *tree;
8986 u64 new_len;
8987
8988 tree = &BTRFS_I(inode)->ordered_tree;
8989
8990 spin_lock_irq(&tree->lock);
8991 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308992 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008993 if (new_len < ordered->truncated_len)
8994 ordered->truncated_len = new_len;
8995 spin_unlock_irq(&tree->lock);
8996
8997 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308998 start,
8999 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04009000 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04009001 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009002 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009003 if (!inode_evicting) {
9004 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309005 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009006 &cached_state);
9007 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309008
9009 start = end + 1;
9010 if (start < page_end)
9011 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009012 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009013
Qu Wenruob9d0b382015-09-29 10:35:16 +08009014 /*
9015 * Qgroup reserved space handler
9016 * Page here will be either
9017 * 1) Already written to disk
9018 * In this case, its reserved space is released from data rsv map
9019 * and will be freed by delayed_ref handler finally.
9020 * So even we call qgroup_free_data(), it won't decrease reserved
9021 * space.
9022 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009023 * This means the reserved space should be freed here. However,
9024 * if a truncate invalidates the page (by clearing PageDirty)
9025 * and the page is accounted for while allocating extent
9026 * in btrfs_check_data_free_space() we let delayed_ref to
9027 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009028 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009029 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08009030 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009031 if (!inode_evicting) {
9032 clear_extent_bit(tree, page_start, page_end,
9033 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009034 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9035 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009036 &cached_state, GFP_NOFS);
9037
9038 __btrfs_releasepage(page, GFP_NOFS);
9039 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009040
Chris Mason4a096752008-07-21 10:29:44 -04009041 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009042 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009043 ClearPagePrivate(page);
9044 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009045 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009046 }
Chris Mason39279cc2007-06-12 06:35:45 -04009047}
9048
Chris Mason9ebefb182007-06-15 13:50:00 -04009049/*
9050 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9051 * called from a page fault handler when a page is first dirtied. Hence we must
9052 * be careful to check for EOF conditions here. We set the page up correctly
9053 * for a written page which means we get ENOSPC checking when writing into
9054 * holes and correct delalloc and unwritten extent mapping on filesystems that
9055 * support these features.
9056 *
9057 * We are not allowed to take the i_mutex here so we have to play games to
9058 * protect against truncate races as the page could now be beyond EOF. Because
9059 * vmtruncate() writes the inode size before removing pages, once we have the
9060 * page lock we can determine safely if the page is beyond EOF. If it is not
9061 * beyond EOF, then the page is guaranteed safe against truncation until we
9062 * unlock the page.
9063 */
Dave Jiang11bac802017-02-24 14:56:41 -08009064int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009065{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009066 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009067 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009068 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009069 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9070 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009071 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009072 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009073 char *kaddr;
9074 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009075 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009076 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009077 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309078 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009079 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009080 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309081 u64 end;
9082
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009083 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009084
Jan Karab2b5ef52012-06-12 16:20:45 +02009085 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009086 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009087 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309088 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009089
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309090 /*
9091 * Reserving delalloc space after obtaining the page lock can lead to
9092 * deadlock. For example, if a dirty page is locked by this function
9093 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9094 * dirty page write out, then the btrfs_writepage() function could
9095 * end up waiting indefinitely to get a lock on the page currently
9096 * being processed by btrfs_page_mkwrite() function.
9097 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009098 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309099 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009100 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009101 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009102 reserved = 1;
9103 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009104 if (ret) {
9105 if (ret == -ENOMEM)
9106 ret = VM_FAULT_OOM;
9107 else /* -ENOSPC, -EIO, etc */
9108 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009109 if (reserved)
9110 goto out;
9111 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009112 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009113
Nick Piggin56a76f82009-03-31 15:23:23 -07009114 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009115again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009116 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009117 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009118
Chris Mason9ebefb182007-06-15 13:50:00 -04009119 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009120 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009121 /* page got truncated out from underneath us */
9122 goto out_unlock;
9123 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009124 wait_on_page_writeback(page);
9125
David Sterbaff13db42015-12-03 14:30:40 +01009126 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009127 set_page_extent_mapped(page);
9128
Chris Masoneb84ae02008-07-17 13:53:27 -04009129 /*
9130 * we can't set the delalloc bits if there are pending ordered
9131 * extents. Drop our locks and wait for them to finish
9132 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009133 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9134 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009135 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009136 unlock_extent_cached(io_tree, page_start, page_end,
9137 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009138 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009139 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009140 btrfs_put_ordered_extent(ordered);
9141 goto again;
9142 }
9143
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009144 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009145 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009146 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009147 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309148 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009149 btrfs_delalloc_release_space(inode, data_reserved,
9150 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309151 }
9152 }
9153
Josef Bacikfbf19082009-10-01 17:10:23 -04009154 /*
Liu Bo54160342016-12-13 12:15:19 -08009155 * page_mkwrite gets called when the page is firstly dirtied after it's
9156 * faulted in, but write(2) could also dirty a page and set delalloc
9157 * bits, thus in this case for space account reason, we still need to
9158 * clear any delalloc bits within this page range since we have to
9159 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009160 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309161 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009162 EXTENT_DIRTY | EXTENT_DELALLOC |
9163 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009164 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009165
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309166 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009167 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009168 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009169 unlock_extent_cached(io_tree, page_start, page_end,
9170 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009171 ret = VM_FAULT_SIGBUS;
9172 goto out_unlock;
9173 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009174 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009175
9176 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009177 if (page_start + PAGE_SIZE > size)
9178 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009179 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009180 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009181
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009182 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009183 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009184 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009185 flush_dcache_page(page);
9186 kunmap(page);
9187 }
Chris Mason247e7432008-07-17 12:53:51 -04009188 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009189 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009190 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009191
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009192 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009193 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009194 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009195
Josef Bacik2ac55d42010-02-03 19:33:23 +00009196 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009197
9198out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009199 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009200 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009201 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009202 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009203 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009204 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009205 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009206out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009207 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009208 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9209 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009210out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009211 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009212 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009213 return ret;
9214}
9215
Josef Bacika41ad392011-01-31 15:30:16 -05009216static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009217{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009218 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009219 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009220 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009221 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009222 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009223 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009224 u64 mask = fs_info->sectorsize - 1;
9225 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009226
Josef Bacik0ef8b722013-10-25 16:13:35 -04009227 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9228 (u64)-1);
9229 if (ret)
9230 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009231
Josef Bacikfcb80c22011-05-03 10:40:22 -04009232 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009233 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009234 * 3 things going on here
9235 *
9236 * 1) We need to reserve space for our orphan item and the space to
9237 * delete our orphan item. Lord knows we don't want to have a dangling
9238 * orphan item because we didn't reserve space to remove it.
9239 *
9240 * 2) We need to reserve space to update our inode.
9241 *
9242 * 3) We need to have something to cache all the space that is going to
9243 * be free'd up by the truncate operation, but also have some slack
9244 * space reserved in case it uses space during the truncate (thank you
9245 * very much snapshotting).
9246 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009247 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009248 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009249 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009250 * doesn't end up using space reserved for updating the inode or
9251 * removing the orphan item. We also need to be able to stop the
9252 * transaction and start a new one, which means we need to be able to
9253 * update the inode several times, and we have no idea of knowing how
9254 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009255 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009256 * Then there is the orphan item, which does indeed need to be held on
9257 * to for the whole operation, and we need nobody to touch this reserved
9258 * space except the orphan code.
9259 *
9260 * So that leaves us with
9261 *
9262 * 1) root->orphan_block_rsv - for the orphan deletion.
9263 * 2) rsv - for the truncate reservation, which we will steal from the
9264 * transaction reservation.
9265 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9266 * updating the inode.
9267 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009268 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009269 if (!rsv)
9270 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009271 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009272 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009273
Josef Bacik907cbce2011-08-08 13:46:15 -04009274 /*
Josef Bacik07127182011-08-19 10:29:59 -04009275 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009276 * 1 for updating the inode.
9277 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009278 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009279 if (IS_ERR(trans)) {
9280 err = PTR_ERR(trans);
9281 goto out;
9282 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009283
Josef Bacik907cbce2011-08-08 13:46:15 -04009284 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009285 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009286 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009287 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009288
Chris Mason5a3f23d2009-03-31 13:27:11 -04009289 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009290 * So if we truncate and then write and fsync we normally would just
9291 * write the extents that changed, which is a problem if we need to
9292 * first truncate that entire inode. So set this flag so we write out
9293 * all of the extents in the inode to the sync log so we're completely
9294 * safe.
9295 */
9296 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009297 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009298
Yan, Zheng80825102009-11-12 09:35:36 +00009299 while (1) {
9300 ret = btrfs_truncate_inode_items(trans, root, inode,
9301 inode->i_size,
9302 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009303 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009304 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009305 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009306 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009307 }
Chris Mason39279cc2007-06-12 06:35:45 -04009308
Yan, Zheng80825102009-11-12 09:35:36 +00009309 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009310 if (ret) {
9311 err = ret;
9312 break;
9313 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009314
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009315 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009316 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009317
9318 trans = btrfs_start_transaction(root, 2);
9319 if (IS_ERR(trans)) {
9320 ret = err = PTR_ERR(trans);
9321 trans = NULL;
9322 break;
9323 }
9324
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009325 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009326 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009327 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009328 BUG_ON(ret); /* shouldn't happen */
9329 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009330 }
9331
Josef Bacikddfae632017-10-19 14:16:02 -04009332 /*
9333 * We can't call btrfs_truncate_block inside a trans handle as we could
9334 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9335 * we've truncated everything except the last little bit, and can do
9336 * btrfs_truncate_block and then update the disk_i_size.
9337 */
9338 if (ret == NEED_TRUNCATE_BLOCK) {
9339 btrfs_end_transaction(trans);
9340 btrfs_btree_balance_dirty(fs_info);
9341
9342 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9343 if (ret)
9344 goto out;
9345 trans = btrfs_start_transaction(root, 1);
9346 if (IS_ERR(trans)) {
9347 ret = PTR_ERR(trans);
9348 goto out;
9349 }
9350 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9351 }
9352
Yan, Zheng80825102009-11-12 09:35:36 +00009353 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009354 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009355 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009356 if (ret)
9357 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009358 }
9359
Chris Mason917c16b2011-11-08 14:49:59 -05009360 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009361 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009362 ret = btrfs_update_inode(trans, root, inode);
9363 if (ret && !err)
9364 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009365
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009366 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009367 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009368 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009369out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009370 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009371
Josef Bacik3893e332011-01-31 16:03:11 -05009372 if (ret && !err)
9373 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009374
Josef Bacik3893e332011-01-31 16:03:11 -05009375 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009376}
9377
Sven Wegener3b963622008-06-09 21:57:42 -04009378/*
Chris Masond352ac62008-09-29 15:18:18 -04009379 * create a new subvolume directory/inode (helper for the ioctl).
9380 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009381int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009382 struct btrfs_root *new_root,
9383 struct btrfs_root *parent_root,
9384 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009385{
Chris Mason39279cc2007-06-12 06:35:45 -04009386 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009387 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009388 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009389
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009390 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9391 new_dirid, new_dirid,
9392 S_IFDIR | (~current_umask() & S_IRWXUGO),
9393 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009394 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009395 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009396 inode->i_op = &btrfs_dir_inode_operations;
9397 inode->i_fop = &btrfs_dir_file_operations;
9398
Miklos Szeredibfe86842011-10-28 14:13:29 +02009399 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009400 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009401 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009402
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009403 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9404 if (err)
9405 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009406 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009407 new_root->root_key.objectid, err);
9408
Yan, Zheng76dda932009-09-21 16:00:26 -04009409 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009410
Yan, Zheng76dda932009-09-21 16:00:26 -04009411 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009412 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009413}
9414
Chris Mason39279cc2007-06-12 06:35:45 -04009415struct inode *btrfs_alloc_inode(struct super_block *sb)
9416{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009417 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009418 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009419 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009420
9421 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9422 if (!ei)
9423 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009424
9425 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009426 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009427 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009428 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009429 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009430 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009431 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009432 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009433 ei->disk_i_size = 0;
9434 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009435 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009436 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009437 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009438 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009439 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009440 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009441
Josef Bacik9e0baf62011-07-15 15:16:44 +00009442 spin_lock_init(&ei->lock);
9443 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009444 if (sb->s_magic != BTRFS_TEST_MAGIC)
9445 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9446 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009447 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009448 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009449 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009450
Miao Xie16cdcec2011-04-22 18:12:22 +08009451 ei->delayed_node = NULL;
9452
chandan r9cc97d62012-07-04 12:48:07 +05309453 ei->i_otime.tv_sec = 0;
9454 ei->i_otime.tv_nsec = 0;
9455
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009456 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009457 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009458 extent_io_tree_init(&ei->io_tree, inode);
9459 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009460 ei->io_tree.track_uptodate = 1;
9461 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009462 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009463 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009464 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009465 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009466 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009467 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009468 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009469 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009470
9471 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009472}
9473
Josef Bacikaaedb552013-10-11 14:44:09 -04009474#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9475void btrfs_test_destroy_inode(struct inode *inode)
9476{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009477 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009478 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9479}
9480#endif
9481
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009482static void btrfs_i_callback(struct rcu_head *head)
9483{
9484 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009485 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9486}
9487
Chris Mason39279cc2007-06-12 06:35:45 -04009488void btrfs_destroy_inode(struct inode *inode)
9489{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009490 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009491 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009492 struct btrfs_root *root = BTRFS_I(inode)->root;
9493
Al Virob3d9b7a2012-06-09 13:51:19 -04009494 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009495 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009496 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9497 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009498 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009499 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009500 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009501 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009502 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009503
Chris Mason5a3f23d2009-03-31 13:27:11 -04009504 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009505 * This can happen where we create an inode, but somebody else also
9506 * created the same inode and we need to destroy the one we already
9507 * created.
9508 */
9509 if (!root)
9510 goto free;
9511
Josef Bacik8a35d952012-05-23 14:26:42 -04009512 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9513 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009514 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009515 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009516 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009517 }
Josef Bacik7b128762008-07-24 12:17:14 -04009518
Chris Masond3977122009-01-05 21:25:51 -05009519 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009520 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9521 if (!ordered)
9522 break;
9523 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009524 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009525 "found ordered extent %llu %llu on inode cleanup",
9526 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009527 btrfs_remove_ordered_extent(inode, ordered);
9528 btrfs_put_ordered_extent(ordered);
9529 btrfs_put_ordered_extent(ordered);
9530 }
9531 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009532 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009533 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009534 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009535free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009536 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009537}
9538
Al Viro45321ac2010-06-07 13:43:19 -04009539int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009540{
9541 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009542
Naohiro Aota6379ef92013-06-06 09:56:34 +00009543 if (root == NULL)
9544 return 1;
9545
Liu Bofa6ac872013-02-20 14:10:23 +00009546 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009547 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009548 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009549 else
Al Viro45321ac2010-06-07 13:43:19 -04009550 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009551}
9552
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009553static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009554{
9555 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9556
9557 inode_init_once(&ei->vfs_inode);
9558}
9559
9560void btrfs_destroy_cachep(void)
9561{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009562 /*
9563 * Make sure all delayed rcu free inodes are flushed before we
9564 * destroy cache.
9565 */
9566 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009567 kmem_cache_destroy(btrfs_inode_cachep);
9568 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009569 kmem_cache_destroy(btrfs_path_cachep);
9570 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009571}
9572
9573int btrfs_init_cachep(void)
9574{
David Sterba837e1972012-09-07 03:00:48 -06009575 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009576 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009577 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9578 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009579 if (!btrfs_inode_cachep)
9580 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009581
David Sterba837e1972012-09-07 03:00:48 -06009582 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009583 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009584 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009585 if (!btrfs_trans_handle_cachep)
9586 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009587
David Sterba837e1972012-09-07 03:00:48 -06009588 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009589 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009590 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009591 if (!btrfs_path_cachep)
9592 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009593
David Sterba837e1972012-09-07 03:00:48 -06009594 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009595 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009596 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009597 if (!btrfs_free_space_cachep)
9598 goto fail;
9599
Chris Mason39279cc2007-06-12 06:35:45 -04009600 return 0;
9601fail:
9602 btrfs_destroy_cachep();
9603 return -ENOMEM;
9604}
9605
David Howellsa528d352017-01-31 16:46:22 +00009606static int btrfs_getattr(const struct path *path, struct kstat *stat,
9607 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009608{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009609 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009610 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009611 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009612 u32 bi_flags = BTRFS_I(inode)->flags;
9613
9614 stat->result_mask |= STATX_BTIME;
9615 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9616 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9617 if (bi_flags & BTRFS_INODE_APPEND)
9618 stat->attributes |= STATX_ATTR_APPEND;
9619 if (bi_flags & BTRFS_INODE_COMPRESS)
9620 stat->attributes |= STATX_ATTR_COMPRESSED;
9621 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9622 stat->attributes |= STATX_ATTR_IMMUTABLE;
9623 if (bi_flags & BTRFS_INODE_NODUMP)
9624 stat->attributes |= STATX_ATTR_NODUMP;
9625
9626 stat->attributes_mask |= (STATX_ATTR_APPEND |
9627 STATX_ATTR_COMPRESSED |
9628 STATX_ATTR_IMMUTABLE |
9629 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009630
Chris Mason39279cc2007-06-12 06:35:45 -04009631 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009632 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009633
9634 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009635 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009636 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009637 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009638 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009639 return 0;
9640}
9641
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009642static int btrfs_rename_exchange(struct inode *old_dir,
9643 struct dentry *old_dentry,
9644 struct inode *new_dir,
9645 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009646{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009647 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009648 struct btrfs_trans_handle *trans;
9649 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009650 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009651 struct inode *new_inode = new_dentry->d_inode;
9652 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009653 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009654 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009655 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9656 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009657 u64 old_idx = 0;
9658 u64 new_idx = 0;
9659 u64 root_objectid;
9660 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009661 bool root_log_pinned = false;
9662 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009663
9664 /* we only allow rename subvolume link between subvolumes */
9665 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9666 return -EXDEV;
9667
9668 /* close the race window with snapshot create/destroy ioctl */
9669 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009670 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009671 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009672 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009673
9674 /*
9675 * We want to reserve the absolute worst case amount of items. So if
9676 * both inodes are subvols and we need to unlink them then that would
9677 * require 4 item modifications, but if they are both normal inodes it
9678 * would require 5 item modifications, so we'll assume their normal
9679 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9680 * should cover the worst case number of items we'll modify.
9681 */
9682 trans = btrfs_start_transaction(root, 12);
9683 if (IS_ERR(trans)) {
9684 ret = PTR_ERR(trans);
9685 goto out_notrans;
9686 }
9687
9688 /*
9689 * We need to find a free sequence number both in the source and
9690 * in the destination directory for the exchange.
9691 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009692 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009693 if (ret)
9694 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009695 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009696 if (ret)
9697 goto out_fail;
9698
9699 BTRFS_I(old_inode)->dir_index = 0ULL;
9700 BTRFS_I(new_inode)->dir_index = 0ULL;
9701
9702 /* Reference for the source. */
9703 if (old_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(root);
9708 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009709 ret = btrfs_insert_inode_ref(trans, dest,
9710 new_dentry->d_name.name,
9711 new_dentry->d_name.len,
9712 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009713 btrfs_ino(BTRFS_I(new_dir)),
9714 old_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 /* And now for the dest. */
9720 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9721 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009722 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009723 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009724 btrfs_pin_log_trans(dest);
9725 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009726 ret = btrfs_insert_inode_ref(trans, root,
9727 old_dentry->d_name.name,
9728 old_dentry->d_name.len,
9729 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009730 btrfs_ino(BTRFS_I(old_dir)),
9731 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009732 if (ret)
9733 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009734 }
9735
9736 /* Update inode version and ctime/mtime. */
9737 inode_inc_iversion(old_dir);
9738 inode_inc_iversion(new_dir);
9739 inode_inc_iversion(old_inode);
9740 inode_inc_iversion(new_inode);
9741 old_dir->i_ctime = old_dir->i_mtime = ctime;
9742 new_dir->i_ctime = new_dir->i_mtime = ctime;
9743 old_inode->i_ctime = ctime;
9744 new_inode->i_ctime = ctime;
9745
9746 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009747 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9748 BTRFS_I(old_inode), 1);
9749 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9750 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009751 }
9752
9753 /* src is a subvolume */
9754 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9755 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9756 ret = btrfs_unlink_subvol(trans, root, old_dir,
9757 root_objectid,
9758 old_dentry->d_name.name,
9759 old_dentry->d_name.len);
9760 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009761 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9762 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009763 old_dentry->d_name.name,
9764 old_dentry->d_name.len);
9765 if (!ret)
9766 ret = btrfs_update_inode(trans, root, old_inode);
9767 }
9768 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009769 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009770 goto out_fail;
9771 }
9772
9773 /* dest is a subvolume */
9774 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9775 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9776 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9777 root_objectid,
9778 new_dentry->d_name.name,
9779 new_dentry->d_name.len);
9780 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009781 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9782 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009783 new_dentry->d_name.name,
9784 new_dentry->d_name.len);
9785 if (!ret)
9786 ret = btrfs_update_inode(trans, dest, new_inode);
9787 }
9788 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009789 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009790 goto out_fail;
9791 }
9792
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009793 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009794 new_dentry->d_name.name,
9795 new_dentry->d_name.len, 0, old_idx);
9796 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009797 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009798 goto out_fail;
9799 }
9800
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009801 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009802 old_dentry->d_name.name,
9803 old_dentry->d_name.len, 0, new_idx);
9804 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009805 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009806 goto out_fail;
9807 }
9808
9809 if (old_inode->i_nlink == 1)
9810 BTRFS_I(old_inode)->dir_index = old_idx;
9811 if (new_inode->i_nlink == 1)
9812 BTRFS_I(new_inode)->dir_index = new_idx;
9813
Filipe Manana86e8aa02016-05-05 02:02:27 +01009814 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009815 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009816 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9817 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009818 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009819 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009820 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009821 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009822 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009823 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9824 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009825 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009826 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009827 }
9828out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009829 /*
9830 * If we have pinned a log and an error happened, we unpin tasks
9831 * trying to sync the log and force them to fallback to a transaction
9832 * commit if the log currently contains any of the inodes involved in
9833 * this rename operation (to ensure we do not persist a log with an
9834 * inconsistent state for any of these inodes or leading to any
9835 * inconsistencies when replayed). If the transaction was aborted, the
9836 * abortion reason is propagated to userspace when attempting to commit
9837 * the transaction. If the log does not contain any of these inodes, we
9838 * allow the tasks to sync it.
9839 */
9840 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009841 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9842 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9843 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009844 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009845 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009846 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009847
9848 if (root_log_pinned) {
9849 btrfs_end_log_trans(root);
9850 root_log_pinned = false;
9851 }
9852 if (dest_log_pinned) {
9853 btrfs_end_log_trans(dest);
9854 dest_log_pinned = false;
9855 }
9856 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009857 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009858out_notrans:
9859 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009860 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009861 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009862 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009863
9864 return ret;
9865}
9866
9867static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9868 struct btrfs_root *root,
9869 struct inode *dir,
9870 struct dentry *dentry)
9871{
9872 int ret;
9873 struct inode *inode;
9874 u64 objectid;
9875 u64 index;
9876
9877 ret = btrfs_find_free_ino(root, &objectid);
9878 if (ret)
9879 return ret;
9880
9881 inode = btrfs_new_inode(trans, root, dir,
9882 dentry->d_name.name,
9883 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009884 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009885 objectid,
9886 S_IFCHR | WHITEOUT_MODE,
9887 &index);
9888
9889 if (IS_ERR(inode)) {
9890 ret = PTR_ERR(inode);
9891 return ret;
9892 }
9893
9894 inode->i_op = &btrfs_special_inode_operations;
9895 init_special_inode(inode, inode->i_mode,
9896 WHITEOUT_DEV);
9897
9898 ret = btrfs_init_inode_security(trans, inode, dir,
9899 &dentry->d_name);
9900 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009901 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009902
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009903 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9904 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009905 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009906 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009907
9908 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009909out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009910 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009911 if (ret)
9912 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009913 iput(inode);
9914
Filipe Mananac9901612016-05-05 01:41:57 +01009915 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009916}
9917
Chris Mason39279cc2007-06-12 06:35:45 -04009918static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009919 struct inode *new_dir, struct dentry *new_dentry,
9920 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009921{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009922 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009923 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009924 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009925 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9926 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009927 struct inode *new_inode = d_inode(new_dentry);
9928 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009929 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009930 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009931 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009932 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009933 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009934
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009935 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009936 return -EPERM;
9937
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009938 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009939 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009940 return -EXDEV;
9941
Li Zefan33345d012011-04-20 10:31:50 +08009942 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009943 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009944 return -ENOTEMPTY;
9945
Chris Mason39279cc2007-06-12 06:35:45 -04009946 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009947 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009948 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009949
9950
9951 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009952 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009953 new_dentry->d_name.name,
9954 new_dentry->d_name.len);
9955
9956 if (ret) {
9957 if (ret == -EEXIST) {
9958 /* we shouldn't get
9959 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309960 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009961 return ret;
9962 }
9963 } else {
9964 /* maybe -EOVERFLOW */
9965 return ret;
9966 }
9967 }
9968 ret = 0;
9969
Chris Mason5a3f23d2009-03-31 13:27:11 -04009970 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009971 * we're using rename to replace one file with another. Start IO on it
9972 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009973 */
Chris Mason8d875f92014-08-12 10:47:42 -07009974 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009975 filemap_flush(old_inode->i_mapping);
9976
Yan, Zheng76dda932009-09-21 16:00:26 -04009977 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009978 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009979 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009980 /*
9981 * We want to reserve the absolute worst case amount of items. So if
9982 * both inodes are subvols and we need to unlink them then that would
9983 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009984 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009985 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9986 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009987 * If our rename has the whiteout flag, we need more 5 units for the
9988 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9989 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009990 */
Filipe Manana5062af32016-05-05 10:26:26 +01009991 trans_num_items = 11;
9992 if (flags & RENAME_WHITEOUT)
9993 trans_num_items += 5;
9994 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009995 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009996 ret = PTR_ERR(trans);
9997 goto out_notrans;
9998 }
Chris Mason5f39d392007-10-15 16:14:19 -04009999
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010000 if (dest != root)
10001 btrfs_record_root_in_trans(trans, dest);
10002
Nikolay Borisov877574e2017-02-20 13:50:33 +020010003 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010004 if (ret)
10005 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -040010006
Miao Xie67de1172013-12-26 13:07:06 +080010007 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +080010008 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010009 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010010 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010011 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +010010012 btrfs_pin_log_trans(root);
10013 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -040010014 ret = btrfs_insert_inode_ref(trans, dest,
10015 new_dentry->d_name.name,
10016 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +080010017 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010018 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010019 if (ret)
10020 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010021 }
Chris Mason5a3f23d2009-03-31 13:27:11 -040010022
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010023 inode_inc_iversion(old_dir);
10024 inode_inc_iversion(new_dir);
10025 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010026 old_dir->i_ctime = old_dir->i_mtime =
10027 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010028 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -040010029
Chris Mason12fcfd22009-03-24 10:24:20 -040010030 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +010010031 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10032 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -040010033
Li Zefan33345d012011-04-20 10:31:50 +080010034 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010035 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
10036 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
10037 old_dentry->d_name.name,
10038 old_dentry->d_name.len);
10039 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010040 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10041 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +000010042 old_dentry->d_name.name,
10043 old_dentry->d_name.len);
10044 if (!ret)
10045 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010046 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010047 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010048 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010049 goto out_fail;
10050 }
Chris Mason39279cc2007-06-12 06:35:45 -040010051
10052 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010053 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010054 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010055 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010056 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10057 root_objectid = BTRFS_I(new_inode)->location.objectid;
10058 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10059 root_objectid,
10060 new_dentry->d_name.name,
10061 new_dentry->d_name.len);
10062 BUG_ON(new_inode->i_nlink == 0);
10063 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010064 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10065 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010066 new_dentry->d_name.name,
10067 new_dentry->d_name.len);
10068 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010069 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010070 ret = btrfs_orphan_add(trans,
10071 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010072 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010073 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010074 goto out_fail;
10075 }
Chris Mason39279cc2007-06-12 06:35:45 -040010076 }
Josef Bacikaec74772008-07-24 12:12:38 -040010077
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010078 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010079 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010080 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010081 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010082 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010083 goto out_fail;
10084 }
Chris Mason39279cc2007-06-12 06:35:45 -040010085
Miao Xie67de1172013-12-26 13:07:06 +080010086 if (old_inode->i_nlink == 1)
10087 BTRFS_I(old_inode)->dir_index = index;
10088
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010089 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010090 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010091
David Sterbaf85b7372017-01-20 14:54:07 +010010092 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10093 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010094 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010095 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010096 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010097
10098 if (flags & RENAME_WHITEOUT) {
10099 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10100 old_dentry);
10101
10102 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010103 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010104 goto out_fail;
10105 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010106 }
Chris Mason39279cc2007-06-12 06:35:45 -040010107out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010108 /*
10109 * If we have pinned the log and an error happened, we unpin tasks
10110 * trying to sync the log and force them to fallback to a transaction
10111 * commit if the log currently contains any of the inodes involved in
10112 * this rename operation (to ensure we do not persist a log with an
10113 * inconsistent state for any of these inodes or leading to any
10114 * inconsistencies when replayed). If the transaction was aborted, the
10115 * abortion reason is propagated to userspace when attempting to commit
10116 * the transaction. If the log does not contain any of these inodes, we
10117 * allow the tasks to sync it.
10118 */
10119 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010120 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10121 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10122 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010123 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010124 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010125 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010126
10127 btrfs_end_log_trans(root);
10128 log_pinned = false;
10129 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010130 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010131out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010132 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010133 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010134
Chris Mason39279cc2007-06-12 06:35:45 -040010135 return ret;
10136}
10137
Miklos Szeredi80ace852014-07-23 15:15:32 +020010138static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10139 struct inode *new_dir, struct dentry *new_dentry,
10140 unsigned int flags)
10141{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010142 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010143 return -EINVAL;
10144
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010145 if (flags & RENAME_EXCHANGE)
10146 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10147 new_dentry);
10148
10149 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010150}
10151
Miao Xie8ccf6f192012-10-25 09:28:04 +000010152static void btrfs_run_delalloc_work(struct btrfs_work *work)
10153{
10154 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010155 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010156
10157 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10158 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010159 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010160 filemap_flush(inode->i_mapping);
10161 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10162 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010163 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010164
10165 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010166 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010167 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010168 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010169 complete(&delalloc_work->completion);
10170}
10171
10172struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010173 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010174{
10175 struct btrfs_delalloc_work *work;
10176
David Sterba100d5702015-12-08 14:39:32 +010010177 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010178 if (!work)
10179 return NULL;
10180
10181 init_completion(&work->completion);
10182 INIT_LIST_HEAD(&work->list);
10183 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010184 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010185 WARN_ON_ONCE(!inode);
10186 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10187 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010188
10189 return work;
10190}
10191
10192void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10193{
10194 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010195 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010196}
10197
Chris Masond352ac62008-09-29 15:18:18 -040010198/*
10199 * some fairly slow code that needs optimization. This walks the list
10200 * of all the inodes with pending delalloc and forces them to disk.
10201 */
Miao Xie6c255e62014-03-06 13:55:01 +080010202static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10203 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010204{
Chris Masonea8c2812008-08-04 23:17:27 -040010205 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010206 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010207 struct btrfs_delalloc_work *work, *next;
10208 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010209 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010210 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010211
Miao Xie8ccf6f192012-10-25 09:28:04 +000010212 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010213 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010214
Miao Xie573bfb72014-03-06 13:55:03 +080010215 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010216 spin_lock(&root->delalloc_lock);
10217 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010218 while (!list_empty(&splice)) {
10219 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010220 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010221
Miao Xieeb73c1b2013-05-15 07:48:22 +000010222 list_move_tail(&binode->delalloc_inodes,
10223 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010224 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010225 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010226 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010227 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010228 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010229 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010230
David Sterba651d4942015-11-27 19:24:16 +010010231 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010232 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010233 if (delay_iput)
10234 btrfs_add_delayed_iput(inode);
10235 else
10236 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010237 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010238 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010239 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010240 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010241 btrfs_queue_work(root->fs_info->flush_workers,
10242 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010243 ret++;
10244 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010245 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010246 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010247 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010248 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010249 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010250
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010251out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010252 list_for_each_entry_safe(work, next, &works, list) {
10253 list_del_init(&work->list);
10254 btrfs_wait_and_free_delalloc_work(work);
10255 }
10256
Miao Xieeb73c1b2013-05-15 07:48:22 +000010257 if (!list_empty_careful(&splice)) {
10258 spin_lock(&root->delalloc_lock);
10259 list_splice_tail(&splice, &root->delalloc_inodes);
10260 spin_unlock(&root->delalloc_lock);
10261 }
Miao Xie573bfb72014-03-06 13:55:03 +080010262 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010263 return ret;
10264}
10265
10266int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10267{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010268 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010269 int ret;
10270
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010271 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010272 return -EROFS;
10273
Miao Xie6c255e62014-03-06 13:55:01 +080010274 ret = __start_delalloc_inodes(root, delay_iput, -1);
10275 if (ret > 0)
10276 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010277 return ret;
10278}
10279
Miao Xie6c255e62014-03-06 13:55:01 +080010280int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10281 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010282{
10283 struct btrfs_root *root;
10284 struct list_head splice;
10285 int ret;
10286
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010287 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010288 return -EROFS;
10289
10290 INIT_LIST_HEAD(&splice);
10291
Miao Xie573bfb72014-03-06 13:55:03 +080010292 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010293 spin_lock(&fs_info->delalloc_root_lock);
10294 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010295 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010296 root = list_first_entry(&splice, struct btrfs_root,
10297 delalloc_root);
10298 root = btrfs_grab_fs_root(root);
10299 BUG_ON(!root);
10300 list_move_tail(&root->delalloc_root,
10301 &fs_info->delalloc_roots);
10302 spin_unlock(&fs_info->delalloc_root_lock);
10303
Miao Xie6c255e62014-03-06 13:55:01 +080010304 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010305 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010306 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010307 goto out;
10308
Miao Xie6c255e62014-03-06 13:55:01 +080010309 if (nr != -1) {
10310 nr -= ret;
10311 WARN_ON(nr < 0);
10312 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010313 spin_lock(&fs_info->delalloc_root_lock);
10314 }
10315 spin_unlock(&fs_info->delalloc_root_lock);
10316
Miao Xie6c255e62014-03-06 13:55:01 +080010317 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010318out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010319 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010320 spin_lock(&fs_info->delalloc_root_lock);
10321 list_splice_tail(&splice, &fs_info->delalloc_roots);
10322 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010323 }
Miao Xie573bfb72014-03-06 13:55:03 +080010324 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010325 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010326}
10327
Chris Mason39279cc2007-06-12 06:35:45 -040010328static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10329 const char *symname)
10330{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010331 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010332 struct btrfs_trans_handle *trans;
10333 struct btrfs_root *root = BTRFS_I(dir)->root;
10334 struct btrfs_path *path;
10335 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010336 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010337 int err;
10338 int drop_inode = 0;
10339 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010340 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010341 int name_len;
10342 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010343 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010344 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010345 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010346
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010347 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010348 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010349 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010350
Josef Bacik9ed74f22009-09-11 16:12:44 -040010351 /*
10352 * 2 items for inode item and ref
10353 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010354 * 1 item for updating parent inode item
10355 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010356 * 1 item for xattr if selinux is on
10357 */
Filipe Manana9269d122015-12-31 18:16:29 +000010358 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010359 if (IS_ERR(trans))
10360 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010361
Li Zefan581bb052011-04-20 10:06:11 +080010362 err = btrfs_find_free_ino(root, &objectid);
10363 if (err)
10364 goto out_unlock;
10365
Josef Bacikaec74772008-07-24 12:12:38 -040010366 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010367 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10368 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010369 if (IS_ERR(inode)) {
10370 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010371 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010372 }
Chris Mason39279cc2007-06-12 06:35:45 -040010373
Casey Schauflerad19db72011-12-15 10:09:07 -050010374 /*
10375 * If the active LSM wants to access the inode during
10376 * d_instantiate it needs these. Smack checks to see
10377 * if the filesystem supports xattrs by looking at the
10378 * ops vector.
10379 */
10380 inode->i_fop = &btrfs_file_operations;
10381 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010382 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010383 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10384
10385 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10386 if (err)
10387 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010388
Chris Mason39279cc2007-06-12 06:35:45 -040010389 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010390 if (!path) {
10391 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010392 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010393 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010394 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010395 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010396 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010397 datasize = btrfs_file_extent_calc_inline_size(name_len);
10398 err = btrfs_insert_empty_item(trans, root, path, &key,
10399 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010400 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010401 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010402 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010403 }
Chris Mason5f39d392007-10-15 16:14:19 -040010404 leaf = path->nodes[0];
10405 ei = btrfs_item_ptr(leaf, path->slots[0],
10406 struct btrfs_file_extent_item);
10407 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10408 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010409 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010410 btrfs_set_file_extent_encryption(leaf, ei, 0);
10411 btrfs_set_file_extent_compression(leaf, ei, 0);
10412 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10413 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10414
Chris Mason39279cc2007-06-12 06:35:45 -040010415 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010416 write_extent_buffer(leaf, symname, ptr, name_len);
10417 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010418 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010419
Chris Mason39279cc2007-06-12 06:35:45 -040010420 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010421 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010422 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010423 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010424 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010425 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010426 /*
10427 * Last step, add directory indexes for our symlink inode. This is the
10428 * last step to avoid extra cleanup of these indexes if an error happens
10429 * elsewhere above.
10430 */
10431 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010432 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10433 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010434 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010435 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010436 goto out_unlock_inode;
10437 }
10438
10439 unlock_new_inode(inode);
10440 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010441
10442out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010443 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010444 if (drop_inode) {
10445 inode_dec_link_count(inode);
10446 iput(inode);
10447 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010448 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010449 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010450
10451out_unlock_inode:
10452 drop_inode = 1;
10453 unlock_new_inode(inode);
10454 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010455}
Chris Mason16432982008-04-10 10:23:21 -040010456
Josef Bacik0af3d002010-06-21 14:48:16 -040010457static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10458 u64 start, u64 num_bytes, u64 min_size,
10459 loff_t actual_len, u64 *alloc_hint,
10460 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010461{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010462 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010463 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10464 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010465 struct btrfs_root *root = BTRFS_I(inode)->root;
10466 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010467 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010468 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010469 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010470 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010471 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010472 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010473 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010474
Josef Bacik0af3d002010-06-21 14:48:16 -040010475 if (trans)
10476 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010477 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010478 if (own_trans) {
10479 trans = btrfs_start_transaction(root, 3);
10480 if (IS_ERR(trans)) {
10481 ret = PTR_ERR(trans);
10482 break;
10483 }
Yan Zhengd899e052008-10-30 14:25:28 -040010484 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010485
Byongho Leeee221842015-12-15 01:42:10 +090010486 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010487 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010488 /*
10489 * If we are severely fragmented we could end up with really
10490 * small allocations, so if the allocator is returning small
10491 * chunks lets make its job easier by only searching for those
10492 * sized chunks.
10493 */
10494 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010495 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10496 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010497 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010498 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010499 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010500 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010501 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010502 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010503
Josef Bacik0b670dc2015-09-23 17:11:16 -040010504 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010505 ret = insert_reserved_file_extent(trans, inode,
10506 cur_offset, ins.objectid,
10507 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010508 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010509 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010510 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010511 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010512 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010513 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010514 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010515 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010516 break;
10517 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010518
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010519 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010520 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010521
Josef Bacik5dc562c2012-08-17 13:14:17 -040010522 em = alloc_extent_map();
10523 if (!em) {
10524 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10525 &BTRFS_I(inode)->runtime_flags);
10526 goto next;
10527 }
10528
10529 em->start = cur_offset;
10530 em->orig_start = cur_offset;
10531 em->len = ins.offset;
10532 em->block_start = ins.objectid;
10533 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010534 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010535 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010536 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010537 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10538 em->generation = trans->transid;
10539
10540 while (1) {
10541 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010542 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010543 write_unlock(&em_tree->lock);
10544 if (ret != -EEXIST)
10545 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010546 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010547 cur_offset + ins.offset - 1,
10548 0);
10549 }
10550 free_extent_map(em);
10551next:
Yan Zhengd899e052008-10-30 14:25:28 -040010552 num_bytes -= ins.offset;
10553 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010554 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010555
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010556 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010557 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010558 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010559 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010560 (actual_len > inode->i_size) &&
10561 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010562 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010563 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010564 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010565 i_size = cur_offset;
10566 i_size_write(inode, i_size);
10567 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010568 }
10569
Yan Zhengd899e052008-10-30 14:25:28 -040010570 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010571
10572 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010573 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010574 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010575 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010576 break;
10577 }
Yan Zhengd899e052008-10-30 14:25:28 -040010578
Josef Bacik0af3d002010-06-21 14:48:16 -040010579 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010580 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010581 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010582 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010583 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010584 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010585 return ret;
10586}
10587
Josef Bacik0af3d002010-06-21 14:48:16 -040010588int btrfs_prealloc_file_range(struct inode *inode, int mode,
10589 u64 start, u64 num_bytes, u64 min_size,
10590 loff_t actual_len, u64 *alloc_hint)
10591{
10592 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10593 min_size, actual_len, alloc_hint,
10594 NULL);
10595}
10596
10597int btrfs_prealloc_file_range_trans(struct inode *inode,
10598 struct btrfs_trans_handle *trans, int mode,
10599 u64 start, u64 num_bytes, u64 min_size,
10600 loff_t actual_len, u64 *alloc_hint)
10601{
10602 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10603 min_size, actual_len, alloc_hint, trans);
10604}
10605
Chris Masone6dcd2d2008-07-17 12:53:50 -040010606static int btrfs_set_page_dirty(struct page *page)
10607{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010608 return __set_page_dirty_nobuffers(page);
10609}
10610
Al Viro10556cb22011-06-20 19:28:19 -040010611static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010612{
Li Zefanb83cc962010-12-20 16:04:08 +080010613 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010614 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010615
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010616 if (mask & MAY_WRITE &&
10617 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10618 if (btrfs_root_readonly(root))
10619 return -EROFS;
10620 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10621 return -EACCES;
10622 }
Al Viro2830ba72011-06-20 19:16:29 -040010623 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010624}
Chris Mason39279cc2007-06-12 06:35:45 -040010625
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010626static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10627{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010628 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010629 struct btrfs_trans_handle *trans;
10630 struct btrfs_root *root = BTRFS_I(dir)->root;
10631 struct inode *inode = NULL;
10632 u64 objectid;
10633 u64 index;
10634 int ret = 0;
10635
10636 /*
10637 * 5 units required for adding orphan entry
10638 */
10639 trans = btrfs_start_transaction(root, 5);
10640 if (IS_ERR(trans))
10641 return PTR_ERR(trans);
10642
10643 ret = btrfs_find_free_ino(root, &objectid);
10644 if (ret)
10645 goto out;
10646
10647 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010648 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010649 if (IS_ERR(inode)) {
10650 ret = PTR_ERR(inode);
10651 inode = NULL;
10652 goto out;
10653 }
10654
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010655 inode->i_fop = &btrfs_file_operations;
10656 inode->i_op = &btrfs_file_inode_operations;
10657
10658 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010659 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10660
Chris Masonb0d5d102014-09-08 13:08:51 -070010661 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10662 if (ret)
10663 goto out_inode;
10664
10665 ret = btrfs_update_inode(trans, root, inode);
10666 if (ret)
10667 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010668 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010669 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010670 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010671
Filipe Manana5762b5c2014-08-01 00:10:32 +010010672 /*
10673 * We set number of links to 0 in btrfs_new_inode(), and here we set
10674 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10675 * through:
10676 *
10677 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10678 */
10679 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010680 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010681 d_tmpfile(dentry, inode);
10682 mark_inode_dirty(inode);
10683
10684out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010685 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010686 if (ret)
10687 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010688 btrfs_balance_delayed_items(fs_info);
10689 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010690 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010691
10692out_inode:
10693 unlock_new_inode(inode);
10694 goto out;
10695
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010696}
10697
David Sterba20a7db82017-02-17 16:24:29 +010010698__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010699static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010700{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010701 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010702}
10703
Josef Bacikc6100a42017-05-05 11:57:13 -040010704static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10705{
10706 struct inode *inode = private_data;
10707 return btrfs_sb(inode->i_sb);
10708}
10709
10710static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10711 u64 start, u64 end)
10712{
10713 struct inode *inode = private_data;
10714 u64 isize;
10715
10716 isize = i_size_read(inode);
10717 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10718 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10719 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10720 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10721 }
10722}
10723
10724void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10725{
10726 struct inode *inode = private_data;
10727 unsigned long index = start >> PAGE_SHIFT;
10728 unsigned long end_index = end >> PAGE_SHIFT;
10729 struct page *page;
10730
10731 while (index <= end_index) {
10732 page = find_get_page(inode->i_mapping, index);
10733 ASSERT(page); /* Pages should be in the extent_io_tree */
10734 set_page_writeback(page);
10735 put_page(page);
10736 index++;
10737 }
10738}
10739
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010740static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010741 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010742 .lookup = btrfs_lookup,
10743 .create = btrfs_create,
10744 .unlink = btrfs_unlink,
10745 .link = btrfs_link,
10746 .mkdir = btrfs_mkdir,
10747 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010748 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010749 .symlink = btrfs_symlink,
10750 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010751 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010752 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010753 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010754 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010755 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010756 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010757 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010758};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010759static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010760 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010761 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010762 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010763};
Yan, Zheng76dda932009-09-21 16:00:26 -040010764
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010765static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010766 .llseek = generic_file_llseek,
10767 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010768 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010769 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010770 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010771#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010772 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010773#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010774 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010775 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010776};
10777
David Sterba20e55062015-11-19 11:42:28 +010010778static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010779 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010780 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010781 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010782 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010783 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010784 .tree_fs_info = iotree_fs_info,
10785 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010786
10787 /* optional callbacks */
10788 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010789 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010790 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010791 .set_bit_hook = btrfs_set_bit_hook,
10792 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010793 .merge_extent_hook = btrfs_merge_extent_hook,
10794 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010795 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010796};
10797
Chris Mason35054392009-01-21 13:11:13 -050010798/*
10799 * btrfs doesn't support the bmap operation because swapfiles
10800 * use bmap to make a mapping of extents in the file. They assume
10801 * these extents won't change over the life of the file and they
10802 * use the bmap result to do IO directly to the drive.
10803 *
10804 * the btrfs bmap call would return logical addresses that aren't
10805 * suitable for IO and they also will change frequently as COW
10806 * operations happen. So, swapfile + btrfs == corruption.
10807 *
10808 * For now we're avoiding this by dropping bmap.
10809 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010810static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010811 .readpage = btrfs_readpage,
10812 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010813 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010814 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010815 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010816 .invalidatepage = btrfs_invalidatepage,
10817 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010818 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010819 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010820};
10821
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010822static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010823 .readpage = btrfs_readpage,
10824 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010825 .invalidatepage = btrfs_invalidatepage,
10826 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010827};
10828
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010829static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010830 .getattr = btrfs_getattr,
10831 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010832 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010833 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010834 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010835 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010836 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010837 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010838};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010839static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010840 .getattr = btrfs_getattr,
10841 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010842 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010843 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010844 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010845 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010846 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010847};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010848static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010849 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010850 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010851 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010852 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010853 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010854 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010855};
Yan, Zheng76dda932009-09-21 16:00:26 -040010856
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010857const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010858 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010859 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010860};