blob: d94e3f68b9b134f8e3027dec70f1952c42e8ce52 [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>
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040059#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000060#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080061#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080062#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Filipe Mananaf28a4922015-12-08 19:23:20 +000069struct btrfs_dio_data {
70 u64 outstanding_extents;
71 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 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400319 trans->block_rsv = &fs_info->delalloc_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 Borisov691fa052017-02-20 13:50:42 +0200351 btrfs_delalloc_release_metadata(BTRFS_I(inode), end + 1 - start);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200352 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400353out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800354 /*
355 * Don't forget to free the reserved space, as for inlined extent
356 * it won't count as data extent, free them directly here.
357 * And at reserve time, it's always aligned to page size, so
358 * just free one page here.
359 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800360 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000361 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400362 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400363 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400364}
365
Chris Mason771ed682008-11-06 22:02:51 -0500366struct async_extent {
367 u64 start;
368 u64 ram_size;
369 u64 compressed_size;
370 struct page **pages;
371 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800372 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500373 struct list_head list;
374};
375
376struct async_cow {
377 struct inode *inode;
378 struct btrfs_root *root;
379 struct page *locked_page;
380 u64 start;
381 u64 end;
382 struct list_head extents;
383 struct btrfs_work work;
384};
385
386static noinline int add_async_extent(struct async_cow *cow,
387 u64 start, u64 ram_size,
388 u64 compressed_size,
389 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800390 unsigned long nr_pages,
391 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500392{
393 struct async_extent *async_extent;
394
395 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100396 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500397 async_extent->start = start;
398 async_extent->ram_size = ram_size;
399 async_extent->compressed_size = compressed_size;
400 async_extent->pages = pages;
401 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800402 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500403 list_add_tail(&async_extent->list, &cow->extents);
404 return 0;
405}
406
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300407static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400409 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800410
411 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400412 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800413 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200414 /* defrag ioctl */
415 if (BTRFS_I(inode)->defrag_compress)
416 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800417 /* bad compression ratios */
418 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
419 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400420 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800421 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200422 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300423 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800424 return 0;
425}
426
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200427static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800428 u64 start, u64 end, u64 num_bytes, u64 small_write)
429{
430 /* If this is a small write inside eof, kick off a defrag */
431 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200432 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800433 btrfs_add_inode_defrag(NULL, inode);
434}
435
Chris Masonc8b97812008-10-29 14:49:59 -0400436/*
Chris Mason771ed682008-11-06 22:02:51 -0500437 * we create compressed extents in two phases. The first
438 * phase compresses a range of pages that have already been
439 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400440 *
Chris Mason771ed682008-11-06 22:02:51 -0500441 * This is done inside an ordered work queue, and the compression
442 * is spread across many cpus. The actual IO submission is step
443 * two, and the ordered work queue takes care of making sure that
444 * happens in the same order things were put onto the queue by
445 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400446 *
Chris Mason771ed682008-11-06 22:02:51 -0500447 * If this code finds it can't get good compression, it puts an
448 * entry onto the work queue to write the uncompressed bytes. This
449 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300450 * are written in the same order that the flusher thread sent them
451 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400452 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100453static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500454 struct page *locked_page,
455 u64 start, u64 end,
456 struct async_cow *async_cow,
457 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400458{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400459 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db2b2007-08-27 16:49:44 -0400460 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400461 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400462 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400463 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500464 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400465 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400466 struct page **pages = NULL;
467 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400468 unsigned long total_compressed = 0;
469 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400470 int i;
471 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400472 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400473 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400474
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200475 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
476 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400477
Chris Mason42dc7ba2008-12-15 11:44:56 -0500478 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400479again:
480 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300481 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100482 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
483 nr_pages = min_t(unsigned long, nr_pages,
484 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400485
Chris Masonf03d9301f2009-02-04 09:31:06 -0500486 /*
487 * we don't want to send crud past the end of i_size through
488 * compression, that's just a waste of CPU time. So, if the
489 * end of the file is before the start of our current
490 * requested range of bytes, we bail out to the uncompressed
491 * cleanup code that can deal with all of this.
492 *
493 * It isn't really the fastest way to fix things, but this is a
494 * very uncommon corner.
495 */
496 if (actual_end <= start)
497 goto cleanup_and_bail_uncompressed;
498
Chris Masonc8b97812008-10-29 14:49:59 -0400499 total_compressed = actual_end - start;
500
Shilong Wang4bcbb332014-10-07 18:44:35 -0400501 /*
502 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400503 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400504 */
505 if (total_compressed <= blocksize &&
506 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
507 goto cleanup_and_bail_uncompressed;
508
David Sterba069eac72017-02-14 19:36:54 +0100509 total_compressed = min_t(unsigned long, total_compressed,
510 BTRFS_MAX_UNCOMPRESSED);
Qu Wenruofda28322013-02-26 08:10:22 +0000511 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500512 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400513 total_in = 0;
514 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400515
Chris Mason771ed682008-11-06 22:02:51 -0500516 /*
517 * we do compression for mount -o compress and when the
518 * inode has not been flagged as nocompress. This flag can
519 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400520 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300521 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400522 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100523 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800524 if (!pages) {
525 /* just bail out to the uncompressed code */
526 goto cont;
527 }
Chris Mason179e29e2007-11-01 11:28:41 -0400528
David Sterbaeec63c62017-07-17 19:41:31 +0200529 if (BTRFS_I(inode)->defrag_compress)
530 compress_type = BTRFS_I(inode)->defrag_compress;
531 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200532 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800533
Chris Mason4adaa612013-03-26 13:07:00 -0400534 /*
535 * we need to call clear_page_dirty_for_io on each
536 * page in the range. Otherwise applications with the file
537 * mmap'd can wander in and change the page contents while
538 * we are compressing them.
539 *
540 * If the compression fails for any reason, we set the pages
541 * dirty again later on.
542 */
543 extent_range_clear_dirty_for_io(inode, start, end);
544 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800545 ret = btrfs_compress_pages(compress_type,
546 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 */
Chris Mason771ed682008-11-06 22:02:51 -0500573 if (ret || total_in < (actual_end - start)) {
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 |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100587 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100588 unsigned long page_error_op;
589
Josef Bacik151a41b2013-07-29 13:22:24 -0400590 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
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.
Chris Mason771ed682008-11-06 22:02:51 -0500597 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800598 extent_clear_unlock_delalloc(inode, start, end, end,
599 NULL, clear_flags,
600 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400601 PAGE_CLEAR_DIRTY |
602 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100603 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400604 PAGE_END_WRITEBACK);
Filipe Manana1c81ba22017-03-08 16:43:49 +0000605 if (ret == 0)
606 btrfs_free_reserved_data_space_noquota(inode,
607 start,
608 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400609 goto free_pages_out;
610 }
611 }
612
613 if (will_compress) {
614 /*
615 * we aren't doing an inline extent round the compressed size
616 * up to a block size boundary so the allocator does sane
617 * things
618 */
Qu Wenruofda28322013-02-26 08:10:22 +0000619 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400620
621 /*
622 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300623 * win, compare the page count read with the blocks on disk,
624 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400625 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300626 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300627 if (total_compressed + blocksize <= total_in) {
Chris Masonc8b97812008-10-29 14:49:59 -0400628 num_bytes = 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 */
636 add_async_extent(async_cow, start, num_bytes,
David Sterba4d3a8002017-02-14 19:04:07 +0100637 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700638 compress_type);
639
640 if (start + num_bytes < end) {
641 start += num_bytes;
642 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,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200861 async_extent->nr_pages)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100862 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
863 struct page *p = async_extent->pages[0];
864 const u64 start = async_extent->start;
865 const u64 end = start + async_extent->ram_size - 1;
866
867 p->mapping = inode->i_mapping;
868 tree->ops->writepage_end_io_hook(p, start, end,
869 NULL, 0);
870 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800871 extent_clear_unlock_delalloc(inode, start, end, end,
872 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100873 PAGE_END_WRITEBACK |
874 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100875 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100876 }
Chris Mason771ed682008-11-06 22:02:51 -0500877 alloc_hint = ins.objectid + ins.offset;
878 kfree(async_extent);
879 cond_resched();
880 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100881 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500882out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400883 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400884 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100885out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400886 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500887 async_extent->start +
888 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800889 async_extent->start +
890 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400891 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100892 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400893 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
894 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100895 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
896 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100897 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100898 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500899 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500900}
901
Josef Bacik4b46fce2010-05-23 11:00:55 -0400902static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
903 u64 num_bytes)
904{
905 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
906 struct extent_map *em;
907 u64 alloc_hint = 0;
908
909 read_lock(&em_tree->lock);
910 em = search_extent_mapping(em_tree, start, num_bytes);
911 if (em) {
912 /*
913 * if block start isn't an actual block number then find the
914 * first block in this inode and use that as a hint. If that
915 * block is also bogus then just don't worry about it.
916 */
917 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
918 free_extent_map(em);
919 em = search_extent_mapping(em_tree, 0, 0);
920 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
921 alloc_hint = em->block_start;
922 if (em)
923 free_extent_map(em);
924 } else {
925 alloc_hint = em->block_start;
926 free_extent_map(em);
927 }
928 }
929 read_unlock(&em_tree->lock);
930
931 return alloc_hint;
932}
933
Chris Mason771ed682008-11-06 22:02:51 -0500934/*
935 * when extent_io.c finds a delayed allocation range in the file,
936 * the call backs end up in this code. The basic idea is to
937 * allocate extents on disk for the range, and create ordered data structs
938 * in ram to track those extents.
939 *
940 * locked_page is the page that writepage had locked already. We use
941 * it to make sure we don't do extra locks or unlocks.
942 *
943 * *page_started is set to one if we unlock locked_page and do everything
944 * required to start IO on it. It may be clean and already done with
945 * IO when we return.
946 */
Josef Bacik00361582013-08-14 14:02:47 -0400947static noinline int cow_file_range(struct inode *inode,
948 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800949 u64 start, u64 end, u64 delalloc_end,
950 int *page_started, unsigned long *nr_written,
951 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500952{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400953 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400954 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500955 u64 alloc_hint = 0;
956 u64 num_bytes;
957 unsigned long ram_size;
958 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000959 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400960 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500961 struct btrfs_key ins;
962 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000963 unsigned clear_bits;
964 unsigned long page_ops;
965 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500966 int ret = 0;
967
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200968 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400969 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500970 ret = -EINVAL;
971 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400972 }
Chris Mason771ed682008-11-06 22:02:51 -0500973
Qu Wenruofda28322013-02-26 08:10:22 +0000974 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500975 num_bytes = max(blocksize, num_bytes);
976 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500977
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200978 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400979
Chris Mason771ed682008-11-06 22:02:51 -0500980 if (start == 0) {
981 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800982 ret = cow_file_range_inline(root, inode, start, end, 0,
983 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500984 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800985 extent_clear_unlock_delalloc(inode, start, end,
986 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400987 EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100988 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400989 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400990 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
991 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800992 btrfs_free_reserved_data_space_noquota(inode, start,
993 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500994 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300995 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500996 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500997 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100998 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100999 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001000 }
1001 }
Chris Masonc8b97812008-10-29 14:49:59 -04001002
1003 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001004 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001005
Josef Bacik4b46fce2010-05-23 11:00:55 -04001006 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001007 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1008 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001009
Chris Masond3977122009-01-05 21:25:51 -05001010 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +00001011 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001012 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001013 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001014 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001015 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001016 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001017 cur_alloc_size = ins.offset;
1018 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001019
Chris Mason771ed682008-11-06 22:02:51 -05001020 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001021 em = create_io_em(inode, start, ins.offset, /* len */
1022 start, /* orig_start */
1023 ins.objectid, /* block_start */
1024 ins.offset, /* block_len */
1025 ins.offset, /* orig_block_len */
1026 ram_size, /* ram_bytes */
1027 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001028 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001029 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001030 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001031 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001032
Chris Masone6dcd2d2008-07-17 12:53:50 -04001033 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001034 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001035 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001036 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001037
Yan Zheng17d217f2008-12-12 10:03:38 -05001038 if (root->root_key.objectid ==
1039 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1040 ret = btrfs_reloc_clone_csums(inode, start,
1041 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001042 /*
1043 * Only drop cache here, and process as normal.
1044 *
1045 * We must not allow extent_clear_unlock_delalloc()
1046 * at out_unlock label to free meta of this ordered
1047 * extent, as its meta should be freed by
1048 * btrfs_finish_ordered_io().
1049 *
1050 * So we must continue until @start is increased to
1051 * skip current ordered extent.
1052 */
Josef Bacik00361582013-08-14 14:02:47 -04001053 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001054 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1055 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001056 }
1057
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001058 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001059
Chris Masonc8b97812008-10-29 14:49:59 -04001060 /* we're not doing compressed IO, don't unlock the first
1061 * page (which the caller expects to stay locked), don't
1062 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001063 *
1064 * Do set the Private2 bit so we know this page was properly
1065 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001066 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001067 page_ops = unlock ? PAGE_UNLOCK : 0;
1068 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001069
Josef Bacikc2790a22013-07-29 11:20:47 -04001070 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001071 start + ram_size - 1,
1072 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001073 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001074 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001075 if (disk_num_bytes < cur_alloc_size)
1076 disk_num_bytes = 0;
1077 else
1078 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001079 num_bytes -= cur_alloc_size;
1080 alloc_hint = ins.objectid + ins.offset;
1081 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001082 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001083
1084 /*
1085 * btrfs_reloc_clone_csums() error, since start is increased
1086 * extent_clear_unlock_delalloc() at out_unlock label won't
1087 * free metadata of current ordered extent, we're OK to exit.
1088 */
1089 if (ret)
1090 goto out_unlock;
Chris Masonb888db2b2007-08-27 16:49:44 -04001091 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001092out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001093 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001094
Filipe Mananad9f85962014-08-25 10:43:00 +01001095out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001096 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001097out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001098 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001099 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001100out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001101 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1102 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001103 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1104 PAGE_END_WRITEBACK;
1105 /*
1106 * If we reserved an extent for our delalloc range (or a subrange) and
1107 * failed to create the respective ordered extent, then it means that
1108 * when we reserved the extent we decremented the extent's size from
1109 * the data space_info's bytes_may_use counter and incremented the
1110 * space_info's bytes_reserved counter by the same amount. We must make
1111 * sure extent_clear_unlock_delalloc() does not try to decrement again
1112 * the data space_info's bytes_may_use counter, therefore we do not pass
1113 * it the flag EXTENT_CLEAR_DATA_RESV.
1114 */
1115 if (extent_reserved) {
1116 extent_clear_unlock_delalloc(inode, start,
1117 start + cur_alloc_size,
1118 start + cur_alloc_size,
1119 locked_page,
1120 clear_bits,
1121 page_ops);
1122 start += cur_alloc_size;
1123 if (start >= end)
1124 goto out;
1125 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001126 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1127 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001128 clear_bits | EXTENT_CLEAR_DATA_RESV,
1129 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001130 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001131}
Chris Masonc8b97812008-10-29 14:49:59 -04001132
Chris Mason771ed682008-11-06 22:02:51 -05001133/*
1134 * work queue call back to started compression on a file and pages
1135 */
1136static noinline void async_cow_start(struct btrfs_work *work)
1137{
1138 struct async_cow *async_cow;
1139 int num_added = 0;
1140 async_cow = container_of(work, struct async_cow, work);
1141
1142 compress_file_range(async_cow->inode, async_cow->locked_page,
1143 async_cow->start, async_cow->end, async_cow,
1144 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001145 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001146 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001147 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001148 }
Chris Mason771ed682008-11-06 22:02:51 -05001149}
1150
1151/*
1152 * work queue call back to submit previously compressed pages
1153 */
1154static noinline void async_cow_submit(struct btrfs_work *work)
1155{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001156 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001157 struct async_cow *async_cow;
1158 struct btrfs_root *root;
1159 unsigned long nr_pages;
1160
1161 async_cow = container_of(work, struct async_cow, work);
1162
1163 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001164 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001165 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1166 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001167
David Sterbaee863952015-02-16 19:41:40 +01001168 /*
1169 * atomic_sub_return implies a barrier for waitqueue_active
1170 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001171 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001172 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001173 waitqueue_active(&fs_info->async_submit_wait))
1174 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001175
Chris Masond3977122009-01-05 21:25:51 -05001176 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001177 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001178}
Chris Masonc8b97812008-10-29 14:49:59 -04001179
Chris Mason771ed682008-11-06 22:02:51 -05001180static noinline void async_cow_free(struct btrfs_work *work)
1181{
1182 struct async_cow *async_cow;
1183 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001184 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001185 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001186 kfree(async_cow);
1187}
1188
1189static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1190 u64 start, u64 end, int *page_started,
1191 unsigned long *nr_written)
1192{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001193 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001194 struct async_cow *async_cow;
1195 struct btrfs_root *root = BTRFS_I(inode)->root;
1196 unsigned long nr_pages;
1197 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001198
Chris Masona3429ab2009-10-08 12:30:20 -04001199 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1200 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001201 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001202 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001203 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001204 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001205 async_cow->root = root;
1206 async_cow->locked_page = locked_page;
1207 async_cow->start = start;
1208
Wang Shilongf79707b2014-07-17 11:44:09 +08001209 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001210 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001211 cur_end = end;
1212 else
Byongho Leeee221842015-12-15 01:42:10 +09001213 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001214
1215 async_cow->end = cur_end;
1216 INIT_LIST_HEAD(&async_cow->extents);
1217
Liu Bo9e0af232014-08-15 23:36:53 +08001218 btrfs_init_work(&async_cow->work,
1219 btrfs_delalloc_helper,
1220 async_cow_start, async_cow_submit,
1221 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001222
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001223 nr_pages = (cur_end - start + PAGE_SIZE) >>
1224 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001225 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001226
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001227 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001228
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001229 while (atomic_read(&fs_info->async_submit_draining) &&
1230 atomic_read(&fs_info->async_delalloc_pages)) {
1231 wait_event(fs_info->async_submit_wait,
1232 (atomic_read(&fs_info->async_delalloc_pages) ==
1233 0));
Chris Mason771ed682008-11-06 22:02:51 -05001234 }
1235
1236 *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,
1584 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001585{
Josef Bacikc6100a42017-05-05 11:57:13 -04001586 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001587 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001588 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001589
Wang Shilong47059d92014-07-03 18:22:07 +08001590 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001591 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001592 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001593 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001594 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001595 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001596 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001597 ret = cow_file_range(inode, locked_page, start, end, end,
1598 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001599 } else {
1600 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1601 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001602 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001603 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001604 }
Qu Wenruo524272602017-03-08 10:25:52 +08001605 if (ret)
1606 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db2b2007-08-27 16:49:44 -04001607 return ret;
1608}
1609
Josef Bacikc6100a42017-05-05 11:57:13 -04001610static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001611 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001612{
Josef Bacikc6100a42017-05-05 11:57:13 -04001613 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001614 u64 size;
1615
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001616 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001617 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001618 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001619
Josef Bacikdcab6a32015-02-11 15:08:59 -05001620 size = orig->end - orig->start + 1;
1621 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001622 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001623 u64 new_size;
1624
1625 /*
Josef Bacikba117212015-03-13 15:01:24 -04001626 * See the explanation in btrfs_merge_extent_hook, the same
1627 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001628 */
1629 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001630 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001631 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001632 num_extents += count_max_extents(new_size);
1633 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001634 return;
1635 }
1636
Josef Bacik9e0baf62011-07-15 15:16:44 +00001637 spin_lock(&BTRFS_I(inode)->lock);
1638 BTRFS_I(inode)->outstanding_extents++;
1639 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001640}
1641
1642/*
1643 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1644 * extents so we can keep track of new extents that are just merged onto old
1645 * extents, such as when we are doing sequential writes, so we can properly
1646 * account for the metadata space we'll need.
1647 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001648static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001649 struct extent_state *new,
1650 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001651{
Josef Bacikc6100a42017-05-05 11:57:13 -04001652 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001653 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001654 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001655
Josef Bacik9ed74f22009-09-11 16:12:44 -04001656 /* not delalloc, ignore it */
1657 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001658 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001659
Josef Bacik8461a3d2015-03-13 15:12:08 -04001660 if (new->start > other->start)
1661 new_size = new->end - other->start + 1;
1662 else
1663 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001664
1665 /* we're not bigger than the max, unreserve the space and go */
1666 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1667 spin_lock(&BTRFS_I(inode)->lock);
1668 BTRFS_I(inode)->outstanding_extents--;
1669 spin_unlock(&BTRFS_I(inode)->lock);
1670 return;
1671 }
1672
1673 /*
Josef Bacikba117212015-03-13 15:01:24 -04001674 * We have to add up either side to figure out how many extents were
1675 * accounted for before we merged into one big extent. If the number of
1676 * extents we accounted for is <= the amount we need for the new range
1677 * then we can return, otherwise drop. Think of it like this
1678 *
1679 * [ 4k][MAX_SIZE]
1680 *
1681 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1682 * need 2 outstanding extents, on one side we have 1 and the other side
1683 * we have 1 so they are == and we can return. But in this case
1684 *
1685 * [MAX_SIZE+4k][MAX_SIZE+4k]
1686 *
1687 * Each range on their own accounts for 2 extents, but merged together
1688 * they are only 3 extents worth of accounting, so we need to drop in
1689 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001690 */
Josef Bacikba117212015-03-13 15:01:24 -04001691 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001692 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001693 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001694 num_extents += count_max_extents(old_size);
1695 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001696 return;
1697
Josef Bacik9e0baf62011-07-15 15:16:44 +00001698 spin_lock(&BTRFS_I(inode)->lock);
1699 BTRFS_I(inode)->outstanding_extents--;
1700 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001701}
1702
Miao Xieeb73c1b2013-05-15 07:48:22 +00001703static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1704 struct inode *inode)
1705{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001706 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1707
Miao Xieeb73c1b2013-05-15 07:48:22 +00001708 spin_lock(&root->delalloc_lock);
1709 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1710 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1711 &root->delalloc_inodes);
1712 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1713 &BTRFS_I(inode)->runtime_flags);
1714 root->nr_delalloc_inodes++;
1715 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001716 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001717 BUG_ON(!list_empty(&root->delalloc_root));
1718 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001719 &fs_info->delalloc_roots);
1720 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001721 }
1722 }
1723 spin_unlock(&root->delalloc_lock);
1724}
1725
1726static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001727 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001728{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001729 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001730
Miao Xieeb73c1b2013-05-15 07:48:22 +00001731 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001732 if (!list_empty(&inode->delalloc_inodes)) {
1733 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001734 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001735 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001736 root->nr_delalloc_inodes--;
1737 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001738 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001739 BUG_ON(list_empty(&root->delalloc_root));
1740 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001741 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001742 }
1743 }
1744 spin_unlock(&root->delalloc_lock);
1745}
1746
Chris Masond352ac62008-09-29 15:18:18 -04001747/*
1748 * extent_io.c set_bit_hook, used to track delayed allocation
1749 * bytes in this file, and to maintain the list of inodes that
1750 * have pending delalloc work to be done.
1751 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001752static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001753 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001754{
Josef Bacikc6100a42017-05-05 11:57:13 -04001755 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001756
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001757 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1758
Wang Shilong47059d92014-07-03 18:22:07 +08001759 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1760 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001761 /*
1762 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001763 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001764 * bit, which is only set or cleared with irqs on
1765 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001766 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001767 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001768 u64 len = state->end + 1 - state->start;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001769 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001770
Josef Bacik9e0baf62011-07-15 15:16:44 +00001771 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001772 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001773 } else {
1774 spin_lock(&BTRFS_I(inode)->lock);
1775 BTRFS_I(inode)->outstanding_extents++;
1776 spin_unlock(&BTRFS_I(inode)->lock);
1777 }
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 Bacik9e0baf62011-07-15 15:16:44 +00001831 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001832 *bits &= ~EXTENT_FIRST_DELALLOC;
Filipe Mananaa315e682017-03-06 23:04:20 +00001833 } else if (!(*bits & EXTENT_CLEAR_META_RESV)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001834 spin_lock(&inode->lock);
1835 inode->outstanding_extents -= num_extents;
1836 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001837 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001838
Josef Bacikb6d08f02013-09-27 14:57:43 -04001839 /*
1840 * We don't reserve metadata space for space cache inodes so we
1841 * don't need to call dellalloc_release_metadata if there is an
1842 * error.
1843 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001844 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001845 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001846 btrfs_delalloc_release_metadata(inode, len);
1847
Josef Bacik6a3891c2015-03-16 17:38:52 -04001848 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001849 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001850 return;
1851
Filipe Mananaa315e682017-03-06 23:04:20 +00001852 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1853 do_list && !(state->state & EXTENT_NORESERVE) &&
1854 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001855 btrfs_free_reserved_data_space_noquota(
1856 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001857 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001858
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001859 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1860 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001861 spin_lock(&inode->lock);
1862 inode->delalloc_bytes -= len;
1863 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001864 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001865 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001866 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001867 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001868 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001869
1870 if ((state->state & EXTENT_DELALLOC_NEW) &&
1871 (*bits & EXTENT_DELALLOC_NEW)) {
1872 spin_lock(&inode->lock);
1873 ASSERT(inode->new_delalloc_bytes >= len);
1874 inode->new_delalloc_bytes -= len;
1875 spin_unlock(&inode->lock);
1876 }
Chris Mason291d6732008-01-29 15:55:23 -05001877}
1878
Chris Masond352ac62008-09-29 15:18:18 -04001879/*
1880 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1881 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001882 *
1883 * return 1 if page cannot be merged to bio
1884 * return 0 if page can be merged to bio
1885 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001886 */
Mike Christie81a75f672016-06-05 14:31:54 -05001887int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001888 size_t size, struct bio *bio,
1889 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001890{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001891 struct inode *inode = page->mapping->host;
1892 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001893 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001894 u64 length = 0;
1895 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001896 int ret;
1897
Chris Mason771ed682008-11-06 22:02:51 -05001898 if (bio_flags & EXTENT_BIO_COMPRESSED)
1899 return 0;
1900
Kent Overstreet4f024f32013-10-11 15:44:27 -07001901 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001902 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001903 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1904 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001905 if (ret < 0)
1906 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001907 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001908 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001909 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001910}
1911
Chris Masond352ac62008-09-29 15:18:18 -04001912/*
1913 * in order to insert checksums into the metadata in large chunks,
1914 * we wait until bio submission time. All the pages in the bio are
1915 * checksummed and sums are attached onto the ordered extent record.
1916 *
1917 * At IO completion time the cums attached on the ordered extent record
1918 * are inserted into the btree
1919 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001920static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001921 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001922 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001923{
Josef Bacikc6100a42017-05-05 11:57:13 -04001924 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001925 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001926
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001927 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001928 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001929 return 0;
1930}
Chris Masone0156402008-04-16 11:15:20 -04001931
Chris Mason4a69a412008-11-06 22:03:00 -05001932/*
1933 * in order to insert checksums into the metadata in large chunks,
1934 * we wait until bio submission time. All the pages in the bio are
1935 * checksummed and sums are attached onto the ordered extent record.
1936 *
1937 * At IO completion time the cums attached on the ordered extent record
1938 * are inserted into the btree
1939 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001940static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001941 int mirror_num, unsigned long bio_flags,
1942 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001943{
Josef Bacikc6100a42017-05-05 11:57:13 -04001944 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001945 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001946 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001947
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001948 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001949 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001950 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001951 bio_endio(bio);
1952 }
Stefan Behrens61891922012-11-05 18:51:52 +01001953 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001954}
1955
Chris Masond352ac62008-09-29 15:18:18 -04001956/*
Chris Masoncad321a2008-12-17 14:51:42 -05001957 * extent_io.c submission hook. This does the right thing for csum calculation
1958 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001959 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001960static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001961 int mirror_num, unsigned long bio_flags,
1962 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001963{
Josef Bacikc6100a42017-05-05 11:57:13 -04001964 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001965 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001966 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301967 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001968 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001969 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001970 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001971
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001972 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001973
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001974 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301975 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001976
Mike Christie37226b22016-06-05 14:31:52 -05001977 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001978 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001979 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001980 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001981
Chris Masond20f7042008-12-08 16:58:54 -05001982 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001983 ret = btrfs_submit_compressed_read(inode, bio,
1984 mirror_num,
1985 bio_flags);
1986 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001987 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001988 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001989 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001990 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001991 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001992 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001993 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001994 /* csum items have already been cloned */
1995 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1996 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001997 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04001998 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
1999 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002000 __btrfs_submit_bio_start,
2001 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002002 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002003 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002004 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002005 if (ret)
2006 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002007 }
2008
Chris Mason0b86a832008-03-24 15:01:56 -04002009mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002010 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002011
2012out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002013 if (ret) {
2014 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002015 bio_endio(bio);
2016 }
Stefan Behrens61891922012-11-05 18:51:52 +01002017 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002018}
Chris Mason6885f302008-02-20 16:11:05 -05002019
Chris Masond352ac62008-09-29 15:18:18 -04002020/*
2021 * given a list of ordered sums record them in the inode. This happens
2022 * at IO completion time based on sums calculated at bio submission time.
2023 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002024static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002025 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002026{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002027 struct btrfs_ordered_sum *sum;
2028
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002029 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002030 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002031 btrfs_csum_file_blocks(trans,
2032 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002033 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002034 }
2035 return 0;
2036}
2037
Josef Bacik2ac55d42010-02-03 19:33:23 +00002038int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002039 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002040{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002041 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002042 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002043 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002044}
2045
Chris Masond352ac62008-09-29 15:18:18 -04002046/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002047struct btrfs_writepage_fixup {
2048 struct page *page;
2049 struct btrfs_work work;
2050};
2051
Christoph Hellwigb2950862008-12-02 09:54:17 -05002052static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002053{
2054 struct btrfs_writepage_fixup *fixup;
2055 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002056 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002057 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002058 struct page *page;
2059 struct inode *inode;
2060 u64 page_start;
2061 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002062 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002063
2064 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2065 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002066again:
Chris Mason247e7432008-07-17 12:53:51 -04002067 lock_page(page);
2068 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2069 ClearPageChecked(page);
2070 goto out_page;
2071 }
2072
2073 inode = page->mapping->host;
2074 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002075 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002076
David Sterbaff13db42015-12-03 14:30:40 +01002077 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002078 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002079
2080 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002081 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002082 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002083
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002084 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002085 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002086 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002087 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2088 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002089 unlock_page(page);
2090 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002091 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002092 goto again;
2093 }
Chris Mason247e7432008-07-17 12:53:51 -04002094
Qu Wenruo364ecf32017-02-27 15:10:38 +08002095 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002096 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002097 if (ret) {
2098 mapping_set_error(page->mapping, ret);
2099 end_extent_writepage(page, ret, page_start, page_end);
2100 ClearPageChecked(page);
2101 goto out;
2102 }
2103
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002104 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2105 0);
Chris Mason247e7432008-07-17 12:53:51 -04002106 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002107 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002108out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002109 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2110 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002111out_page:
2112 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002113 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002114 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002115 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002116}
2117
2118/*
2119 * There are a few paths in the higher layers of the kernel that directly
2120 * set the page dirty bit without asking the filesystem if it is a
2121 * good idea. This causes problems because we want to make sure COW
2122 * properly happens and the data=ordered rules are followed.
2123 *
Chris Masonc8b97812008-10-29 14:49:59 -04002124 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002125 * hasn't been properly setup for IO. We kick off an async process
2126 * to fix it up. The async helper will wait for ordered extents, set
2127 * the delalloc bit and make it safe to write the page.
2128 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002129static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002130{
2131 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002132 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002133 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002134
Chris Mason8b62b722009-09-02 16:53:46 -04002135 /* this page is properly in the ordered list */
2136 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002137 return 0;
2138
2139 if (PageChecked(page))
2140 return -EAGAIN;
2141
2142 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2143 if (!fixup)
2144 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002145
Chris Mason247e7432008-07-17 12:53:51 -04002146 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002147 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002148 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2149 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002150 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002151 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002152 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002153}
2154
Yan Zhengd899e052008-10-30 14:25:28 -04002155static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2156 struct inode *inode, u64 file_pos,
2157 u64 disk_bytenr, u64 disk_num_bytes,
2158 u64 num_bytes, u64 ram_bytes,
2159 u8 compression, u8 encryption,
2160 u16 other_encoding, int extent_type)
2161{
2162 struct btrfs_root *root = BTRFS_I(inode)->root;
2163 struct btrfs_file_extent_item *fi;
2164 struct btrfs_path *path;
2165 struct extent_buffer *leaf;
2166 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002167 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002168 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002169 int ret;
2170
2171 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002172 if (!path)
2173 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002174
Chris Masona1ed8352009-09-11 12:27:37 -04002175 /*
2176 * we may be replacing one extent in the tree with another.
2177 * The new extent is pinned in the extent map, and we don't want
2178 * to drop it from the cache until it is completely in the btree.
2179 *
2180 * So, tell btrfs_drop_extents to leave this extent in the cache.
2181 * the caller is expected to unpin it and allow it to be merged
2182 * with the others.
2183 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002184 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2185 file_pos + num_bytes, NULL, 0,
2186 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002187 if (ret)
2188 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002189
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002190 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002191 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002192 ins.offset = file_pos;
2193 ins.type = BTRFS_EXTENT_DATA_KEY;
2194
2195 path->leave_spinning = 1;
2196 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2197 sizeof(*fi));
2198 if (ret)
2199 goto out;
2200 }
Yan Zhengd899e052008-10-30 14:25:28 -04002201 leaf = path->nodes[0];
2202 fi = btrfs_item_ptr(leaf, path->slots[0],
2203 struct btrfs_file_extent_item);
2204 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2205 btrfs_set_file_extent_type(leaf, fi, extent_type);
2206 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2207 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2208 btrfs_set_file_extent_offset(leaf, fi, 0);
2209 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2210 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2211 btrfs_set_file_extent_compression(leaf, fi, compression);
2212 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2213 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002214
Yan Zhengd899e052008-10-30 14:25:28 -04002215 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002216 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002217
2218 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002219
2220 ins.objectid = disk_bytenr;
2221 ins.offset = disk_num_bytes;
2222 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002223
Qu Wenruo297d7502015-09-08 17:08:37 +08002224 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002225 * Release the reserved range from inode dirty range map, as it is
2226 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002227 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002228 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2229 if (ret < 0)
2230 goto out;
2231 qg_released = ret;
2232 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
2233 btrfs_ino(BTRFS_I(inode)), file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002234out:
Yan Zhengd899e052008-10-30 14:25:28 -04002235 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002236
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002237 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002238}
2239
Liu Bo38c227d2013-01-29 03:18:40 +00002240/* snapshot-aware defrag */
2241struct sa_defrag_extent_backref {
2242 struct rb_node node;
2243 struct old_sa_defrag_extent *old;
2244 u64 root_id;
2245 u64 inum;
2246 u64 file_pos;
2247 u64 extent_offset;
2248 u64 num_bytes;
2249 u64 generation;
2250};
2251
2252struct old_sa_defrag_extent {
2253 struct list_head list;
2254 struct new_sa_defrag_extent *new;
2255
2256 u64 extent_offset;
2257 u64 bytenr;
2258 u64 offset;
2259 u64 len;
2260 int count;
2261};
2262
2263struct new_sa_defrag_extent {
2264 struct rb_root root;
2265 struct list_head head;
2266 struct btrfs_path *path;
2267 struct inode *inode;
2268 u64 file_pos;
2269 u64 len;
2270 u64 bytenr;
2271 u64 disk_len;
2272 u8 compress_type;
2273};
2274
2275static int backref_comp(struct sa_defrag_extent_backref *b1,
2276 struct sa_defrag_extent_backref *b2)
2277{
2278 if (b1->root_id < b2->root_id)
2279 return -1;
2280 else if (b1->root_id > b2->root_id)
2281 return 1;
2282
2283 if (b1->inum < b2->inum)
2284 return -1;
2285 else if (b1->inum > b2->inum)
2286 return 1;
2287
2288 if (b1->file_pos < b2->file_pos)
2289 return -1;
2290 else if (b1->file_pos > b2->file_pos)
2291 return 1;
2292
2293 /*
2294 * [------------------------------] ===> (a range of space)
2295 * |<--->| |<---->| =============> (fs/file tree A)
2296 * |<---------------------------->| ===> (fs/file tree B)
2297 *
2298 * A range of space can refer to two file extents in one tree while
2299 * refer to only one file extent in another tree.
2300 *
2301 * So we may process a disk offset more than one time(two extents in A)
2302 * and locate at the same extent(one extent in B), then insert two same
2303 * backrefs(both refer to the extent in B).
2304 */
2305 return 0;
2306}
2307
2308static void backref_insert(struct rb_root *root,
2309 struct sa_defrag_extent_backref *backref)
2310{
2311 struct rb_node **p = &root->rb_node;
2312 struct rb_node *parent = NULL;
2313 struct sa_defrag_extent_backref *entry;
2314 int ret;
2315
2316 while (*p) {
2317 parent = *p;
2318 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2319
2320 ret = backref_comp(backref, entry);
2321 if (ret < 0)
2322 p = &(*p)->rb_left;
2323 else
2324 p = &(*p)->rb_right;
2325 }
2326
2327 rb_link_node(&backref->node, parent, p);
2328 rb_insert_color(&backref->node, root);
2329}
2330
2331/*
2332 * Note the backref might has changed, and in this case we just return 0.
2333 */
2334static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2335 void *ctx)
2336{
2337 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002338 struct old_sa_defrag_extent *old = ctx;
2339 struct new_sa_defrag_extent *new = old->new;
2340 struct btrfs_path *path = new->path;
2341 struct btrfs_key key;
2342 struct btrfs_root *root;
2343 struct sa_defrag_extent_backref *backref;
2344 struct extent_buffer *leaf;
2345 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002346 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002347 int slot;
2348 int ret;
2349 u64 extent_offset;
2350 u64 num_bytes;
2351
2352 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002353 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002354 return 0;
2355
2356 key.objectid = root_id;
2357 key.type = BTRFS_ROOT_ITEM_KEY;
2358 key.offset = (u64)-1;
2359
Liu Bo38c227d2013-01-29 03:18:40 +00002360 root = btrfs_read_fs_root_no_name(fs_info, &key);
2361 if (IS_ERR(root)) {
2362 if (PTR_ERR(root) == -ENOENT)
2363 return 0;
2364 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002365 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002366 inum, offset, root_id);
2367 return PTR_ERR(root);
2368 }
2369
2370 key.objectid = inum;
2371 key.type = BTRFS_EXTENT_DATA_KEY;
2372 if (offset > (u64)-1 << 32)
2373 key.offset = 0;
2374 else
2375 key.offset = offset;
2376
2377 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302378 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002379 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002380 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002381
2382 while (1) {
2383 cond_resched();
2384
2385 leaf = path->nodes[0];
2386 slot = path->slots[0];
2387
2388 if (slot >= btrfs_header_nritems(leaf)) {
2389 ret = btrfs_next_leaf(root, path);
2390 if (ret < 0) {
2391 goto out;
2392 } else if (ret > 0) {
2393 ret = 0;
2394 goto out;
2395 }
2396 continue;
2397 }
2398
2399 path->slots[0]++;
2400
2401 btrfs_item_key_to_cpu(leaf, &key, slot);
2402
2403 if (key.objectid > inum)
2404 goto out;
2405
2406 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2407 continue;
2408
2409 extent = btrfs_item_ptr(leaf, slot,
2410 struct btrfs_file_extent_item);
2411
2412 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2413 continue;
2414
Liu Boe68afa42013-07-01 22:13:26 +08002415 /*
2416 * 'offset' refers to the exact key.offset,
2417 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2418 * (key.offset - extent_offset).
2419 */
2420 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002421 continue;
2422
Liu Boe68afa42013-07-01 22:13:26 +08002423 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002424 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002425
Liu Bo38c227d2013-01-29 03:18:40 +00002426 if (extent_offset >= old->extent_offset + old->offset +
2427 old->len || extent_offset + num_bytes <=
2428 old->extent_offset + old->offset)
2429 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002430 break;
2431 }
2432
2433 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2434 if (!backref) {
2435 ret = -ENOENT;
2436 goto out;
2437 }
2438
2439 backref->root_id = root_id;
2440 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002441 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002442 backref->num_bytes = num_bytes;
2443 backref->extent_offset = extent_offset;
2444 backref->generation = btrfs_file_extent_generation(leaf, extent);
2445 backref->old = old;
2446 backref_insert(&new->root, backref);
2447 old->count++;
2448out:
2449 btrfs_release_path(path);
2450 WARN_ON(ret);
2451 return ret;
2452}
2453
2454static noinline bool record_extent_backrefs(struct btrfs_path *path,
2455 struct new_sa_defrag_extent *new)
2456{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002457 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002458 struct old_sa_defrag_extent *old, *tmp;
2459 int ret;
2460
2461 new->path = path;
2462
2463 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002464 ret = iterate_inodes_from_logical(old->bytenr +
2465 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002466 path, record_one_backref,
2467 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002468 if (ret < 0 && ret != -ENOENT)
2469 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002470
2471 /* no backref to be processed for this extent */
2472 if (!old->count) {
2473 list_del(&old->list);
2474 kfree(old);
2475 }
2476 }
2477
2478 if (list_empty(&new->head))
2479 return false;
2480
2481 return true;
2482}
2483
2484static int relink_is_mergable(struct extent_buffer *leaf,
2485 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002486 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002487{
Liu Bo116e0022013-08-02 16:30:40 +08002488 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002489 return 0;
2490
2491 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2492 return 0;
2493
Liu Bo116e0022013-08-02 16:30:40 +08002494 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2495 return 0;
2496
2497 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002498 btrfs_file_extent_other_encoding(leaf, fi))
2499 return 0;
2500
2501 return 1;
2502}
2503
2504/*
2505 * Note the backref might has changed, and in this case we just return 0.
2506 */
2507static noinline int relink_extent_backref(struct btrfs_path *path,
2508 struct sa_defrag_extent_backref *prev,
2509 struct sa_defrag_extent_backref *backref)
2510{
2511 struct btrfs_file_extent_item *extent;
2512 struct btrfs_file_extent_item *item;
2513 struct btrfs_ordered_extent *ordered;
2514 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002515 struct btrfs_root *root;
2516 struct btrfs_key key;
2517 struct extent_buffer *leaf;
2518 struct old_sa_defrag_extent *old = backref->old;
2519 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002520 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002521 struct inode *inode;
2522 struct extent_state *cached = NULL;
2523 int ret = 0;
2524 u64 start;
2525 u64 len;
2526 u64 lock_start;
2527 u64 lock_end;
2528 bool merge = false;
2529 int index;
2530
2531 if (prev && prev->root_id == backref->root_id &&
2532 prev->inum == backref->inum &&
2533 prev->file_pos + prev->num_bytes == backref->file_pos)
2534 merge = true;
2535
2536 /* step 1: get root */
2537 key.objectid = backref->root_id;
2538 key.type = BTRFS_ROOT_ITEM_KEY;
2539 key.offset = (u64)-1;
2540
Liu Bo38c227d2013-01-29 03:18:40 +00002541 index = srcu_read_lock(&fs_info->subvol_srcu);
2542
2543 root = btrfs_read_fs_root_no_name(fs_info, &key);
2544 if (IS_ERR(root)) {
2545 srcu_read_unlock(&fs_info->subvol_srcu, index);
2546 if (PTR_ERR(root) == -ENOENT)
2547 return 0;
2548 return PTR_ERR(root);
2549 }
Liu Bo38c227d2013-01-29 03:18:40 +00002550
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002551 if (btrfs_root_readonly(root)) {
2552 srcu_read_unlock(&fs_info->subvol_srcu, index);
2553 return 0;
2554 }
2555
Liu Bo38c227d2013-01-29 03:18:40 +00002556 /* step 2: get inode */
2557 key.objectid = backref->inum;
2558 key.type = BTRFS_INODE_ITEM_KEY;
2559 key.offset = 0;
2560
2561 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2562 if (IS_ERR(inode)) {
2563 srcu_read_unlock(&fs_info->subvol_srcu, index);
2564 return 0;
2565 }
2566
2567 srcu_read_unlock(&fs_info->subvol_srcu, index);
2568
2569 /* step 3: relink backref */
2570 lock_start = backref->file_pos;
2571 lock_end = backref->file_pos + backref->num_bytes - 1;
2572 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002573 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002574
2575 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2576 if (ordered) {
2577 btrfs_put_ordered_extent(ordered);
2578 goto out_unlock;
2579 }
2580
2581 trans = btrfs_join_transaction(root);
2582 if (IS_ERR(trans)) {
2583 ret = PTR_ERR(trans);
2584 goto out_unlock;
2585 }
2586
2587 key.objectid = backref->inum;
2588 key.type = BTRFS_EXTENT_DATA_KEY;
2589 key.offset = backref->file_pos;
2590
2591 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2592 if (ret < 0) {
2593 goto out_free_path;
2594 } else if (ret > 0) {
2595 ret = 0;
2596 goto out_free_path;
2597 }
2598
2599 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2600 struct btrfs_file_extent_item);
2601
2602 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2603 backref->generation)
2604 goto out_free_path;
2605
2606 btrfs_release_path(path);
2607
2608 start = backref->file_pos;
2609 if (backref->extent_offset < old->extent_offset + old->offset)
2610 start += old->extent_offset + old->offset -
2611 backref->extent_offset;
2612
2613 len = min(backref->extent_offset + backref->num_bytes,
2614 old->extent_offset + old->offset + old->len);
2615 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2616
2617 ret = btrfs_drop_extents(trans, root, inode, start,
2618 start + len, 1);
2619 if (ret)
2620 goto out_free_path;
2621again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002622 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002623 key.type = BTRFS_EXTENT_DATA_KEY;
2624 key.offset = start;
2625
Liu Boa09a0a72013-03-11 09:20:58 +00002626 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002627 if (merge) {
2628 struct btrfs_file_extent_item *fi;
2629 u64 extent_len;
2630 struct btrfs_key found_key;
2631
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002632 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002633 if (ret < 0)
2634 goto out_free_path;
2635
2636 path->slots[0]--;
2637 leaf = path->nodes[0];
2638 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2639
2640 fi = btrfs_item_ptr(leaf, path->slots[0],
2641 struct btrfs_file_extent_item);
2642 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2643
Liu Bo116e0022013-08-02 16:30:40 +08002644 if (extent_len + found_key.offset == start &&
2645 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002646 btrfs_set_file_extent_num_bytes(leaf, fi,
2647 extent_len + len);
2648 btrfs_mark_buffer_dirty(leaf);
2649 inode_add_bytes(inode, len);
2650
2651 ret = 1;
2652 goto out_free_path;
2653 } else {
2654 merge = false;
2655 btrfs_release_path(path);
2656 goto again;
2657 }
2658 }
2659
2660 ret = btrfs_insert_empty_item(trans, root, path, &key,
2661 sizeof(*extent));
2662 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002663 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002664 goto out_free_path;
2665 }
2666
2667 leaf = path->nodes[0];
2668 item = btrfs_item_ptr(leaf, path->slots[0],
2669 struct btrfs_file_extent_item);
2670 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2671 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2672 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2673 btrfs_set_file_extent_num_bytes(leaf, item, len);
2674 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2675 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2676 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2677 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2678 btrfs_set_file_extent_encryption(leaf, item, 0);
2679 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2680
2681 btrfs_mark_buffer_dirty(leaf);
2682 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002683 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002684
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002685 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002686 new->disk_len, 0,
2687 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002688 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002689 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002690 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002691 goto out_free_path;
2692 }
2693
2694 ret = 1;
2695out_free_path:
2696 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002697 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002698 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002699out_unlock:
2700 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2701 &cached, GFP_NOFS);
2702 iput(inode);
2703 return ret;
2704}
2705
Liu Bo6f519562013-10-29 10:45:05 +08002706static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2707{
2708 struct old_sa_defrag_extent *old, *tmp;
2709
2710 if (!new)
2711 return;
2712
2713 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002714 kfree(old);
2715 }
2716 kfree(new);
2717}
2718
Liu Bo38c227d2013-01-29 03:18:40 +00002719static void relink_file_extents(struct new_sa_defrag_extent *new)
2720{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002721 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002722 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002723 struct sa_defrag_extent_backref *backref;
2724 struct sa_defrag_extent_backref *prev = NULL;
2725 struct inode *inode;
2726 struct btrfs_root *root;
2727 struct rb_node *node;
2728 int ret;
2729
2730 inode = new->inode;
2731 root = BTRFS_I(inode)->root;
2732
2733 path = btrfs_alloc_path();
2734 if (!path)
2735 return;
2736
2737 if (!record_extent_backrefs(path, new)) {
2738 btrfs_free_path(path);
2739 goto out;
2740 }
2741 btrfs_release_path(path);
2742
2743 while (1) {
2744 node = rb_first(&new->root);
2745 if (!node)
2746 break;
2747 rb_erase(node, &new->root);
2748
2749 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2750
2751 ret = relink_extent_backref(path, prev, backref);
2752 WARN_ON(ret < 0);
2753
2754 kfree(prev);
2755
2756 if (ret == 1)
2757 prev = backref;
2758 else
2759 prev = NULL;
2760 cond_resched();
2761 }
2762 kfree(prev);
2763
2764 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002765out:
Liu Bo6f519562013-10-29 10:45:05 +08002766 free_sa_defrag_extent(new);
2767
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002768 atomic_dec(&fs_info->defrag_running);
2769 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002770}
2771
2772static struct new_sa_defrag_extent *
2773record_old_file_extents(struct inode *inode,
2774 struct btrfs_ordered_extent *ordered)
2775{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002776 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002777 struct btrfs_root *root = BTRFS_I(inode)->root;
2778 struct btrfs_path *path;
2779 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002780 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002781 struct new_sa_defrag_extent *new;
2782 int ret;
2783
2784 new = kmalloc(sizeof(*new), GFP_NOFS);
2785 if (!new)
2786 return NULL;
2787
2788 new->inode = inode;
2789 new->file_pos = ordered->file_offset;
2790 new->len = ordered->len;
2791 new->bytenr = ordered->start;
2792 new->disk_len = ordered->disk_len;
2793 new->compress_type = ordered->compress_type;
2794 new->root = RB_ROOT;
2795 INIT_LIST_HEAD(&new->head);
2796
2797 path = btrfs_alloc_path();
2798 if (!path)
2799 goto out_kfree;
2800
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002801 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002802 key.type = BTRFS_EXTENT_DATA_KEY;
2803 key.offset = new->file_pos;
2804
2805 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2806 if (ret < 0)
2807 goto out_free_path;
2808 if (ret > 0 && path->slots[0] > 0)
2809 path->slots[0]--;
2810
2811 /* find out all the old extents for the file range */
2812 while (1) {
2813 struct btrfs_file_extent_item *extent;
2814 struct extent_buffer *l;
2815 int slot;
2816 u64 num_bytes;
2817 u64 offset;
2818 u64 end;
2819 u64 disk_bytenr;
2820 u64 extent_offset;
2821
2822 l = path->nodes[0];
2823 slot = path->slots[0];
2824
2825 if (slot >= btrfs_header_nritems(l)) {
2826 ret = btrfs_next_leaf(root, path);
2827 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002828 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002829 else if (ret > 0)
2830 break;
2831 continue;
2832 }
2833
2834 btrfs_item_key_to_cpu(l, &key, slot);
2835
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002836 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002837 break;
2838 if (key.type != BTRFS_EXTENT_DATA_KEY)
2839 break;
2840 if (key.offset >= new->file_pos + new->len)
2841 break;
2842
2843 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2844
2845 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2846 if (key.offset + num_bytes < new->file_pos)
2847 goto next;
2848
2849 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2850 if (!disk_bytenr)
2851 goto next;
2852
2853 extent_offset = btrfs_file_extent_offset(l, extent);
2854
2855 old = kmalloc(sizeof(*old), GFP_NOFS);
2856 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002857 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002858
2859 offset = max(new->file_pos, key.offset);
2860 end = min(new->file_pos + new->len, key.offset + num_bytes);
2861
2862 old->bytenr = disk_bytenr;
2863 old->extent_offset = extent_offset;
2864 old->offset = offset - key.offset;
2865 old->len = end - offset;
2866 old->new = new;
2867 old->count = 0;
2868 list_add_tail(&old->list, &new->head);
2869next:
2870 path->slots[0]++;
2871 cond_resched();
2872 }
2873
2874 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002875 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002876
2877 return new;
2878
Liu Bo38c227d2013-01-29 03:18:40 +00002879out_free_path:
2880 btrfs_free_path(path);
2881out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002882 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002883 return NULL;
2884}
2885
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002886static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002887 u64 start, u64 len)
2888{
2889 struct btrfs_block_group_cache *cache;
2890
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002891 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002892 ASSERT(cache);
2893
2894 spin_lock(&cache->lock);
2895 cache->delalloc_bytes -= len;
2896 spin_unlock(&cache->lock);
2897
2898 btrfs_put_block_group(cache);
2899}
2900
Chris Masond352ac62008-09-29 15:18:18 -04002901/* as ordered data IO finishes, this gets called so we can finish
2902 * an ordered extent if the range of bytes in the file it covers are
2903 * fully written.
2904 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002905static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002906{
Josef Bacik5fd02042012-05-02 14:00:54 -04002907 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002908 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002909 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002910 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002911 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002912 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002913 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002914 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002915 int ret = 0;
2916 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002917 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002918 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002919 bool range_locked = false;
2920 bool clear_new_delalloc_bytes = false;
2921
2922 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2923 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2924 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2925 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002926
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002927 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002928
Josef Bacik5fd02042012-05-02 14:00:54 -04002929 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2930 ret = -EIO;
2931 goto out;
2932 }
2933
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002934 btrfs_free_io_failure_record(BTRFS_I(inode),
2935 ordered_extent->file_offset,
2936 ordered_extent->file_offset +
2937 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002938
Josef Bacik77cef2e2013-08-29 13:57:21 -04002939 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2940 truncated = true;
2941 logical_len = ordered_extent->truncated_len;
2942 /* Truncated the entire extent, don't bother adding */
2943 if (!logical_len)
2944 goto out;
2945 }
2946
Yan, Zhengc2167752009-11-12 09:34:21 +00002947 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002948 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002949
2950 /*
2951 * For mwrite(mmap + memset to write) case, we still reserve
2952 * space for NOCOW range.
2953 * As NOCOW won't cause a new delayed ref, just free the space
2954 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002955 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002956 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002957 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2958 if (nolock)
2959 trans = btrfs_join_transaction_nolock(root);
2960 else
2961 trans = btrfs_join_transaction(root);
2962 if (IS_ERR(trans)) {
2963 ret = PTR_ERR(trans);
2964 trans = NULL;
2965 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002966 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002967 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002968 ret = btrfs_update_inode_fallback(trans, root, inode);
2969 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002970 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002971 goto out;
2972 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002973
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002974 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002975 lock_extent_bits(io_tree, ordered_extent->file_offset,
2976 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002977 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002978
Liu Bo38c227d2013-01-29 03:18:40 +00002979 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2980 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002981 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002982 if (ret) {
2983 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002984 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002985 /* the inode is shared */
2986 new = record_old_file_extents(inode, ordered_extent);
2987
2988 clear_extent_bit(io_tree, ordered_extent->file_offset,
2989 ordered_extent->file_offset + ordered_extent->len - 1,
2990 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2991 }
2992
Josef Bacik0cb59c92010-07-02 12:14:14 -04002993 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002994 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002995 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002996 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002997 if (IS_ERR(trans)) {
2998 ret = PTR_ERR(trans);
2999 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003000 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003001 }
Chris Masona79b7d42014-05-22 16:18:52 -07003002
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003003 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003004
Chris Masonc8b97812008-10-29 14:49:59 -04003005 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003006 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003007 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003008 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003009 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003010 ordered_extent->file_offset,
3011 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003012 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003013 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003014 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003015 ret = insert_reserved_file_extent(trans, inode,
3016 ordered_extent->file_offset,
3017 ordered_extent->start,
3018 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003019 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003020 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003021 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003022 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003023 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003024 ordered_extent->start,
3025 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003026 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003027 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3028 ordered_extent->file_offset, ordered_extent->len,
3029 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003030 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003031 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003032 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003033 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003034
David Sterbadf9f6282017-02-10 19:35:37 +01003035 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003036
Josef Bacik6c760c02012-11-09 10:53:21 -05003037 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3038 ret = btrfs_update_inode_fallback(trans, root, inode);
3039 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003040 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003041 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003042 }
3043 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003044out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003045 if (range_locked || clear_new_delalloc_bytes) {
3046 unsigned int clear_bits = 0;
3047
3048 if (range_locked)
3049 clear_bits |= EXTENT_LOCKED;
3050 if (clear_new_delalloc_bytes)
3051 clear_bits |= EXTENT_DELALLOC_NEW;
3052 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3053 ordered_extent->file_offset,
3054 ordered_extent->file_offset +
3055 ordered_extent->len - 1,
3056 clear_bits,
3057 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
3058 0, &cached_state, GFP_NOFS);
3059 }
3060
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003061 if (root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02003062 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3063 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003064 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003065 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003066
Josef Bacik77cef2e2013-08-29 13:57:21 -04003067 if (ret || truncated) {
3068 u64 start, end;
3069
3070 if (truncated)
3071 start = ordered_extent->file_offset + logical_len;
3072 else
3073 start = ordered_extent->file_offset;
3074 end = ordered_extent->file_offset + ordered_extent->len - 1;
3075 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3076
3077 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003078 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003079
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003080 /*
3081 * If the ordered extent had an IOERR or something else went
3082 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003083 * back to the allocator. We only free the extent in the
3084 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003085 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003086 if ((ret || !logical_len) &&
3087 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003088 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003089 btrfs_free_reserved_extent(fs_info,
3090 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003091 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003092 }
3093
3094
Josef Bacik5fd02042012-05-02 14:00:54 -04003095 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003096 * This needs to be done to make sure anybody waiting knows we are done
3097 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003098 */
3099 btrfs_remove_ordered_extent(inode, ordered_extent);
3100
Liu Bo38c227d2013-01-29 03:18:40 +00003101 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003102 if (new) {
3103 if (ret) {
3104 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003105 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003106 } else {
3107 relink_file_extents(new);
3108 }
3109 }
Liu Bo38c227d2013-01-29 03:18:40 +00003110
Chris Masone6dcd2d2008-07-17 12:53:50 -04003111 /* once for us */
3112 btrfs_put_ordered_extent(ordered_extent);
3113 /* once for the tree */
3114 btrfs_put_ordered_extent(ordered_extent);
3115
Josef Bacik5fd02042012-05-02 14:00:54 -04003116 return ret;
3117}
3118
3119static void finish_ordered_fn(struct btrfs_work *work)
3120{
3121 struct btrfs_ordered_extent *ordered_extent;
3122 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3123 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003124}
3125
David Sterbac3988d62017-02-17 15:18:32 +01003126static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003127 struct extent_state *state, int uptodate)
3128{
Josef Bacik5fd02042012-05-02 14:00:54 -04003129 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003130 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003131 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003132 struct btrfs_workqueue *wq;
3133 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003134
liubo1abe9b82011-03-24 11:18:59 +00003135 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3136
Chris Mason8b62b722009-09-02 16:53:46 -04003137 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003138 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3139 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003140 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003141
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003142 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003143 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003144 func = btrfs_freespace_write_helper;
3145 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003146 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003147 func = btrfs_endio_write_helper;
3148 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003149
Liu Bo9e0af232014-08-15 23:36:53 +08003150 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3151 NULL);
3152 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003153}
3154
Miao Xiedc380ae2014-09-12 18:43:55 +08003155static int __readpage_endio_check(struct inode *inode,
3156 struct btrfs_io_bio *io_bio,
3157 int icsum, struct page *page,
3158 int pgoff, u64 start, size_t len)
3159{
3160 char *kaddr;
3161 u32 csum_expected;
3162 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003163
3164 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3165
3166 kaddr = kmap_atomic(page);
3167 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003168 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003169 if (csum != csum_expected)
3170 goto zeroit;
3171
3172 kunmap_atomic(kaddr);
3173 return 0;
3174zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003175 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003176 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003177 memset(kaddr + pgoff, 1, len);
3178 flush_dcache_page(page);
3179 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003180 return -EIO;
3181}
3182
Chris Masond352ac62008-09-29 15:18:18 -04003183/*
Chris Masond352ac62008-09-29 15:18:18 -04003184 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003185 * if there's a match, we allow the bio to finish. If not, the code in
3186 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003187 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003188static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3189 u64 phy_offset, struct page *page,
3190 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003191{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003192 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003193 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003194 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003195 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003196
Chris Masond20f7042008-12-08 16:58:54 -05003197 if (PageChecked(page)) {
3198 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003199 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003200 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003201
3202 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003203 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003204
Yan Zheng17d217f2008-12-12 10:03:38 -05003205 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003206 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003207 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003208 return 0;
3209 }
3210
Miao Xiefacc8a222013-07-25 19:22:34 +08003211 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003212 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3213 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003214}
Chris Masonb888db2b2007-08-27 16:49:44 -04003215
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003216void btrfs_add_delayed_iput(struct inode *inode)
3217{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003218 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003219 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003220
3221 if (atomic_add_unless(&inode->i_count, -1, 1))
3222 return;
3223
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003224 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003225 if (binode->delayed_iput_count == 0) {
3226 ASSERT(list_empty(&binode->delayed_iput));
3227 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3228 } else {
3229 binode->delayed_iput_count++;
3230 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003231 spin_unlock(&fs_info->delayed_iput_lock);
3232}
3233
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003234void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003235{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003236
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003237 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003238 while (!list_empty(&fs_info->delayed_iputs)) {
3239 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003240
David Sterba8089fe62015-11-19 14:15:51 +01003241 inode = list_first_entry(&fs_info->delayed_iputs,
3242 struct btrfs_inode, delayed_iput);
3243 if (inode->delayed_iput_count) {
3244 inode->delayed_iput_count--;
3245 list_move_tail(&inode->delayed_iput,
3246 &fs_info->delayed_iputs);
3247 } else {
3248 list_del_init(&inode->delayed_iput);
3249 }
3250 spin_unlock(&fs_info->delayed_iput_lock);
3251 iput(&inode->vfs_inode);
3252 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003253 }
David Sterba8089fe62015-11-19 14:15:51 +01003254 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003255}
3256
Yan, Zhengd68fc572010-05-16 10:49:58 -04003257/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003258 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003259 * files in the subvolume, it removes orphan item and frees block_rsv
3260 * structure.
3261 */
3262void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3263 struct btrfs_root *root)
3264{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003265 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003266 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003267 int ret;
3268
Josef Bacik8a35d952012-05-23 14:26:42 -04003269 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003270 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3271 return;
3272
Josef Bacik90290e12011-12-02 15:44:12 -05003273 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003274 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003275 spin_unlock(&root->orphan_lock);
3276 return;
3277 }
3278
3279 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3280 spin_unlock(&root->orphan_lock);
3281 return;
3282 }
3283
3284 block_rsv = root->orphan_block_rsv;
3285 root->orphan_block_rsv = NULL;
3286 spin_unlock(&root->orphan_lock);
3287
Miao Xie27cdeb72014-04-02 19:51:05 +08003288 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003289 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003290 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003291 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003292 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003293 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003294 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003295 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3296 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003297 }
3298
Josef Bacik90290e12011-12-02 15:44:12 -05003299 if (block_rsv) {
3300 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003301 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003302 }
3303}
3304
3305/*
Josef Bacik7b128762008-07-24 12:17:14 -04003306 * This creates an orphan entry for the given inode in case something goes
3307 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003308 *
3309 * NOTE: caller of this function should reserve 5 units of metadata for
3310 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003311 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003312int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3313 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003314{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003315 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3316 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003317 struct btrfs_block_rsv *block_rsv = NULL;
3318 int reserve = 0;
3319 int insert = 0;
3320 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003321
Yan, Zhengd68fc572010-05-16 10:49:58 -04003322 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003323 block_rsv = btrfs_alloc_block_rsv(fs_info,
3324 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003325 if (!block_rsv)
3326 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003327 }
3328
Yan, Zhengd68fc572010-05-16 10:49:58 -04003329 spin_lock(&root->orphan_lock);
3330 if (!root->orphan_block_rsv) {
3331 root->orphan_block_rsv = block_rsv;
3332 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003333 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003334 block_rsv = NULL;
3335 }
Josef Bacik7b128762008-07-24 12:17:14 -04003336
Josef Bacik8a35d952012-05-23 14:26:42 -04003337 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003338 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003339#if 0
3340 /*
3341 * For proper ENOSPC handling, we should do orphan
3342 * cleanup when mounting. But this introduces backward
3343 * compatibility issue.
3344 */
3345 if (!xchg(&root->orphan_item_inserted, 1))
3346 insert = 2;
3347 else
3348 insert = 1;
3349#endif
3350 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003351 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003352 }
Josef Bacik7b128762008-07-24 12:17:14 -04003353
Josef Bacik72ac3c02012-05-23 14:13:11 -04003354 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003355 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003356 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003357 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003358
Yan, Zhengd68fc572010-05-16 10:49:58 -04003359 /* grab metadata reservation from transaction handle */
3360 if (reserve) {
3361 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003362 ASSERT(!ret);
3363 if (ret) {
3364 atomic_dec(&root->orphan_inodes);
3365 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003366 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003367 if (insert)
3368 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003369 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003370 return ret;
3371 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003372 }
3373
3374 /* insert an orphan item to track this unlinked/truncated file */
3375 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003376 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003377 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003378 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003379 if (reserve) {
3380 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003381 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003382 btrfs_orphan_release_metadata(inode);
3383 }
3384 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003385 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003386 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003387 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003388 return ret;
3389 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003390 }
3391 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003392 }
3393
3394 /* insert an orphan item to track subvolume contains orphan files */
3395 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003396 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003397 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003398 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003399 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003400 return ret;
3401 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003402 }
3403 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003404}
3405
3406/*
3407 * We have done the truncate/delete so we can go ahead and remove the orphan
3408 * item for this particular inode.
3409 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003410static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003411 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003412{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003413 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003414 int delete_item = 0;
3415 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003416 int ret = 0;
3417
Yan, Zhengd68fc572010-05-16 10:49:58 -04003418 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003419 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003420 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003421 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003422
Josef Bacik72ac3c02012-05-23 14:13:11 -04003423 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003424 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003425 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003426 spin_unlock(&root->orphan_lock);
3427
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003428 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003429 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003430 if (trans)
3431 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003432 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003433 }
Josef Bacik7b128762008-07-24 12:17:14 -04003434
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003435 if (release_rsv)
3436 btrfs_orphan_release_metadata(inode);
3437
Josef Bacik4ef31a42013-08-13 14:10:08 -04003438 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003439}
3440
3441/*
3442 * this cleans up any orphans that may be left on the list from the last use
3443 * of this root.
3444 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003445int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003446{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003447 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003448 struct btrfs_path *path;
3449 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003450 struct btrfs_key key, found_key;
3451 struct btrfs_trans_handle *trans;
3452 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003453 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003454 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3455
Yan, Zhengd68fc572010-05-16 10:49:58 -04003456 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003457 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003458
3459 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003460 if (!path) {
3461 ret = -ENOMEM;
3462 goto out;
3463 }
David Sterbae4058b52015-11-27 16:31:35 +01003464 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003465
3466 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003467 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003468 key.offset = (u64)-1;
3469
Josef Bacik7b128762008-07-24 12:17:14 -04003470 while (1) {
3471 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003472 if (ret < 0)
3473 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003474
3475 /*
3476 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003477 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003478 * find the key and see if we have stuff that matches
3479 */
3480 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003481 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003482 if (path->slots[0] == 0)
3483 break;
3484 path->slots[0]--;
3485 }
3486
3487 /* pull out the item */
3488 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003489 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3490
3491 /* make sure the item matches what we want */
3492 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3493 break;
David Sterba962a2982014-06-04 18:41:45 +02003494 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003495 break;
3496
3497 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003498 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003499
3500 /*
3501 * this is where we are basically btrfs_lookup, without the
3502 * crossing root thing. we store the inode number in the
3503 * offset of the orphan item.
3504 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003505
3506 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003507 btrfs_err(fs_info,
3508 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003509 ret = -EINVAL;
3510 goto out;
3511 }
3512
3513 last_objectid = found_key.offset;
3514
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003515 found_key.objectid = found_key.offset;
3516 found_key.type = BTRFS_INODE_ITEM_KEY;
3517 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003518 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303519 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003520 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003521 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003522
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003523 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003524 struct btrfs_root *dead_root;
3525 struct btrfs_fs_info *fs_info = root->fs_info;
3526 int is_dead_root = 0;
3527
3528 /*
3529 * this is an orphan in the tree root. Currently these
3530 * could come from 2 sources:
3531 * a) a snapshot deletion in progress
3532 * b) a free space cache inode
3533 * We need to distinguish those two, as the snapshot
3534 * orphan must not get deleted.
3535 * find_dead_roots already ran before us, so if this
3536 * is a snapshot deletion, we should find the root
3537 * in the dead_roots list
3538 */
3539 spin_lock(&fs_info->trans_lock);
3540 list_for_each_entry(dead_root, &fs_info->dead_roots,
3541 root_list) {
3542 if (dead_root->root_key.objectid ==
3543 found_key.objectid) {
3544 is_dead_root = 1;
3545 break;
3546 }
3547 }
3548 spin_unlock(&fs_info->trans_lock);
3549 if (is_dead_root) {
3550 /* prevent this orphan from being found again */
3551 key.offset = found_key.objectid - 1;
3552 continue;
3553 }
3554 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003555 /*
3556 * Inode is already gone but the orphan item is still there,
3557 * kill the orphan item.
3558 */
Filipe Manana67710892016-06-06 11:51:25 +01003559 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003560 trans = btrfs_start_transaction(root, 1);
3561 if (IS_ERR(trans)) {
3562 ret = PTR_ERR(trans);
3563 goto out;
3564 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003565 btrfs_debug(fs_info, "auto deleting %Lu",
3566 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003567 ret = btrfs_del_orphan_item(trans, root,
3568 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003569 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003570 if (ret)
3571 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003572 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003573 }
Josef Bacik7b128762008-07-24 12:17:14 -04003574
Josef Bacik7b128762008-07-24 12:17:14 -04003575 /*
3576 * add this inode to the orphan list so btrfs_orphan_del does
3577 * the proper thing when we hit it
3578 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003579 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3580 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003581 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003582
Josef Bacik7b128762008-07-24 12:17:14 -04003583 /* if we have links, this was a truncate, lets do that */
3584 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303585 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003586 iput(inode);
3587 continue;
3588 }
Josef Bacik7b128762008-07-24 12:17:14 -04003589 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003590
3591 /* 1 for the orphan item deletion. */
3592 trans = btrfs_start_transaction(root, 1);
3593 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003594 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003595 ret = PTR_ERR(trans);
3596 goto out;
3597 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003598 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003599 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003600 if (ret) {
3601 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003602 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003603 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003604
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003605 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003606 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003607 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003608 } else {
3609 nr_unlink++;
3610 }
3611
3612 /* this will do delete_inode and everything for us */
3613 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003614 if (ret)
3615 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003616 }
Miao Xie3254c872011-11-10 20:45:05 -05003617 /* release the path since we're done with it */
3618 btrfs_release_path(path);
3619
Yan, Zhengd68fc572010-05-16 10:49:58 -04003620 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3621
3622 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003623 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003624 (u64)-1);
3625
Miao Xie27cdeb72014-04-02 19:51:05 +08003626 if (root->orphan_block_rsv ||
3627 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003628 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003629 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003630 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003631 }
Josef Bacik7b128762008-07-24 12:17:14 -04003632
3633 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003634 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003635 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003636 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003637
3638out:
3639 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003640 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003641 btrfs_free_path(path);
3642 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003643}
3644
Chris Masond352ac62008-09-29 15:18:18 -04003645/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003646 * very simple check to peek ahead in the leaf looking for xattrs. If we
3647 * don't find any xattrs, we know there can't be any acls.
3648 *
3649 * slot is the slot the inode is in, objectid is the objectid of the inode
3650 */
3651static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003652 int slot, u64 objectid,
3653 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003654{
3655 u32 nritems = btrfs_header_nritems(leaf);
3656 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003657 static u64 xattr_access = 0;
3658 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003659 int scanned = 0;
3660
Josef Bacikf23b5a52013-06-19 10:16:26 -04003661 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003662 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3663 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3664 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3665 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003666 }
3667
Chris Mason46a53cc2009-04-27 11:47:50 -04003668 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003669 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003670 while (slot < nritems) {
3671 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3672
3673 /* we found a different objectid, there must not be acls */
3674 if (found_key.objectid != objectid)
3675 return 0;
3676
3677 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003678 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003679 if (*first_xattr_slot == -1)
3680 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003681 if (found_key.offset == xattr_access ||
3682 found_key.offset == xattr_default)
3683 return 1;
3684 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003685
3686 /*
3687 * we found a key greater than an xattr key, there can't
3688 * be any acls later on
3689 */
3690 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3691 return 0;
3692
3693 slot++;
3694 scanned++;
3695
3696 /*
3697 * it goes inode, inode backrefs, xattrs, extents,
3698 * so if there are a ton of hard links to an inode there can
3699 * be a lot of backrefs. Don't waste time searching too hard,
3700 * this is just an optimization
3701 */
3702 if (scanned >= 8)
3703 break;
3704 }
3705 /* we hit the end of the leaf before we found an xattr or
3706 * something larger than an xattr. We have to assume the inode
3707 * has acls
3708 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003709 if (*first_xattr_slot == -1)
3710 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003711 return 1;
3712}
3713
3714/*
Chris Masond352ac62008-09-29 15:18:18 -04003715 * read an inode from the btree into the in-memory inode
3716 */
Filipe Manana67710892016-06-06 11:51:25 +01003717static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003718{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003719 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003720 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003721 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003722 struct btrfs_inode_item *inode_item;
3723 struct btrfs_root *root = BTRFS_I(inode)->root;
3724 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003725 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003726 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003727 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003728 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003729 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003730 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003731
3732 ret = btrfs_fill_inode(inode, &rdev);
3733 if (!ret)
3734 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003735
3736 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003737 if (!path) {
3738 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003739 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003740 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003741
Chris Mason39279cc2007-06-12 06:35:45 -04003742 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003743
Chris Mason39279cc2007-06-12 06:35:45 -04003744 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003745 if (ret) {
3746 if (ret > 0)
3747 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003748 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003749 }
Chris Mason39279cc2007-06-12 06:35:45 -04003750
Chris Mason5f39d392007-10-15 16:14:19 -04003751 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003752
3753 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003754 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003755
Chris Mason5f39d392007-10-15 16:14:19 -04003756 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3757 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003758 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003759 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003760 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3761 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003762 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003763
David Sterbaa937b972014-12-12 17:39:12 +01003764 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3765 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003766
David Sterbaa937b972014-12-12 17:39:12 +01003767 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3768 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003769
David Sterbaa937b972014-12-12 17:39:12 +01003770 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3771 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003772
chandan r9cc97d62012-07-04 12:48:07 +05303773 BTRFS_I(inode)->i_otime.tv_sec =
3774 btrfs_timespec_sec(leaf, &inode_item->otime);
3775 BTRFS_I(inode)->i_otime.tv_nsec =
3776 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003777
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003778 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003779 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003780 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3781
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003782 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003783 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003784 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003785 rdev = btrfs_inode_rdev(leaf, inode_item);
3786
Josef Bacikaec74772008-07-24 12:12:38 -04003787 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003788 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003789
3790cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003791 /*
3792 * If we were modified in the current generation and evicted from memory
3793 * and then re-read we need to do a full sync since we don't have any
3794 * idea about which extents were modified before we were evicted from
3795 * cache.
3796 *
3797 * This is required for both inode re-read from disk and delayed inode
3798 * in delayed_nodes_tree.
3799 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003800 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003801 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3802 &BTRFS_I(inode)->runtime_flags);
3803
Filipe Mananabde6c242015-07-24 00:00:19 +01003804 /*
3805 * We don't persist the id of the transaction where an unlink operation
3806 * against the inode was last made. So here we assume the inode might
3807 * have been evicted, and therefore the exact value of last_unlink_trans
3808 * lost, and set it to last_trans to avoid metadata inconsistencies
3809 * between the inode and its parent if the inode is fsync'ed and the log
3810 * replayed. For example, in the scenario:
3811 *
3812 * touch mydir/foo
3813 * ln mydir/foo mydir/bar
3814 * sync
3815 * unlink mydir/bar
3816 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3817 * xfs_io -c fsync mydir/foo
3818 * <power failure>
3819 * mount fs, triggers fsync log replay
3820 *
3821 * We must make sure that when we fsync our inode foo we also log its
3822 * parent inode, otherwise after log replay the parent still has the
3823 * dentry with the "bar" name but our inode foo has a link count of 1
3824 * and doesn't have an inode ref with the name "bar" anymore.
3825 *
3826 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003827 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003828 * transaction commits on fsync if our inode is a directory, or if our
3829 * inode is not a directory, logging its parent unnecessarily.
3830 */
3831 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3832
Miao Xie67de1172013-12-26 13:07:06 +08003833 path->slots[0]++;
3834 if (inode->i_nlink != 1 ||
3835 path->slots[0] >= btrfs_header_nritems(leaf))
3836 goto cache_acl;
3837
3838 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003839 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003840 goto cache_acl;
3841
3842 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3843 if (location.type == BTRFS_INODE_REF_KEY) {
3844 struct btrfs_inode_ref *ref;
3845
3846 ref = (struct btrfs_inode_ref *)ptr;
3847 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3848 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3849 struct btrfs_inode_extref *extref;
3850
3851 extref = (struct btrfs_inode_extref *)ptr;
3852 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3853 extref);
3854 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003855cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003856 /*
3857 * try to precache a NULL acl entry for files that don't have
3858 * any xattrs or acls
3859 */
Li Zefan33345d012011-04-20 10:31:50 +08003860 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003861 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003862 if (first_xattr_slot != -1) {
3863 path->slots[0] = first_xattr_slot;
3864 ret = btrfs_load_inode_props(inode, path);
3865 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003866 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003867 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003868 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003869 root->root_key.objectid, ret);
3870 }
3871 btrfs_free_path(path);
3872
Al Viro72c04902009-06-24 16:58:48 -04003873 if (!maybe_acls)
3874 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003875
Chris Mason39279cc2007-06-12 06:35:45 -04003876 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003877 case S_IFREG:
3878 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003879 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003880 inode->i_fop = &btrfs_file_operations;
3881 inode->i_op = &btrfs_file_inode_operations;
3882 break;
3883 case S_IFDIR:
3884 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003885 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003886 break;
3887 case S_IFLNK:
3888 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003889 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003890 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3891 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003892 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003893 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003894 init_special_inode(inode, inode->i_mode, rdev);
3895 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003896 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003897
3898 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003899 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003900
3901make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003902 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003903 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003904 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003905}
3906
Chris Masond352ac62008-09-29 15:18:18 -04003907/*
3908 * given a leaf and an inode, copy the inode fields into the leaf
3909 */
Chris Masone02119d2008-09-05 16:13:11 -04003910static void fill_inode_item(struct btrfs_trans_handle *trans,
3911 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003912 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003913 struct inode *inode)
3914{
Liu Bo51fab692012-12-27 09:01:21 +00003915 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003916
Liu Bo51fab692012-12-27 09:01:21 +00003917 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003918
Liu Bo51fab692012-12-27 09:01:21 +00003919 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3920 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3921 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3922 &token);
3923 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3924 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003925
David Sterbaa937b972014-12-12 17:39:12 +01003926 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003927 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003928 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003929 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003930
David Sterbaa937b972014-12-12 17:39:12 +01003931 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003932 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003933 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003934 inode->i_mtime.tv_nsec, &token);
3935
David Sterbaa937b972014-12-12 17:39:12 +01003936 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003937 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003938 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003939 inode->i_ctime.tv_nsec, &token);
3940
chandan r9cc97d62012-07-04 12:48:07 +05303941 btrfs_set_token_timespec_sec(leaf, &item->otime,
3942 BTRFS_I(inode)->i_otime.tv_sec, &token);
3943 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3944 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3945
Liu Bo51fab692012-12-27 09:01:21 +00003946 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3947 &token);
3948 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3949 &token);
3950 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3951 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3952 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3953 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3954 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003955}
3956
Chris Masond352ac62008-09-29 15:18:18 -04003957/*
3958 * copy everything in the in-memory inode into the btree.
3959 */
Chris Mason21151332011-11-10 20:39:08 -05003960static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003961 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003962{
3963 struct btrfs_inode_item *inode_item;
3964 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003965 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003966 int ret;
3967
3968 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003969 if (!path)
3970 return -ENOMEM;
3971
Chris Masonb9473432009-03-13 11:00:37 -04003972 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003973 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3974 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003975 if (ret) {
3976 if (ret > 0)
3977 ret = -ENOENT;
3978 goto failed;
3979 }
3980
Chris Mason5f39d392007-10-15 16:14:19 -04003981 leaf = path->nodes[0];
3982 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003983 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003984
Chris Masone02119d2008-09-05 16:13:11 -04003985 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003986 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003987 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003988 ret = 0;
3989failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003990 btrfs_free_path(path);
3991 return ret;
3992}
3993
Chris Masond352ac62008-09-29 15:18:18 -04003994/*
Chris Mason21151332011-11-10 20:39:08 -05003995 * copy everything in the in-memory inode into the btree.
3996 */
3997noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3998 struct btrfs_root *root, struct inode *inode)
3999{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004000 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004001 int ret;
4002
4003 /*
4004 * If the inode is a free space inode, we can deadlock during commit
4005 * if we put it into the delayed code.
4006 *
4007 * The data relocation inode should also be directly updated
4008 * without delay
4009 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004010 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004011 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004012 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004013 btrfs_update_root_times(trans, root);
4014
Chris Mason21151332011-11-10 20:39:08 -05004015 ret = btrfs_delayed_update_inode(trans, root, inode);
4016 if (!ret)
4017 btrfs_set_inode_last_trans(trans, inode);
4018 return ret;
4019 }
4020
4021 return btrfs_update_inode_item(trans, root, inode);
4022}
4023
Josef Bacikbe6aef62012-10-22 15:43:12 -04004024noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4025 struct btrfs_root *root,
4026 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004027{
4028 int ret;
4029
4030 ret = btrfs_update_inode(trans, root, inode);
4031 if (ret == -ENOSPC)
4032 return btrfs_update_inode_item(trans, root, inode);
4033 return ret;
4034}
4035
4036/*
Chris Masond352ac62008-09-29 15:18:18 -04004037 * unlink helper that gets used here in inode.c and in the tree logging
4038 * recovery code. It remove a link in a directory with a given name, and
4039 * also drops the back refs in the inode to the directory
4040 */
Al Viro92986792011-03-04 17:14:37 +00004041static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4042 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004043 struct btrfs_inode *dir,
4044 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004045 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004046{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004047 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004048 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004049 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004050 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004051 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004052 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004053 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004054 u64 ino = btrfs_ino(inode);
4055 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004056
4057 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004058 if (!path) {
4059 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004060 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004061 }
4062
Chris Masonb9473432009-03-13 11:00:37 -04004063 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004064 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004065 name, name_len, -1);
4066 if (IS_ERR(di)) {
4067 ret = PTR_ERR(di);
4068 goto err;
4069 }
4070 if (!di) {
4071 ret = -ENOENT;
4072 goto err;
4073 }
Chris Mason5f39d392007-10-15 16:14:19 -04004074 leaf = path->nodes[0];
4075 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004076 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004077 if (ret)
4078 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004079 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004080
Miao Xie67de1172013-12-26 13:07:06 +08004081 /*
4082 * If we don't have dir index, we have to get it by looking up
4083 * the inode ref, since we get the inode ref, remove it directly,
4084 * it is unnecessary to do delayed deletion.
4085 *
4086 * But if we have dir index, needn't search inode ref to get it.
4087 * Since the inode ref is close to the inode item, it is better
4088 * that we delay to delete it, and just do this deletion when
4089 * we update the inode item.
4090 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004091 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004092 ret = btrfs_delayed_delete_inode_ref(inode);
4093 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004094 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004095 goto skip_backref;
4096 }
4097 }
4098
Li Zefan33345d012011-04-20 10:31:50 +08004099 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4100 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004101 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004102 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004103 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004104 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004105 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004106 goto err;
4107 }
Miao Xie67de1172013-12-26 13:07:06 +08004108skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004109 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004110 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004111 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004112 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004113 }
Chris Mason39279cc2007-06-12 06:35:45 -04004114
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004115 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4116 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004117 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004118 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004119 goto err;
4120 }
Chris Masone02119d2008-09-05 16:13:11 -04004121
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004122 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4123 index);
Chris Mason6418c962010-10-30 07:34:24 -04004124 if (ret == -ENOENT)
4125 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004126 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004127 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004128err:
4129 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004130 if (ret)
4131 goto out;
4132
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004133 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004134 inode_inc_iversion(&inode->vfs_inode);
4135 inode_inc_iversion(&dir->vfs_inode);
4136 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4137 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4138 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004139out:
Chris Mason39279cc2007-06-12 06:35:45 -04004140 return ret;
4141}
4142
Al Viro92986792011-03-04 17:14:37 +00004143int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4144 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004145 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004146 const char *name, int name_len)
4147{
4148 int ret;
4149 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4150 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004151 drop_nlink(&inode->vfs_inode);
4152 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004153 }
4154 return ret;
4155}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004156
4157/*
4158 * helper to start transaction for unlink and rmdir.
4159 *
Josef Bacikd52be812013-05-29 14:54:47 -04004160 * unlink and rmdir are special in btrfs, they do not always free space, so
4161 * if we cannot make our reservations the normal way try and see if there is
4162 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4163 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004164 */
Josef Bacikd52be812013-05-29 14:54:47 -04004165static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004166{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004167 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004168
Josef Bacike70bea52011-10-11 14:18:24 -04004169 /*
4170 * 1 for the possible orphan item
4171 * 1 for the dir item
4172 * 1 for the dir index
4173 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004174 * 1 for the inode
4175 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004176 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004177}
4178
Chris Mason39279cc2007-06-12 06:35:45 -04004179static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4180{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004181 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004182 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004183 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004184 int ret;
4185
Josef Bacikd52be812013-05-29 14:54:47 -04004186 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004187 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004188 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004189
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004190 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4191 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004192
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004193 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4194 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4195 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004196 if (ret)
4197 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004198
Yan, Zhenga22285a2010-05-16 10:48:46 -04004199 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004200 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004201 if (ret)
4202 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004203 }
Josef Bacik7b128762008-07-24 12:17:14 -04004204
Tsutomu Itohb5324022011-07-19 07:27:20 +00004205out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004206 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004207 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004208 return ret;
4209}
4210
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004211int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4212 struct btrfs_root *root,
4213 struct inode *dir, u64 objectid,
4214 const char *name, int name_len)
4215{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004216 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004217 struct btrfs_path *path;
4218 struct extent_buffer *leaf;
4219 struct btrfs_dir_item *di;
4220 struct btrfs_key key;
4221 u64 index;
4222 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004223 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004224
4225 path = btrfs_alloc_path();
4226 if (!path)
4227 return -ENOMEM;
4228
Li Zefan33345d012011-04-20 10:31:50 +08004229 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004230 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004231 if (IS_ERR_OR_NULL(di)) {
4232 if (!di)
4233 ret = -ENOENT;
4234 else
4235 ret = PTR_ERR(di);
4236 goto out;
4237 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004238
4239 leaf = path->nodes[0];
4240 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4241 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4242 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004243 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004244 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004245 goto out;
4246 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004247 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004248
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004249 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4250 root->root_key.objectid, dir_ino,
4251 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004252 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004253 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004254 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004255 goto out;
4256 }
Li Zefan33345d012011-04-20 10:31:50 +08004257 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004258 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004259 if (IS_ERR_OR_NULL(di)) {
4260 if (!di)
4261 ret = -ENOENT;
4262 else
4263 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004264 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004265 goto out;
4266 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004267
4268 leaf = path->nodes[0];
4269 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004270 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004271 index = key.offset;
4272 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004273 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004274
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004275 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004276 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004277 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004278 goto out;
4279 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004280
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004281 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004282 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004283 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004284 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004285 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004286 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004287out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004288 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004289 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004290}
4291
Chris Mason39279cc2007-06-12 06:35:45 -04004292static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4293{
David Howells2b0143b2015-03-17 22:25:59 +00004294 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004295 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004296 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004297 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004298 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004299
David Sterbab3ae2442012-09-13 16:04:34 -06004300 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004301 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004302 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004303 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004304
Josef Bacikd52be812013-05-29 14:54:47 -04004305 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004306 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004307 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004308
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004309 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004310 err = btrfs_unlink_subvol(trans, root, dir,
4311 BTRFS_I(inode)->location.objectid,
4312 dentry->d_name.name,
4313 dentry->d_name.len);
4314 goto out;
4315 }
4316
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004317 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004318 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004319 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004320
Filipe Manana44f714d2016-06-06 16:11:13 +01004321 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4322
Chris Mason39279cc2007-06-12 06:35:45 -04004323 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004324 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4325 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4326 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004327 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004328 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004329 /*
4330 * Propagate the last_unlink_trans value of the deleted dir to
4331 * its parent directory. This is to prevent an unrecoverable
4332 * log tree in the case we do something like this:
4333 * 1) create dir foo
4334 * 2) create snapshot under dir foo
4335 * 3) delete the snapshot
4336 * 4) rmdir foo
4337 * 5) mkdir foo
4338 * 6) fsync foo or some file inside foo
4339 */
4340 if (last_unlink_trans >= trans->transid)
4341 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4342 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004343out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004344 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004345 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004346
Chris Mason39279cc2007-06-12 06:35:45 -04004347 return err;
4348}
4349
Chris Mason28f75a02015-02-04 06:59:29 -08004350static int truncate_space_check(struct btrfs_trans_handle *trans,
4351 struct btrfs_root *root,
4352 u64 bytes_deleted)
4353{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004354 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004355 int ret;
4356
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004357 /*
4358 * This is only used to apply pressure to the enospc system, we don't
4359 * intend to use this reservation at all.
4360 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004361 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004362 bytes_deleted *= fs_info->nodesize;
4363 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004364 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004365 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004366 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004367 trans->transid,
4368 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004369 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004370 }
Chris Mason28f75a02015-02-04 06:59:29 -08004371 return ret;
4372
4373}
4374
Filipe Manana0305cd52015-10-16 12:34:25 +01004375static int truncate_inline_extent(struct inode *inode,
4376 struct btrfs_path *path,
4377 struct btrfs_key *found_key,
4378 const u64 item_end,
4379 const u64 new_size)
4380{
4381 struct extent_buffer *leaf = path->nodes[0];
4382 int slot = path->slots[0];
4383 struct btrfs_file_extent_item *fi;
4384 u32 size = (u32)(new_size - found_key->offset);
4385 struct btrfs_root *root = BTRFS_I(inode)->root;
4386
4387 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4388
4389 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4390 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004391 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004392
4393 /*
4394 * Zero out the remaining of the last page of our inline extent,
4395 * instead of directly truncating our inline extent here - that
4396 * would be much more complex (decompressing all the data, then
4397 * compressing the truncated data, which might be bigger than
4398 * the size of the inline extent, resize the extent, etc).
4399 * We release the path because to get the page we might need to
4400 * read the extent item from disk (data not in the page cache).
4401 */
4402 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304403 return btrfs_truncate_block(inode, offset, page_end - offset,
4404 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004405 }
4406
4407 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4408 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004409 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004410
4411 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4412 inode_sub_bytes(inode, item_end + 1 - new_size);
4413
4414 return 0;
4415}
4416
Chris Mason323ac952008-10-01 19:05:46 -04004417/*
Chris Mason39279cc2007-06-12 06:35:45 -04004418 * this can truncate away extent items, csum items and directory items.
4419 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004420 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004421 *
4422 * csum items that cross the new i_size are truncated to the new size
4423 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004424 *
4425 * min_type is the minimum key type to truncate down to. If set to 0, this
4426 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004427 */
Yan, Zheng80825102009-11-12 09:35:36 +00004428int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4429 struct btrfs_root *root,
4430 struct inode *inode,
4431 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004432{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004433 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004434 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004435 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004436 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004437 struct btrfs_key key;
4438 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004439 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004440 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004441 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004442 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004443 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004444 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004445 int found_extent;
4446 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004447 int pending_del_nr = 0;
4448 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004449 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004450 int ret;
4451 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004452 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004453 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004454 bool be_nice = 0;
4455 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004456 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004457
4458 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004459
Chris Mason28ed1342014-12-17 09:41:04 -08004460 /*
4461 * for non-free space inodes and ref cows, we want to back off from
4462 * time to time
4463 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004464 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004465 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4466 be_nice = 1;
4467
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004468 path = btrfs_alloc_path();
4469 if (!path)
4470 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004471 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004472
Josef Bacik5dc562c2012-08-17 13:14:17 -04004473 /*
4474 * We want to drop from the next block forward in case this new size is
4475 * not block aligned since we will be keeping the last block of the
4476 * extent just the way it is.
4477 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004478 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004479 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004480 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004481 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004482 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004483
Miao Xie16cdcec2011-04-22 18:12:22 +08004484 /*
4485 * This function is also used to drop the items in the log tree before
4486 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4487 * it is used to drop the loged items. So we shouldn't kill the delayed
4488 * items.
4489 */
4490 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004491 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004492
Li Zefan33345d012011-04-20 10:31:50 +08004493 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004494 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004495 key.type = (u8)-1;
4496
Chris Mason85e21ba2008-01-29 15:11:36 -05004497search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004498 /*
4499 * with a 16K leaf size and 128MB extents, you can actually queue
4500 * up a huge file in a single leaf. Most of the time that
4501 * bytes_deleted is > 0, it will be huge by the time we get here
4502 */
Byongho Leeee221842015-12-15 01:42:10 +09004503 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004504 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004505 err = -EAGAIN;
4506 goto error;
4507 }
4508 }
4509
4510
Chris Masonb9473432009-03-13 11:00:37 -04004511 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004512 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004513 if (ret < 0) {
4514 err = ret;
4515 goto out;
4516 }
Chris Masond3977122009-01-05 21:25:51 -05004517
Chris Mason85e21ba2008-01-29 15:11:36 -05004518 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004519 /* there are no items in the tree for us to truncate, we're
4520 * done
4521 */
Yan, Zheng80825102009-11-12 09:35:36 +00004522 if (path->slots[0] == 0)
4523 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004524 path->slots[0]--;
4525 }
4526
Chris Masond3977122009-01-05 21:25:51 -05004527 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004528 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004529 leaf = path->nodes[0];
4530 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004531 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004532
Li Zefan33345d012011-04-20 10:31:50 +08004533 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004534 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004535
Chris Mason85e21ba2008-01-29 15:11:36 -05004536 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004537 break;
4538
Chris Mason5f39d392007-10-15 16:14:19 -04004539 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004540 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004541 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004542 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004543 extent_type = btrfs_file_extent_type(leaf, fi);
4544 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004545 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004546 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004547
4548 trace_btrfs_truncate_show_fi_regular(
4549 BTRFS_I(inode), leaf, fi,
4550 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004551 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004552 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004553 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004554
4555 trace_btrfs_truncate_show_fi_inline(
4556 BTRFS_I(inode), leaf, fi, path->slots[0],
4557 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004558 }
Yan008630c2007-11-07 13:31:09 -05004559 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004560 }
Yan, Zheng80825102009-11-12 09:35:36 +00004561 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004562 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004563 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004564 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004565 break;
4566 if (found_key.offset >= new_size)
4567 del_item = 1;
4568 else
4569 del_item = 0;
4570 }
Chris Mason39279cc2007-06-12 06:35:45 -04004571 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004572 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004573 if (found_type != BTRFS_EXTENT_DATA_KEY)
4574 goto delete;
4575
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004576 if (del_item)
4577 last_size = found_key.offset;
4578 else
4579 last_size = new_size;
4580
Chris Mason179e29e2007-11-01 11:28:41 -04004581 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004582 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004583 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004584 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004585 u64 orig_num_bytes =
4586 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004587 extent_num_bytes = ALIGN(new_size -
4588 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004589 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004590 btrfs_set_file_extent_num_bytes(leaf, fi,
4591 extent_num_bytes);
4592 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004593 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004594 if (test_bit(BTRFS_ROOT_REF_COWS,
4595 &root->state) &&
4596 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004597 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004598 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004599 } else {
Chris Masondb945352007-10-15 16:15:53 -04004600 extent_num_bytes =
4601 btrfs_file_extent_disk_num_bytes(leaf,
4602 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004603 extent_offset = found_key.offset -
4604 btrfs_file_extent_offset(leaf, fi);
4605
Chris Mason39279cc2007-06-12 06:35:45 -04004606 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004607 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004608 if (extent_start != 0) {
4609 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004610 if (test_bit(BTRFS_ROOT_REF_COWS,
4611 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004612 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004613 }
4614 }
Chris Mason90692182008-02-08 13:49:28 -05004615 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004616 /*
4617 * we can't truncate inline items that have had
4618 * special encodings
4619 */
4620 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004621 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4622 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004623
4624 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004625 * Need to release path in order to truncate a
4626 * compressed extent. So delete any accumulated
4627 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004628 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004629 if (btrfs_file_extent_compression(leaf, fi) !=
4630 BTRFS_COMPRESS_NONE && pending_del_nr) {
4631 err = btrfs_del_items(trans, root, path,
4632 pending_del_slot,
4633 pending_del_nr);
4634 if (err) {
4635 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004636 err);
4637 goto error;
4638 }
4639 pending_del_nr = 0;
4640 }
4641
4642 err = truncate_inline_extent(inode, path,
4643 &found_key,
4644 item_end,
4645 new_size);
4646 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004647 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004648 goto error;
4649 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004650 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4651 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004652 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004653 }
Chris Mason39279cc2007-06-12 06:35:45 -04004654 }
Chris Mason179e29e2007-11-01 11:28:41 -04004655delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004656 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004657 if (!pending_del_nr) {
4658 /* no pending yet, add ourselves */
4659 pending_del_slot = path->slots[0];
4660 pending_del_nr = 1;
4661 } else if (pending_del_nr &&
4662 path->slots[0] + 1 == pending_del_slot) {
4663 /* hop on the pending chunk */
4664 pending_del_nr++;
4665 pending_del_slot = path->slots[0];
4666 } else {
Chris Masond3977122009-01-05 21:25:51 -05004667 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004668 }
Chris Mason39279cc2007-06-12 06:35:45 -04004669 } else {
4670 break;
4671 }
Chris Mason28f75a02015-02-04 06:59:29 -08004672 should_throttle = 0;
4673
Miao Xie27cdeb72014-04-02 19:51:05 +08004674 if (found_extent &&
4675 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004676 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004677 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004678 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004679 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004680 extent_num_bytes, 0,
4681 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004682 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004683 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004684 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4685 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004686 trans->delayed_ref_updates * 2,
4687 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004688 if (be_nice) {
4689 if (truncate_space_check(trans, root,
4690 extent_num_bytes)) {
4691 should_end = 1;
4692 }
4693 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004694 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004695 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004696 }
Chris Mason39279cc2007-06-12 06:35:45 -04004697 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004698
Yan, Zheng80825102009-11-12 09:35:36 +00004699 if (found_type == BTRFS_INODE_ITEM_KEY)
4700 break;
4701
4702 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004703 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004704 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004705 if (pending_del_nr) {
4706 ret = btrfs_del_items(trans, root, path,
4707 pending_del_slot,
4708 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004709 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004710 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004711 goto error;
4712 }
Yan, Zheng80825102009-11-12 09:35:36 +00004713 pending_del_nr = 0;
4714 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004715 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004716 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004717 unsigned long updates = trans->delayed_ref_updates;
4718 if (updates) {
4719 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004720 ret = btrfs_run_delayed_refs(trans,
4721 fs_info,
4722 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004723 if (ret && !err)
4724 err = ret;
4725 }
4726 }
Chris Mason28f75a02015-02-04 06:59:29 -08004727 /*
4728 * if we failed to refill our space rsv, bail out
4729 * and let the transaction restart
4730 */
4731 if (should_end) {
4732 err = -EAGAIN;
4733 goto error;
4734 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004735 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004736 } else {
4737 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004738 }
Chris Mason39279cc2007-06-12 06:35:45 -04004739 }
Yan, Zheng80825102009-11-12 09:35:36 +00004740out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004741 if (pending_del_nr) {
4742 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4743 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004744 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004745 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004746 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004747error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004748 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4749 ASSERT(last_size >= new_size);
4750 if (!err && last_size > new_size)
4751 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004752 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004753 }
Chris Mason28ed1342014-12-17 09:41:04 -08004754
Chris Mason39279cc2007-06-12 06:35:45 -04004755 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004756
Byongho Leeee221842015-12-15 01:42:10 +09004757 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004758 unsigned long updates = trans->delayed_ref_updates;
4759 if (updates) {
4760 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004761 ret = btrfs_run_delayed_refs(trans, fs_info,
4762 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004763 if (ret && !err)
4764 err = ret;
4765 }
4766 }
Yan, Zheng80825102009-11-12 09:35:36 +00004767 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004768}
4769
Chris Masona52d9a82007-08-27 16:49:44 -04004770/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304771 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004772 * @inode - inode that we're zeroing
4773 * @from - the offset to start zeroing
4774 * @len - the length to zero, 0 to zero the entire range respective to the
4775 * offset
4776 * @front - zero up to the offset instead of from the offset on
4777 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304778 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004779 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004780 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304781int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004782 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004783{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004784 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004785 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004786 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4787 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004788 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004789 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004790 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004791 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004792 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304793 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004794 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004795 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004796 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304797 u64 block_start;
4798 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004799
Josef Bacik2aaa6652012-08-29 14:27:18 -04004800 if ((offset & (blocksize - 1)) == 0 &&
4801 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004802 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304803
Qu Wenruo364ecf32017-02-27 15:10:38 +08004804 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304805 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004806 if (ret)
4807 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004808
Chris Mason211c17f2008-05-15 09:13:45 -04004809again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004810 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004811 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004812 btrfs_delalloc_release_space(inode, data_reserved,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304813 round_down(from, blocksize),
4814 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004815 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004816 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004817 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004818
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304819 block_start = round_down(from, blocksize);
4820 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004821
Chris Masona52d9a82007-08-27 16:49:44 -04004822 if (!PageUptodate(page)) {
4823 ret = btrfs_readpage(NULL, page);
4824 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004825 if (page->mapping != mapping) {
4826 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004827 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004828 goto again;
4829 }
Chris Masona52d9a82007-08-27 16:49:44 -04004830 if (!PageUptodate(page)) {
4831 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004832 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004833 }
4834 }
Chris Mason211c17f2008-05-15 09:13:45 -04004835 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004836
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304837 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004838 set_page_extent_mapped(page);
4839
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304840 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004841 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304842 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004843 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004844 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004845 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004846 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004847 btrfs_put_ordered_extent(ordered);
4848 goto again;
4849 }
4850
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304851 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004852 EXTENT_DIRTY | EXTENT_DELALLOC |
4853 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004854 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004855
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304856 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004857 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004858 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304859 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004860 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004861 goto out_unlock;
4862 }
4863
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304864 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004865 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304866 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004867 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004868 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304869 memset(kaddr + (block_start - page_offset(page)),
4870 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004871 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304872 memset(kaddr + (block_start - page_offset(page)) + offset,
4873 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004874 flush_dcache_page(page);
4875 kunmap(page);
4876 }
Chris Mason247e7432008-07-17 12:53:51 -04004877 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004878 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304879 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004880 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004881
Chris Mason89642222008-07-24 09:41:53 -04004882out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004883 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004884 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304885 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004886 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004887 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004888out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004889 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004890 return ret;
4891}
4892
Josef Bacik16e75492013-10-22 12:18:51 -04004893static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4894 u64 offset, u64 len)
4895{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004896 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004897 struct btrfs_trans_handle *trans;
4898 int ret;
4899
4900 /*
4901 * Still need to make sure the inode looks like it's been updated so
4902 * that any holes get logged if we fsync.
4903 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004904 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4905 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004906 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4907 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4908 return 0;
4909 }
4910
4911 /*
4912 * 1 - for the one we're dropping
4913 * 1 - for the one we're adding
4914 * 1 - for updating the inode.
4915 */
4916 trans = btrfs_start_transaction(root, 3);
4917 if (IS_ERR(trans))
4918 return PTR_ERR(trans);
4919
4920 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4921 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004922 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004923 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004924 return ret;
4925 }
4926
David Sterbaf85b7372017-01-20 14:54:07 +01004927 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4928 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004929 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004930 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004931 else
4932 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004933 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004934 return ret;
4935}
4936
Josef Bacik695a0d02011-03-04 15:46:53 -05004937/*
4938 * This function puts in dummy file extents for the area we're creating a hole
4939 * for. So if we are truncating this file to a larger size we need to insert
4940 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4941 * the range between oldsize and size
4942 */
Josef Bacika41ad392011-01-31 15:30:16 -05004943int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004944{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004945 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004946 struct btrfs_root *root = BTRFS_I(inode)->root;
4947 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004948 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004949 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004950 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004951 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4952 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004953 u64 last_byte;
4954 u64 cur_offset;
4955 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004956 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004957
Josef Bacika71754f2013-06-17 17:14:39 -04004958 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304959 * If our size started in the middle of a block we need to zero out the
4960 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004961 * expose stale data.
4962 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304963 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004964 if (err)
4965 return err;
4966
Yan Zheng9036c102008-10-30 14:19:41 -04004967 if (size <= hole_start)
4968 return 0;
4969
Yan Zheng9036c102008-10-30 14:19:41 -04004970 while (1) {
4971 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004972
David Sterbaff13db42015-12-03 14:30:40 +01004973 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004974 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004975 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004976 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004977 if (!ordered)
4978 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004979 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4980 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004981 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004982 btrfs_put_ordered_extent(ordered);
4983 }
4984
Yan Zheng9036c102008-10-30 14:19:41 -04004985 cur_offset = hole_start;
4986 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004987 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004988 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004989 if (IS_ERR(em)) {
4990 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004991 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004992 break;
4993 }
Yan Zheng9036c102008-10-30 14:19:41 -04004994 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004995 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004996 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004997 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004998 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004999
Josef Bacik16e75492013-10-22 12:18:51 -04005000 err = maybe_insert_hole(root, inode, cur_offset,
5001 hole_size);
5002 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005003 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005004 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005005 cur_offset + hole_size - 1, 0);
5006 hole_em = alloc_extent_map();
5007 if (!hole_em) {
5008 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5009 &BTRFS_I(inode)->runtime_flags);
5010 goto next;
5011 }
5012 hole_em->start = cur_offset;
5013 hole_em->len = hole_size;
5014 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005015
Josef Bacik5dc562c2012-08-17 13:14:17 -04005016 hole_em->block_start = EXTENT_MAP_HOLE;
5017 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005018 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005019 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005020 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005021 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005022 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005023
5024 while (1) {
5025 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005026 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005027 write_unlock(&em_tree->lock);
5028 if (err != -EEXIST)
5029 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005030 btrfs_drop_extent_cache(BTRFS_I(inode),
5031 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005032 cur_offset +
5033 hole_size - 1, 0);
5034 }
5035 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005036 }
Josef Bacik16e75492013-10-22 12:18:51 -04005037next:
Yan Zheng9036c102008-10-30 14:19:41 -04005038 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005039 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005040 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005041 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005042 break;
5043 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005044 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005045 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
5046 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04005047 return err;
5048}
5049
Eric Sandeen3972f262013-01-12 02:57:22 +00005050static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005051{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005052 struct btrfs_root *root = BTRFS_I(inode)->root;
5053 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005054 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005055 loff_t newsize = attr->ia_size;
5056 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005057 int ret;
5058
Eric Sandeen3972f262013-01-12 02:57:22 +00005059 /*
5060 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5061 * special case where we need to update the times despite not having
5062 * these flags set. For all other operations the VFS set these flags
5063 * explicitly if it wants a timestamp update.
5064 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005065 if (newsize != oldsize) {
5066 inode_inc_iversion(inode);
5067 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5068 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005069 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005070 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005071
Josef Bacika41ad392011-01-31 15:30:16 -05005072 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005073 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005074 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005075 * This is to ensure the snapshot captures a fully consistent
5076 * state of this file - if the snapshot captures this expanding
5077 * truncation, it must capture all writes that happened before
5078 * this truncation.
5079 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005080 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005081 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005082 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005083 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005084 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005085 }
Yan, Zheng80825102009-11-12 09:35:36 +00005086
Miao Xief4a2f4c2011-12-14 20:12:01 -05005087 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005088 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005089 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005090 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005091 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005092
5093 i_size_write(inode, newsize);
5094 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305095 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005096 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005097 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005098 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005099 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005100
Josef Bacika41ad392011-01-31 15:30:16 -05005101 /*
5102 * We're truncating a file that used to have good data down to
5103 * zero. Make sure it gets into the ordered flush list so that
5104 * any new writes get down to disk quickly.
5105 */
5106 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005107 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5108 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005109
Josef Bacikf3fe8202013-01-07 17:03:21 -05005110 /*
5111 * 1 for the orphan item we're going to add
5112 * 1 for the orphan item deletion.
5113 */
5114 trans = btrfs_start_transaction(root, 2);
5115 if (IS_ERR(trans))
5116 return PTR_ERR(trans);
5117
5118 /*
5119 * We need to do this in case we fail at _any_ point during the
5120 * actual truncate. Once we do the truncate_setsize we could
5121 * invalidate pages which forces any outstanding ordered io to
5122 * be instantly completed which will give us extents that need
5123 * to be truncated. If we fail to get an orphan inode down we
5124 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005125 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005126 * will be consistent.
5127 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005128 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005129 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005130 if (ret)
5131 return ret;
5132
Josef Bacika41ad392011-01-31 15:30:16 -05005133 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5134 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005135
5136 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005137 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005138 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005139 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005140
Josef Bacika41ad392011-01-31 15:30:16 -05005141 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005142 if (ret && inode->i_nlink) {
5143 int err;
5144
Liu Bo19fd2df2016-12-01 13:01:02 -08005145 /* To get a stable disk_i_size */
5146 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5147 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005148 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005149 return err;
5150 }
5151
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005152 /*
5153 * failed to truncate, disk_i_size is only adjusted down
5154 * as we remove extents, so it should represent the true
5155 * size of the inode, so reset the in memory size and
5156 * delete our orphan entry.
5157 */
5158 trans = btrfs_join_transaction(root);
5159 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005160 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005161 return ret;
5162 }
5163 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005164 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005165 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005166 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005167 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005168 }
Yan, Zheng80825102009-11-12 09:35:36 +00005169 }
5170
Josef Bacika41ad392011-01-31 15:30:16 -05005171 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005172}
5173
Chris Mason39279cc2007-06-12 06:35:45 -04005174static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5175{
David Howells2b0143b2015-03-17 22:25:59 +00005176 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005177 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005178 int err;
5179
Li Zefanb83cc962010-12-20 16:04:08 +08005180 if (btrfs_root_readonly(root))
5181 return -EROFS;
5182
Jan Kara31051c82016-05-26 16:55:18 +02005183 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005184 if (err)
5185 return err;
5186
Chris Mason5a3f23d2009-03-31 13:27:11 -04005187 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005188 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005189 if (err)
5190 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005191 }
Yan Zheng9036c102008-10-30 14:19:41 -04005192
Christoph Hellwig10257742010-06-04 11:30:02 +02005193 if (attr->ia_valid) {
5194 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005195 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005196 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005197
Josef Bacik22c44fe2011-11-30 10:45:38 -05005198 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005199 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005200 }
5201
Chris Mason39279cc2007-06-12 06:35:45 -04005202 return err;
5203}
Chris Mason61295eb2008-01-14 16:24:38 -05005204
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005205/*
5206 * While truncating the inode pages during eviction, we get the VFS calling
5207 * btrfs_invalidatepage() against each page of the inode. This is slow because
5208 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5209 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5210 * extent_state structures over and over, wasting lots of time.
5211 *
5212 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5213 * those expensive operations on a per page basis and do only the ordered io
5214 * finishing, while we release here the extent_map and extent_state structures,
5215 * without the excessive merging and splitting.
5216 */
5217static void evict_inode_truncate_pages(struct inode *inode)
5218{
5219 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5220 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5221 struct rb_node *node;
5222
5223 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005224 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005225
5226 write_lock(&map_tree->lock);
5227 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5228 struct extent_map *em;
5229
5230 node = rb_first(&map_tree->map);
5231 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005232 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5233 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005234 remove_extent_mapping(map_tree, em);
5235 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005236 if (need_resched()) {
5237 write_unlock(&map_tree->lock);
5238 cond_resched();
5239 write_lock(&map_tree->lock);
5240 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005241 }
5242 write_unlock(&map_tree->lock);
5243
Filipe Manana6ca07092015-05-26 00:55:42 +01005244 /*
5245 * Keep looping until we have no more ranges in the io tree.
5246 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005247 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5248 * still in progress (unlocked the pages in the bio but did not yet
5249 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005250 * ranges can still be locked and eviction started because before
5251 * submitting those bios, which are executed by a separate task (work
5252 * queue kthread), inode references (inode->i_count) were not taken
5253 * (which would be dropped in the end io callback of each bio).
5254 * Therefore here we effectively end up waiting for those bios and
5255 * anyone else holding locked ranges without having bumped the inode's
5256 * reference count - if we don't do it, when they access the inode's
5257 * io_tree to unlock a range it may be too late, leading to an
5258 * use-after-free issue.
5259 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005260 spin_lock(&io_tree->lock);
5261 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5262 struct extent_state *state;
5263 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005264 u64 start;
5265 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005266
5267 node = rb_first(&io_tree->state);
5268 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005269 start = state->start;
5270 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005271 spin_unlock(&io_tree->lock);
5272
David Sterbaff13db42015-12-03 14:30:40 +01005273 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005274
5275 /*
5276 * If still has DELALLOC flag, the extent didn't reach disk,
5277 * and its reserved space won't be freed by delayed_ref.
5278 * So we need to free its reserved space here.
5279 * (Refer to comment in btrfs_invalidatepage, case 2)
5280 *
5281 * Note, end is the bytenr of last byte, so we need + 1 here.
5282 */
5283 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005284 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005285
Filipe Manana6ca07092015-05-26 00:55:42 +01005286 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005287 EXTENT_LOCKED | EXTENT_DIRTY |
5288 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5289 EXTENT_DEFRAG, 1, 1,
5290 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005291
Filipe Manana7064dd52014-08-08 02:47:05 +01005292 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005293 spin_lock(&io_tree->lock);
5294 }
5295 spin_unlock(&io_tree->lock);
5296}
5297
Al Virobd555972010-06-07 11:35:40 -04005298void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005299{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005300 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005301 struct btrfs_trans_handle *trans;
5302 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005303 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005304 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005305 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005306 int ret;
5307
liubo1abe9b82011-03-24 11:18:59 +00005308 trace_btrfs_inode_evict(inode);
5309
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005310 if (!root) {
5311 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5312 return;
5313 }
5314
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005315 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005316
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005317 evict_inode_truncate_pages(inode);
5318
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005319 if (inode->i_nlink &&
5320 ((btrfs_root_refs(&root->root_item) != 0 &&
5321 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005322 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005323 goto no_delete;
5324
Chris Mason39279cc2007-06-12 06:35:45 -04005325 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005326 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005327 goto no_delete;
5328 }
Al Virobd555972010-06-07 11:35:40 -04005329 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005330 if (!special_file(inode->i_mode))
5331 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005332
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005333 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005334
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005335 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005336 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005337 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005338 goto no_delete;
5339 }
5340
Yan, Zheng76dda932009-09-21 16:00:26 -04005341 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005342 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5343 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005344 goto no_delete;
5345 }
5346
Nikolay Borisovaa790212017-01-10 20:35:40 +02005347 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005348 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005349 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005350 goto no_delete;
5351 }
5352
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005353 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005354 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005355 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005356 goto no_delete;
5357 }
Josef Bacik4a338542011-08-29 11:01:31 -04005358 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005359 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005360 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005361
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005362 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005363
Josef Bacik4289a662011-08-05 13:22:24 -04005364 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005365 * This is a bit simpler than btrfs_truncate since we've already
5366 * reserved our space for our orphan item in the unlink, so we just
5367 * need to reserve some slack space in case we add bytes and update
5368 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005369 */
Yan, Zheng80825102009-11-12 09:35:36 +00005370 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005371 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5372 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005373
Josef Bacik726c35f2011-09-26 15:46:06 -04005374 /*
5375 * Try and steal from the global reserve since we will
5376 * likely not use this space anyway, we want to try as
5377 * hard as possible to get this to work.
5378 */
5379 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005380 steal_from_global++;
5381 else
5382 steal_from_global = 0;
5383 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005384
Josef Bacik3bce8762015-02-24 12:35:51 -08005385 /*
5386 * steal_from_global == 0: we reserved stuff, hooray!
5387 * steal_from_global == 1: we didn't reserve stuff, boo!
5388 * steal_from_global == 2: we've committed, still not a lot of
5389 * room but maybe we'll have room in the global reserve this
5390 * time.
5391 * steal_from_global == 3: abandon all hope!
5392 */
5393 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005394 btrfs_warn(fs_info,
5395 "Could not get space for a delete, will truncate on mount %d",
5396 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005397 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005398 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005399 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005400 }
5401
Miao Xie0e8c36a2012-12-19 06:59:51 +00005402 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005403 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005404 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005405 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005406 goto no_delete;
5407 }
5408
Josef Bacik3bce8762015-02-24 12:35:51 -08005409 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005410 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005411 * sure there is room to do it, if not we need to commit and try
5412 * again.
5413 */
5414 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005415 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005416 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005417 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005418 else
5419 ret = -ENOSPC;
5420 }
5421
5422 /*
5423 * Couldn't steal from the global reserve, we have too much
5424 * pending stuff built up, commit the transaction and try it
5425 * again.
5426 */
5427 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005428 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005429 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005430 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005431 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005432 goto no_delete;
5433 }
5434 continue;
5435 } else {
5436 steal_from_global = 0;
5437 }
5438
Josef Bacik4289a662011-08-05 13:22:24 -04005439 trans->block_rsv = rsv;
5440
Yan, Zhengd68fc572010-05-16 10:49:58 -04005441 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005442 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005443 break;
5444
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005445 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005446 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005447 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005448 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005449 }
5450
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005451 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005452
Josef Bacik4ef31a42013-08-13 14:10:08 -04005453 /*
5454 * Errors here aren't a big deal, it just means we leave orphan items
5455 * in the tree. They will be cleaned up on the next mount.
5456 */
Yan, Zheng80825102009-11-12 09:35:36 +00005457 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005458 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005459 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005460 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005461 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005462 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005463
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005464 trans->block_rsv = &fs_info->trans_block_rsv;
5465 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005466 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005467 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005468
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005469 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005470 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005471no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005472 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005473 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005474}
5475
5476/*
5477 * this returns the key found in the dir entry in the location pointer.
5478 * If no dir entries were found, location->objectid is 0.
5479 */
5480static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5481 struct btrfs_key *location)
5482{
5483 const char *name = dentry->d_name.name;
5484 int namelen = dentry->d_name.len;
5485 struct btrfs_dir_item *di;
5486 struct btrfs_path *path;
5487 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005488 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005489
5490 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005491 if (!path)
5492 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005493
David Sterbaf85b7372017-01-20 14:54:07 +01005494 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5495 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005496 if (IS_ERR(di))
5497 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005498
David Sterbac7040052011-04-19 18:00:01 +02005499 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005500 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005501
Chris Mason5f39d392007-10-15 16:14:19 -04005502 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005503out:
Chris Mason39279cc2007-06-12 06:35:45 -04005504 btrfs_free_path(path);
5505 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005506out_err:
5507 location->objectid = 0;
5508 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005509}
5510
5511/*
5512 * when we hit a tree root in a directory, the btrfs part of the inode
5513 * needs to be changed to reflect the root directory of the tree root. This
5514 * is kind of like crossing a mount point.
5515 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005516static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005517 struct inode *dir,
5518 struct dentry *dentry,
5519 struct btrfs_key *location,
5520 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005521{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005522 struct btrfs_path *path;
5523 struct btrfs_root *new_root;
5524 struct btrfs_root_ref *ref;
5525 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005526 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005527 int ret;
5528 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005529
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005530 path = btrfs_alloc_path();
5531 if (!path) {
5532 err = -ENOMEM;
5533 goto out;
5534 }
Chris Mason39279cc2007-06-12 06:35:45 -04005535
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005536 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005537 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5538 key.type = BTRFS_ROOT_REF_KEY;
5539 key.offset = location->objectid;
5540
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005541 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005542 if (ret) {
5543 if (ret < 0)
5544 err = ret;
5545 goto out;
5546 }
Chris Mason39279cc2007-06-12 06:35:45 -04005547
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005548 leaf = path->nodes[0];
5549 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005550 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005551 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5552 goto out;
5553
5554 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5555 (unsigned long)(ref + 1),
5556 dentry->d_name.len);
5557 if (ret)
5558 goto out;
5559
David Sterbab3b4aa72011-04-21 01:20:15 +02005560 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005561
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005562 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005563 if (IS_ERR(new_root)) {
5564 err = PTR_ERR(new_root);
5565 goto out;
5566 }
5567
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005568 *sub_root = new_root;
5569 location->objectid = btrfs_root_dirid(&new_root->root_item);
5570 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005571 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005572 err = 0;
5573out:
5574 btrfs_free_path(path);
5575 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005576}
5577
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005578static void inode_tree_add(struct inode *inode)
5579{
5580 struct btrfs_root *root = BTRFS_I(inode)->root;
5581 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005582 struct rb_node **p;
5583 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005584 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005585 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005586
Al Viro1d3382cb2010-10-23 15:19:20 -04005587 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005588 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005589 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005590 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005591 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005592 while (*p) {
5593 parent = *p;
5594 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5595
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005596 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005597 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005598 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005599 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005600 else {
5601 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005602 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005603 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005604 RB_CLEAR_NODE(parent);
5605 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005606 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607 }
5608 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005609 rb_link_node(new, parent, p);
5610 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005611 spin_unlock(&root->inode_lock);
5612}
5613
5614static void inode_tree_del(struct inode *inode)
5615{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005616 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005617 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005618 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005619
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005620 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005621 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005622 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005623 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005624 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005625 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005626 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005627
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005628 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005629 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005630 spin_lock(&root->inode_lock);
5631 empty = RB_EMPTY_ROOT(&root->inode_tree);
5632 spin_unlock(&root->inode_lock);
5633 if (empty)
5634 btrfs_add_dead_root(root);
5635 }
5636}
5637
Jeff Mahoney143bede2012-03-01 14:56:26 +01005638void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005639{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005640 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005641 struct rb_node *node;
5642 struct rb_node *prev;
5643 struct btrfs_inode *entry;
5644 struct inode *inode;
5645 u64 objectid = 0;
5646
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005647 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005648 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005649
5650 spin_lock(&root->inode_lock);
5651again:
5652 node = root->inode_tree.rb_node;
5653 prev = NULL;
5654 while (node) {
5655 prev = node;
5656 entry = rb_entry(node, struct btrfs_inode, rb_node);
5657
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005658 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005659 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005660 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005661 node = node->rb_right;
5662 else
5663 break;
5664 }
5665 if (!node) {
5666 while (prev) {
5667 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005668 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005669 node = prev;
5670 break;
5671 }
5672 prev = rb_next(prev);
5673 }
5674 }
5675 while (node) {
5676 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005677 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005678 inode = igrab(&entry->vfs_inode);
5679 if (inode) {
5680 spin_unlock(&root->inode_lock);
5681 if (atomic_read(&inode->i_count) > 1)
5682 d_prune_aliases(inode);
5683 /*
Al Viro45321ac2010-06-07 13:43:19 -04005684 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005685 * the inode cache when its usage count
5686 * hits zero.
5687 */
5688 iput(inode);
5689 cond_resched();
5690 spin_lock(&root->inode_lock);
5691 goto again;
5692 }
5693
5694 if (cond_resched_lock(&root->inode_lock))
5695 goto again;
5696
5697 node = rb_next(node);
5698 }
5699 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005700}
5701
Chris Masone02119d2008-09-05 16:13:11 -04005702static int btrfs_init_locked_inode(struct inode *inode, void *p)
5703{
5704 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005705 inode->i_ino = args->location->objectid;
5706 memcpy(&BTRFS_I(inode)->location, args->location,
5707 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005708 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005709 return 0;
5710}
5711
5712static int btrfs_find_actor(struct inode *inode, void *opaque)
5713{
5714 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005715 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005716 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005717}
5718
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005719static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005720 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005721 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005722{
5723 struct inode *inode;
5724 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005725 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005726
Chris Mason90d3e592014-01-09 17:28:00 -08005727 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005728 args.root = root;
5729
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005730 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005731 btrfs_init_locked_inode,
5732 (void *)&args);
5733 return inode;
5734}
5735
Balaji Rao1a54ef82008-07-21 02:01:04 +05305736/* Get an inode object given its location and corresponding root.
5737 * Returns in *is_new if the inode was read from disk
5738 */
5739struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005740 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305741{
5742 struct inode *inode;
5743
Chris Mason90d3e592014-01-09 17:28:00 -08005744 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305745 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005746 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305747
5748 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005749 int ret;
5750
5751 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005752 if (!is_bad_inode(inode)) {
5753 inode_tree_add(inode);
5754 unlock_new_inode(inode);
5755 if (new)
5756 *new = 1;
5757 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005758 unlock_new_inode(inode);
5759 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005760 ASSERT(ret < 0);
5761 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005762 }
5763 }
5764
Balaji Rao1a54ef82008-07-21 02:01:04 +05305765 return inode;
5766}
5767
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005768static struct inode *new_simple_dir(struct super_block *s,
5769 struct btrfs_key *key,
5770 struct btrfs_root *root)
5771{
5772 struct inode *inode = new_inode(s);
5773
5774 if (!inode)
5775 return ERR_PTR(-ENOMEM);
5776
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005777 BTRFS_I(inode)->root = root;
5778 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005779 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005780
5781 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005782 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005783 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005784 inode->i_fop = &simple_dir_operations;
5785 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005786 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305787 inode->i_atime = inode->i_mtime;
5788 inode->i_ctime = inode->i_mtime;
5789 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005790
5791 return inode;
5792}
5793
Chris Mason3de45862008-11-17 21:02:50 -05005794struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005795{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005796 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005797 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005798 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005799 struct btrfs_root *sub_root = root;
5800 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005801 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005802 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005803
5804 if (dentry->d_name.len > BTRFS_NAME_LEN)
5805 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005806
Jeff Layton39e3c952012-11-28 11:30:53 -05005807 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005808 if (ret < 0)
5809 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005810
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005811 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005812 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005813
5814 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005815 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005816 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005817 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005818
5819 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5820
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005821 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005822 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005823 &location, &sub_root);
5824 if (ret < 0) {
5825 if (ret != -ENOENT)
5826 inode = ERR_PTR(ret);
5827 else
5828 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5829 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005830 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005831 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005832 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005833
Julia Lawall34d19ba2011-01-24 19:55:19 +00005834 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005835 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005836 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005837 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005838 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005839 if (ret) {
5840 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005841 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005842 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005843 }
5844
Chris Mason3de45862008-11-17 21:02:50 -05005845 return inode;
5846}
5847
Nick Pigginfe15ce42011-01-07 17:49:23 +11005848static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005849{
5850 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005851 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005852
Li Zefan848cce02012-02-21 17:04:28 +08005853 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005854 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005855
Li Zefan848cce02012-02-21 17:04:28 +08005856 if (inode) {
5857 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005858 if (btrfs_root_refs(&root->root_item) == 0)
5859 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005860
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005861 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005862 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005863 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005864 return 0;
5865}
5866
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005867static void btrfs_dentry_release(struct dentry *dentry)
5868{
Daeseok Youn944a4512014-04-14 15:37:02 +09005869 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005870}
5871
Chris Mason3de45862008-11-17 21:02:50 -05005872static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005873 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005874{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005875 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005876
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005877 inode = btrfs_lookup_dentry(dir, dentry);
5878 if (IS_ERR(inode)) {
5879 if (PTR_ERR(inode) == -ENOENT)
5880 inode = NULL;
5881 else
5882 return ERR_CAST(inode);
5883 }
5884
Al Viro41d28bc2014-10-12 22:24:21 -04005885 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005886}
5887
Miao Xie16cdcec2011-04-22 18:12:22 +08005888unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005889 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5890};
5891
Josef Bacik23b5ec72017-07-24 15:14:25 -04005892/*
5893 * All this infrastructure exists because dir_emit can fault, and we are holding
5894 * the tree lock when doing readdir. For now just allocate a buffer and copy
5895 * our information into that, and then dir_emit from the buffer. This is
5896 * similar to what NFS does, only we don't keep the buffer around in pagecache
5897 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5898 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5899 * tree lock.
5900 */
5901static int btrfs_opendir(struct inode *inode, struct file *file)
5902{
5903 struct btrfs_file_private *private;
5904
5905 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5906 if (!private)
5907 return -ENOMEM;
5908 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5909 if (!private->filldir_buf) {
5910 kfree(private);
5911 return -ENOMEM;
5912 }
5913 file->private_data = private;
5914 return 0;
5915}
5916
5917struct dir_entry {
5918 u64 ino;
5919 u64 offset;
5920 unsigned type;
5921 int name_len;
5922};
5923
5924static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5925{
5926 while (entries--) {
5927 struct dir_entry *entry = addr;
5928 char *name = (char *)(entry + 1);
5929
5930 ctx->pos = entry->offset;
5931 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5932 entry->type))
5933 return 1;
5934 addr += sizeof(struct dir_entry) + entry->name_len;
5935 ctx->pos++;
5936 }
5937 return 0;
5938}
5939
Al Viro9cdda8d2013-05-22 16:48:09 -04005940static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005941{
Al Viro9cdda8d2013-05-22 16:48:09 -04005942 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005943 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005944 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005945 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005946 struct btrfs_dir_item *di;
5947 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005948 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005949 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005950 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005951 struct list_head ins_list;
5952 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005953 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005954 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005955 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005956 char *name_ptr;
5957 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005958 int entries = 0;
5959 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005960 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005961 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005962
Al Viro9cdda8d2013-05-22 16:48:09 -04005963 if (!dir_emit_dots(file, ctx))
5964 return 0;
5965
David Woodhouse49593bf2008-08-17 17:08:36 +01005966 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005967 if (!path)
5968 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005969
Josef Bacik23b5ec72017-07-24 15:14:25 -04005970 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005971 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005972
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005973 INIT_LIST_HEAD(&ins_list);
5974 INIT_LIST_HEAD(&del_list);
5975 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005976
Josef Bacik23b5ec72017-07-24 15:14:25 -04005977again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005978 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005979 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005980 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005981
Chris Mason39279cc2007-06-12 06:35:45 -04005982 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5983 if (ret < 0)
5984 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005985
5986 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005987 struct dir_entry *entry;
5988
Chris Mason5f39d392007-10-15 16:14:19 -04005989 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005990 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005991 if (slot >= btrfs_header_nritems(leaf)) {
5992 ret = btrfs_next_leaf(root, path);
5993 if (ret < 0)
5994 goto err;
5995 else if (ret > 0)
5996 break;
5997 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005998 }
Chris Mason3de45862008-11-17 21:02:50 -05005999
Chris Mason5f39d392007-10-15 16:14:19 -04006000 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6001
6002 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006003 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006004 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006005 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006006 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006007 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006008 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006009 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006010 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Su Yuee79a3322017-06-06 17:57:01 +08006011 if (verify_dir_item(fs_info, leaf, slot, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006012 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01006013
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006014 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006015 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6016 PAGE_SIZE) {
6017 btrfs_release_path(path);
6018 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6019 if (ret)
6020 goto nopos;
6021 addr = private->filldir_buf;
6022 entries = 0;
6023 total_len = 0;
6024 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006025 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006026
6027 entry = addr;
6028 entry->name_len = name_len;
6029 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006030 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6031 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006032 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006033 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006034 entry->ino = location.objectid;
6035 entry->offset = found_key.offset;
6036 entries++;
6037 addr += sizeof(struct dir_entry) + name_len;
6038 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006039next:
6040 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006041 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006042 btrfs_release_path(path);
6043
6044 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6045 if (ret)
6046 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006047
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006048 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006049 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006050 goto nopos;
6051
Zach Browndb62efb2013-07-11 16:19:42 -07006052 /*
6053 * Stop new entries from being returned after we return the last
6054 * entry.
6055 *
6056 * New directory entries are assigned a strictly increasing
6057 * offset. This means that new entries created during readdir
6058 * are *guaranteed* to be seen in the future by that readdir.
6059 * This has broken buggy programs which operate on names as
6060 * they're returned by readdir. Until we re-use freed offsets
6061 * we have this hack to stop new entries from being returned
6062 * under the assumption that they'll never reach this huge
6063 * offset.
6064 *
6065 * This is being careful not to overflow 32bit loff_t unless the
6066 * last entry requires it because doing so has broken 32bit apps
6067 * in the past.
6068 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006069 if (ctx->pos >= INT_MAX)
6070 ctx->pos = LLONG_MAX;
6071 else
6072 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006073nopos:
6074 ret = 0;
6075err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006076 if (put)
6077 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006078 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006079 return ret;
6080}
6081
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006082int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006083{
6084 struct btrfs_root *root = BTRFS_I(inode)->root;
6085 struct btrfs_trans_handle *trans;
6086 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006087 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006088
Josef Bacik72ac3c02012-05-23 14:13:11 -04006089 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006090 return 0;
6091
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006092 if (btrfs_fs_closing(root->fs_info) &&
6093 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006094 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006095
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006096 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006097 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006098 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006099 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006100 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006101 if (IS_ERR(trans))
6102 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006103 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006104 }
6105 return ret;
6106}
6107
6108/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006109 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006110 * inode changes. But, it is most likely to find the inode in cache.
6111 * FIXME, needs more benchmarking...there are no reasons other than performance
6112 * to keep or drop this code.
6113 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006114static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006115{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006116 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006117 struct btrfs_root *root = BTRFS_I(inode)->root;
6118 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006119 int ret;
6120
Josef Bacik72ac3c02012-05-23 14:13:11 -04006121 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006122 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006123
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006124 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006125 if (IS_ERR(trans))
6126 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006127
6128 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006129 if (ret && ret == -ENOSPC) {
6130 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006131 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006132 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006133 if (IS_ERR(trans))
6134 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006135
Chris Mason94b60442010-05-26 11:02:00 -04006136 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006137 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006138 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006139 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006140 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006141
6142 return ret;
6143}
6144
6145/*
6146 * This is a copy of file_update_time. We need this so we can return error on
6147 * ENOSPC for updating the inode in the case of file write and mmap writes.
6148 */
Josef Bacike41f9412012-03-26 09:46:47 -04006149static int btrfs_update_time(struct inode *inode, struct timespec *now,
6150 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006151{
Alexander Block2bc5565282012-06-15 09:49:33 +02006152 struct btrfs_root *root = BTRFS_I(inode)->root;
6153
6154 if (btrfs_root_readonly(root))
6155 return -EROFS;
6156
Josef Bacike41f9412012-03-26 09:46:47 -04006157 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006158 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006159 if (flags & S_CTIME)
6160 inode->i_ctime = *now;
6161 if (flags & S_MTIME)
6162 inode->i_mtime = *now;
6163 if (flags & S_ATIME)
6164 inode->i_atime = *now;
6165 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006166}
6167
Chris Masond352ac62008-09-29 15:18:18 -04006168/*
6169 * find the highest existing sequence number in a directory
6170 * and then set the in-memory index_cnt variable to reflect
6171 * free sequence numbers
6172 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006173static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006174{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006175 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006176 struct btrfs_key key, found_key;
6177 struct btrfs_path *path;
6178 struct extent_buffer *leaf;
6179 int ret;
6180
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006181 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006182 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006183 key.offset = (u64)-1;
6184
6185 path = btrfs_alloc_path();
6186 if (!path)
6187 return -ENOMEM;
6188
6189 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6190 if (ret < 0)
6191 goto out;
6192 /* FIXME: we should be able to handle this */
6193 if (ret == 0)
6194 goto out;
6195 ret = 0;
6196
6197 /*
6198 * MAGIC NUMBER EXPLANATION:
6199 * since we search a directory based on f_pos we have to start at 2
6200 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6201 * else has to start at 2
6202 */
6203 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006204 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006205 goto out;
6206 }
6207
6208 path->slots[0]--;
6209
6210 leaf = path->nodes[0];
6211 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6212
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006213 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006214 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006215 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006216 goto out;
6217 }
6218
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006219 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006220out:
6221 btrfs_free_path(path);
6222 return ret;
6223}
6224
Chris Masond352ac62008-09-29 15:18:18 -04006225/*
6226 * helper to find a free sequence number in a given directory. This current
6227 * code is very simple, later versions will do smarter things in the btree
6228 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006229int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006230{
6231 int ret = 0;
6232
Nikolay Borisov877574e2017-02-20 13:50:33 +02006233 if (dir->index_cnt == (u64)-1) {
6234 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006235 if (ret) {
6236 ret = btrfs_set_inode_index_count(dir);
6237 if (ret)
6238 return ret;
6239 }
Josef Bacikaec74772008-07-24 12:12:38 -04006240 }
6241
Nikolay Borisov877574e2017-02-20 13:50:33 +02006242 *index = dir->index_cnt;
6243 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006244
6245 return ret;
6246}
6247
Chris Masonb0d5d102014-09-08 13:08:51 -07006248static int btrfs_insert_inode_locked(struct inode *inode)
6249{
6250 struct btrfs_iget_args args;
6251 args.location = &BTRFS_I(inode)->location;
6252 args.root = BTRFS_I(inode)->root;
6253
6254 return insert_inode_locked4(inode,
6255 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6256 btrfs_find_actor, &args);
6257}
6258
Anand Jain19aee8d2017-07-18 17:37:05 +08006259/*
6260 * Inherit flags from the parent inode.
6261 *
6262 * Currently only the compression flags and the cow flags are inherited.
6263 */
6264static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6265{
6266 unsigned int flags;
6267
6268 if (!dir)
6269 return;
6270
6271 flags = BTRFS_I(dir)->flags;
6272
6273 if (flags & BTRFS_INODE_NOCOMPRESS) {
6274 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6275 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6276 } else if (flags & BTRFS_INODE_COMPRESS) {
6277 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6278 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6279 }
6280
6281 if (flags & BTRFS_INODE_NODATACOW) {
6282 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6283 if (S_ISREG(inode->i_mode))
6284 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6285 }
6286
6287 btrfs_update_iflags(inode);
6288}
6289
Chris Mason39279cc2007-06-12 06:35:45 -04006290static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6291 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006292 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006293 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006294 u64 ref_objectid, u64 objectid,
6295 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006296{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006297 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006298 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006299 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006300 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006301 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006302 struct btrfs_inode_ref *ref;
6303 struct btrfs_key key[2];
6304 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006305 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006306 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006307 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006308
Chris Mason5f39d392007-10-15 16:14:19 -04006309 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006310 if (!path)
6311 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006312
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006313 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006314 if (!inode) {
6315 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006316 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006317 }
Chris Mason39279cc2007-06-12 06:35:45 -04006318
Li Zefan581bb052011-04-20 10:06:11 +08006319 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006320 * O_TMPFILE, set link count to 0, so that after this point,
6321 * we fill in an inode item with the correct link count.
6322 */
6323 if (!name)
6324 set_nlink(inode, 0);
6325
6326 /*
Li Zefan581bb052011-04-20 10:06:11 +08006327 * we have to initialize this early, so we can reclaim the inode
6328 * number if we fail afterwards in this function.
6329 */
6330 inode->i_ino = objectid;
6331
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006332 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006333 trace_btrfs_inode_request(dir);
6334
Nikolay Borisov877574e2017-02-20 13:50:33 +02006335 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006336 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006337 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006338 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006339 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006340 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006341 } else if (dir) {
6342 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006343 }
6344 /*
6345 * index_cnt is ignored for everything but a dir,
6346 * btrfs_get_inode_index_count has an explanation for the magic
6347 * number
6348 */
6349 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006350 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006351 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006352 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006353 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006354
Josef Bacik5dc562c2012-08-17 13:14:17 -04006355 /*
6356 * We could have gotten an inode number from somebody who was fsynced
6357 * and then removed in this same transaction, so let's just set full
6358 * sync since it will be a full sync anyway and this will blow away the
6359 * old info in the log.
6360 */
6361 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6362
Chris Mason9c583092008-01-29 15:15:18 -05006363 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006364 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006365 key[0].offset = 0;
6366
Chris Mason9c583092008-01-29 15:15:18 -05006367 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006368
6369 if (name) {
6370 /*
6371 * Start new inodes with an inode_ref. This is slightly more
6372 * efficient for small numbers of hard links since they will
6373 * be packed into one item. Extended refs will kick in if we
6374 * add more hard links than can fit in the ref item.
6375 */
6376 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006377 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006378 key[1].offset = ref_objectid;
6379
6380 sizes[1] = name_len + sizeof(*ref);
6381 }
Chris Mason9c583092008-01-29 15:15:18 -05006382
Chris Masonb0d5d102014-09-08 13:08:51 -07006383 location = &BTRFS_I(inode)->location;
6384 location->objectid = objectid;
6385 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006386 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006387
6388 ret = btrfs_insert_inode_locked(inode);
6389 if (ret < 0)
6390 goto fail;
6391
Chris Masonb9473432009-03-13 11:00:37 -04006392 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006393 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006394 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006395 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006396
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006397 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006398 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306399
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006400 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306401 inode->i_atime = inode->i_mtime;
6402 inode->i_ctime = inode->i_mtime;
6403 BTRFS_I(inode)->i_otime = inode->i_mtime;
6404
Chris Mason5f39d392007-10-15 16:14:19 -04006405 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6406 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006407 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006408 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006409 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006410
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006411 if (name) {
6412 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6413 struct btrfs_inode_ref);
6414 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6415 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6416 ptr = (unsigned long)(ref + 1);
6417 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6418 }
Chris Mason9c583092008-01-29 15:15:18 -05006419
Chris Mason5f39d392007-10-15 16:14:19 -04006420 btrfs_mark_buffer_dirty(path->nodes[0]);
6421 btrfs_free_path(path);
6422
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006423 btrfs_inherit_iflags(inode, dir);
6424
Al Viro569254b2011-07-24 17:08:40 -04006425 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006426 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006427 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006428 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006429 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6430 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006431 }
6432
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006433 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006434
6435 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006436 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006437
Alexander Block8ea05e32012-07-25 17:35:53 +02006438 btrfs_update_root_times(trans, root);
6439
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006440 ret = btrfs_inode_inherit_props(trans, inode, dir);
6441 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006442 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006443 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006444 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006445
Chris Mason39279cc2007-06-12 06:35:45 -04006446 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006447
6448fail_unlock:
6449 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006450fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006451 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006452 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006453 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006454 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006455 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006456}
6457
6458static inline u8 btrfs_inode_type(struct inode *inode)
6459{
6460 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6461}
6462
Chris Masond352ac62008-09-29 15:18:18 -04006463/*
6464 * utility function to add 'inode' into 'parent_inode' with
6465 * a give name and a given sequence number.
6466 * if 'add_backref' is true, also insert a backref from the
6467 * inode to the parent directory.
6468 */
Chris Masone02119d2008-09-05 16:13:11 -04006469int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006470 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006471 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006472{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006473 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006474 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006475 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006476 struct btrfs_root *root = parent_inode->root;
6477 u64 ino = btrfs_ino(inode);
6478 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006479
Li Zefan33345d012011-04-20 10:31:50 +08006480 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006481 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006482 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006483 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006484 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006485 key.offset = 0;
6486 }
Chris Mason39279cc2007-06-12 06:35:45 -04006487
Li Zefan33345d012011-04-20 10:31:50 +08006488 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006489 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6490 root->root_key.objectid, parent_ino,
6491 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006492 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006493 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6494 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006495 }
6496
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006497 /* Nothing to clean up yet */
6498 if (ret)
6499 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006500
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006501 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6502 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006503 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006504 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006505 goto fail_dir_item;
6506 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006507 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006508 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006509 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006510
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006511 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006512 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006513 inode_inc_iversion(&parent_inode->vfs_inode);
6514 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6515 current_time(&parent_inode->vfs_inode);
6516 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006517 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006518 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006519 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006520
6521fail_dir_item:
6522 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6523 u64 local_index;
6524 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006525 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6526 root->root_key.objectid, parent_ino,
6527 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006528
6529 } else if (add_backref) {
6530 u64 local_index;
6531 int err;
6532
6533 err = btrfs_del_inode_ref(trans, root, name, name_len,
6534 ino, parent_ino, &local_index);
6535 }
6536 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006537}
6538
6539static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006540 struct btrfs_inode *dir, struct dentry *dentry,
6541 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006542{
Josef Bacika1b075d2010-11-19 20:36:11 +00006543 int err = btrfs_add_link(trans, dir, inode,
6544 dentry->d_name.name, dentry->d_name.len,
6545 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006546 if (err > 0)
6547 err = -EEXIST;
6548 return err;
6549}
6550
Josef Bacik618e21d2007-07-11 10:18:17 -04006551static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006552 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006553{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006554 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006555 struct btrfs_trans_handle *trans;
6556 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006557 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006558 int err;
6559 int drop_inode = 0;
6560 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006561 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006562
Josef Bacik9ed74f22009-09-11 16:12:44 -04006563 /*
6564 * 2 for inode item and ref
6565 * 2 for dir items
6566 * 1 for xattr if selinux is on
6567 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006568 trans = btrfs_start_transaction(root, 5);
6569 if (IS_ERR(trans))
6570 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006571
Li Zefan581bb052011-04-20 10:06:11 +08006572 err = btrfs_find_free_ino(root, &objectid);
6573 if (err)
6574 goto out_unlock;
6575
Josef Bacikaec74772008-07-24 12:12:38 -04006576 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006577 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6578 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006579 if (IS_ERR(inode)) {
6580 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006581 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006582 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006583
Casey Schauflerad19db72011-12-15 10:09:07 -05006584 /*
6585 * If the active LSM wants to access the inode during
6586 * d_instantiate it needs these. Smack checks to see
6587 * if the filesystem supports xattrs by looking at the
6588 * ops vector.
6589 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006590 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006591 init_special_inode(inode, inode->i_mode, rdev);
6592
6593 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006594 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006595 goto out_unlock_inode;
6596
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006597 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6598 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006599 if (err) {
6600 goto out_unlock_inode;
6601 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006602 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006603 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006604 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006605 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006606
Josef Bacik618e21d2007-07-11 10:18:17 -04006607out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006608 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006609 btrfs_balance_delayed_items(fs_info);
6610 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006611 if (drop_inode) {
6612 inode_dec_link_count(inode);
6613 iput(inode);
6614 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006615 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006616
6617out_unlock_inode:
6618 drop_inode = 1;
6619 unlock_new_inode(inode);
6620 goto out_unlock;
6621
Josef Bacik618e21d2007-07-11 10:18:17 -04006622}
6623
Chris Mason39279cc2007-06-12 06:35:45 -04006624static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006625 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006626{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006627 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006628 struct btrfs_trans_handle *trans;
6629 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006630 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006631 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006632 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006633 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006634 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006635
Josef Bacik9ed74f22009-09-11 16:12:44 -04006636 /*
6637 * 2 for inode item and ref
6638 * 2 for dir items
6639 * 1 for xattr if selinux is on
6640 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006641 trans = btrfs_start_transaction(root, 5);
6642 if (IS_ERR(trans))
6643 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006644
Li Zefan581bb052011-04-20 10:06:11 +08006645 err = btrfs_find_free_ino(root, &objectid);
6646 if (err)
6647 goto out_unlock;
6648
Josef Bacikaec74772008-07-24 12:12:38 -04006649 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006650 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6651 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006652 if (IS_ERR(inode)) {
6653 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006654 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006655 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006656 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006657 /*
6658 * If the active LSM wants to access the inode during
6659 * d_instantiate it needs these. Smack checks to see
6660 * if the filesystem supports xattrs by looking at the
6661 * ops vector.
6662 */
6663 inode->i_fop = &btrfs_file_operations;
6664 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006665 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006666
6667 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6668 if (err)
6669 goto out_unlock_inode;
6670
6671 err = btrfs_update_inode(trans, root, inode);
6672 if (err)
6673 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006674
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006675 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6676 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006677 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006678 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006679
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006680 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006681 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006682 d_instantiate(dentry, inode);
6683
Chris Mason39279cc2007-06-12 06:35:45 -04006684out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006685 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006686 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006687 inode_dec_link_count(inode);
6688 iput(inode);
6689 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006690 btrfs_balance_delayed_items(fs_info);
6691 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006692 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006693
6694out_unlock_inode:
6695 unlock_new_inode(inode);
6696 goto out_unlock;
6697
Chris Mason39279cc2007-06-12 06:35:45 -04006698}
6699
6700static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6701 struct dentry *dentry)
6702{
Filipe Manana271dba452016-01-05 16:24:05 +00006703 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006704 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006705 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006706 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006707 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006708 int err;
6709 int drop_inode = 0;
6710
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006711 /* do not allow sys_link's with other subvols of the same device */
6712 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006713 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006714
Mark Fashehf1863732012-08-08 11:32:27 -07006715 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006716 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006717
Nikolay Borisov877574e2017-02-20 13:50:33 +02006718 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006719 if (err)
6720 goto fail;
6721
Yan, Zhenga22285a2010-05-16 10:48:46 -04006722 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006723 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006724 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006725 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006726 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006727 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006728 if (IS_ERR(trans)) {
6729 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006730 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006731 goto fail;
6732 }
Chris Mason5f39d392007-10-15 16:14:19 -04006733
Miao Xie67de1172013-12-26 13:07:06 +08006734 /* There are several dir indexes for this inode, clear the cache. */
6735 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006736 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006737 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006738 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006739 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006740 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006741
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006742 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6743 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006744
Yan, Zhenga5719522009-09-24 09:17:31 -04006745 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006746 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006747 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006748 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006749 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006750 if (err)
6751 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006752 if (inode->i_nlink == 1) {
6753 /*
6754 * If new hard link count is 1, it's a file created
6755 * with open(2) O_TMPFILE flag.
6756 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006757 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006758 if (err)
6759 goto fail;
6760 }
Al Viro08c422c2011-12-23 07:58:13 -05006761 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006762 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006763 }
Chris Mason39279cc2007-06-12 06:35:45 -04006764
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006765 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006766fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006767 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006768 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006769 if (drop_inode) {
6770 inode_dec_link_count(inode);
6771 iput(inode);
6772 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006773 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006774 return err;
6775}
6776
Al Viro18bb1db2011-07-26 01:41:39 -04006777static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006778{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006779 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006780 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006781 struct btrfs_trans_handle *trans;
6782 struct btrfs_root *root = BTRFS_I(dir)->root;
6783 int err = 0;
6784 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006785 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006786 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006787
Josef Bacik9ed74f22009-09-11 16:12:44 -04006788 /*
6789 * 2 items for inode and ref
6790 * 2 items for dir items
6791 * 1 for xattr if selinux is on
6792 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006793 trans = btrfs_start_transaction(root, 5);
6794 if (IS_ERR(trans))
6795 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006796
Li Zefan581bb052011-04-20 10:06:11 +08006797 err = btrfs_find_free_ino(root, &objectid);
6798 if (err)
6799 goto out_fail;
6800
Josef Bacikaec74772008-07-24 12:12:38 -04006801 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006802 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6803 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006804 if (IS_ERR(inode)) {
6805 err = PTR_ERR(inode);
6806 goto out_fail;
6807 }
Chris Mason5f39d392007-10-15 16:14:19 -04006808
Chris Mason39279cc2007-06-12 06:35:45 -04006809 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006810 /* these must be set before we unlock the inode */
6811 inode->i_op = &btrfs_dir_inode_operations;
6812 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006813
Eric Paris2a7dba32011-02-01 11:05:39 -05006814 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006815 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006816 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006817
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006818 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006819 err = btrfs_update_inode(trans, root, inode);
6820 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006821 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006822
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006823 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6824 dentry->d_name.name,
6825 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006826 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006827 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006828
Chris Mason39279cc2007-06-12 06:35:45 -04006829 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006830 /*
6831 * mkdir is special. We're unlocking after we call d_instantiate
6832 * to avoid a race with nfsd calling d_instantiate.
6833 */
6834 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006835 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006836
6837out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006838 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006839 if (drop_on_err) {
6840 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006841 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006842 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006843 btrfs_balance_delayed_items(fs_info);
6844 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006845 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006846
6847out_fail_inode:
6848 unlock_new_inode(inode);
6849 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006850}
6851
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006852/* Find next extent map of a given extent map, caller needs to ensure locks */
6853static struct extent_map *next_extent_map(struct extent_map *em)
6854{
6855 struct rb_node *next;
6856
6857 next = rb_next(&em->rb_node);
6858 if (!next)
6859 return NULL;
6860 return container_of(next, struct extent_map, rb_node);
6861}
6862
6863static struct extent_map *prev_extent_map(struct extent_map *em)
6864{
6865 struct rb_node *prev;
6866
6867 prev = rb_prev(&em->rb_node);
6868 if (!prev)
6869 return NULL;
6870 return container_of(prev, struct extent_map, rb_node);
6871}
6872
Chris Masond352ac62008-09-29 15:18:18 -04006873/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006874 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006875 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006876 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006877 */
Chris Mason3b951512008-04-17 11:29:12 -04006878static int merge_extent_mapping(struct extent_map_tree *em_tree,
6879 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006880 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006881 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006882{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006883 struct extent_map *prev;
6884 struct extent_map *next;
6885 u64 start;
6886 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006887 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006888
Chris Masone6dcd2d2008-07-17 12:53:50 -04006889 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006890
6891 if (existing->start > map_start) {
6892 next = existing;
6893 prev = prev_extent_map(next);
6894 } else {
6895 prev = existing;
6896 next = next_extent_map(prev);
6897 }
6898
6899 start = prev ? extent_map_end(prev) : em->start;
6900 start = max_t(u64, start, em->start);
6901 end = next ? next->start : extent_map_end(em);
6902 end = min_t(u64, end, extent_map_end(em));
6903 start_diff = start - em->start;
6904 em->start = start;
6905 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006906 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6907 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006908 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006909 em->block_len -= start_diff;
6910 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006911 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006912}
6913
Chris Masonc8b97812008-10-29 14:49:59 -04006914static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006915 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006916 size_t pg_offset, u64 extent_offset,
6917 struct btrfs_file_extent_item *item)
6918{
6919 int ret;
6920 struct extent_buffer *leaf = path->nodes[0];
6921 char *tmp;
6922 size_t max_size;
6923 unsigned long inline_size;
6924 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006925 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006926
6927 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006928 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006929 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6930 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006931 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006932 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006933 if (!tmp)
6934 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006935 ptr = btrfs_file_extent_inline_start(item);
6936
6937 read_extent_buffer(leaf, tmp, ptr, inline_size);
6938
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006939 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006940 ret = btrfs_decompress(compress_type, tmp, page,
6941 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006942
6943 /*
6944 * decompression code contains a memset to fill in any space between the end
6945 * of the uncompressed data and the end of max_size in case the decompressed
6946 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6947 * the end of an inline extent and the beginning of the next block, so we
6948 * cover that region here.
6949 */
6950
6951 if (max_size + pg_offset < PAGE_SIZE) {
6952 char *map = kmap(page);
6953 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6954 kunmap(page);
6955 }
Chris Masonc8b97812008-10-29 14:49:59 -04006956 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006957 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006958}
6959
Chris Masond352ac62008-09-29 15:18:18 -04006960/*
6961 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006962 * the ugly parts come from merging extents from the disk with the in-ram
6963 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006964 * where the in-ram extents might be locked pending data=ordered completion.
6965 *
6966 * This also copies inline extents directly into the page.
6967 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006968struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6969 struct page *page,
6970 size_t pg_offset, u64 start, u64 len,
6971 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006972{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006973 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006974 int ret;
6975 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006976 u64 extent_start = 0;
6977 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006978 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006979 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006980 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006981 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006982 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006983 struct extent_buffer *leaf;
6984 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006985 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006986 struct extent_map_tree *em_tree = &inode->extent_tree;
6987 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006988 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006989 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006990
Chris Masona52d9a82007-08-27 16:49:44 -04006991again:
Chris Mason890871b2009-09-02 16:24:52 -04006992 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006993 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006994 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006995 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006996 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006997
Chris Masona52d9a82007-08-27 16:49:44 -04006998 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006999 if (em->start > start || em->start + em->len <= start)
7000 free_extent_map(em);
7001 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05007002 free_extent_map(em);
7003 else
7004 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007005 }
David Sterba172ddd62011-04-21 00:48:27 +02007006 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04007007 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05007008 err = -ENOMEM;
7009 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007010 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007011 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05007012 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05007013 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05007014 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04007015 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04007016
7017 if (!path) {
7018 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04007019 if (!path) {
7020 err = -ENOMEM;
7021 goto out;
7022 }
7023 /*
7024 * Chances are we'll be called again, so go ahead and do
7025 * readahead
7026 */
David Sterbae4058b52015-11-27 16:31:35 +01007027 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04007028 }
7029
Chris Mason179e29e2007-11-01 11:28:41 -04007030 ret = btrfs_lookup_file_extent(trans, root, path,
7031 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04007032 if (ret < 0) {
7033 err = ret;
7034 goto out;
7035 }
7036
7037 if (ret != 0) {
7038 if (path->slots[0] == 0)
7039 goto not_found;
7040 path->slots[0]--;
7041 }
7042
Chris Mason5f39d392007-10-15 16:14:19 -04007043 leaf = path->nodes[0];
7044 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007045 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04007046 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04007047 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02007048 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04007049 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04007050 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007051 /*
7052 * If we backup past the first extent we want to move forward
7053 * and see if there is an extent in front of us, otherwise we'll
7054 * say there is a hole for our whole search range which can
7055 * cause problems.
7056 */
7057 extent_end = start;
7058 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007059 }
7060
Chris Mason5f39d392007-10-15 16:14:19 -04007061 found_type = btrfs_file_extent_type(leaf, item);
7062 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007063 if (found_type == BTRFS_FILE_EXTENT_REG ||
7064 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007065 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007066 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007067
7068 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7069 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007070 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7071 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007072 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007073 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007074 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007075
7076 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7077 path->slots[0],
7078 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007079 }
Josef Bacik25a50342013-10-14 12:08:38 -04007080next:
Yan Zheng9036c102008-10-30 14:19:41 -04007081 if (start >= extent_end) {
7082 path->slots[0]++;
7083 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7084 ret = btrfs_next_leaf(root, path);
7085 if (ret < 0) {
7086 err = ret;
7087 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007088 }
Yan Zheng9036c102008-10-30 14:19:41 -04007089 if (ret > 0)
7090 goto not_found;
7091 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007092 }
Yan Zheng9036c102008-10-30 14:19:41 -04007093 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7094 if (found_key.objectid != objectid ||
7095 found_key.type != BTRFS_EXTENT_DATA_KEY)
7096 goto not_found;
7097 if (start + len <= found_key.offset)
7098 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007099 if (start > found_key.offset)
7100 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007101 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007102 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007103 em->len = found_key.offset - start;
7104 goto not_found_em;
7105 }
7106
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007107 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007108 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007109
Yan Zhengd899e052008-10-30 14:25:28 -04007110 if (found_type == BTRFS_FILE_EXTENT_REG ||
7111 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007112 goto insert;
7113 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007114 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007115 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007116 size_t size;
7117 size_t extent_offset;
7118 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007119
Filipe Manana7ffbb592014-06-09 03:48:05 +01007120 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007121 goto out;
Yan689f9342007-10-29 11:41:07 -04007122
Chris Mason514ac8a2014-01-03 21:07:00 -08007123 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007124 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007125 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7126 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007127 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007128 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007129 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007130 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007131 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007132 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007133 if (btrfs_file_extent_compression(leaf, item) !=
7134 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007135 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007136 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007137 if (ret) {
7138 err = ret;
7139 goto out;
7140 }
Chris Masonc8b97812008-10-29 14:49:59 -04007141 } else {
7142 map = kmap(page);
7143 read_extent_buffer(leaf, map + pg_offset, ptr,
7144 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007145 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007146 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007147 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007148 copy_size);
7149 }
Chris Masonc8b97812008-10-29 14:49:59 -04007150 kunmap(page);
7151 }
Chris Mason179e29e2007-11-01 11:28:41 -04007152 flush_dcache_page(page);
7153 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007154 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007155 if (!trans) {
7156 kunmap(page);
7157 free_extent_map(em);
7158 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007159
David Sterbab3b4aa72011-04-21 01:20:15 +02007160 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007161 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007162
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007163 if (IS_ERR(trans))
7164 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007165 goto again;
7166 }
Chris Masonc8b97812008-10-29 14:49:59 -04007167 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007168 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007169 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007170 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007171 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007172 }
Chris Masond1310b22008-01-24 16:13:08 -05007173 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007174 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007175 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007176 }
7177not_found:
7178 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007179 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007180 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007181not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007182 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007183 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007184insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007185 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007186 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007187 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007188 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7189 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007190 err = -EIO;
7191 goto out;
7192 }
Chris Masond1310b22008-01-24 16:13:08 -05007193
7194 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007195 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007196 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007197 /* it is possible that someone inserted the extent into the tree
7198 * while we had the lock dropped. It is also possible that
7199 * an overlapping map exists in the tree
7200 */
Chris Masona52d9a82007-08-27 16:49:44 -04007201 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007202 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007203
7204 ret = 0;
7205
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007206 existing = search_extent_mapping(em_tree, start, len);
7207 /*
7208 * existing will always be non-NULL, since there must be
7209 * extent causing the -EEXIST.
7210 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007211 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007212 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007213 em->block_start == existing->block_start) {
7214 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007215 * The existing extent map already encompasses the
7216 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007217 */
7218 free_extent_map(em);
7219 em = existing;
7220 err = 0;
7221
7222 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007223 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007224 /*
7225 * The existing extent map is the one nearest to
7226 * the [start, start + len) range which overlaps
7227 */
7228 err = merge_extent_mapping(em_tree, existing,
7229 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007230 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007231 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007232 free_extent_map(em);
7233 em = NULL;
7234 }
7235 } else {
7236 free_extent_map(em);
7237 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007238 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007239 }
Chris Masona52d9a82007-08-27 16:49:44 -04007240 }
Chris Mason890871b2009-09-02 16:24:52 -04007241 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007242out:
liubo1abe9b82011-03-24 11:18:59 +00007243
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007244 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007245
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007246 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007247 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007248 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007249 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007250 err = ret;
7251 }
Chris Masona52d9a82007-08-27 16:49:44 -04007252 if (err) {
7253 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007254 return ERR_PTR(err);
7255 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007256 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007257 return em;
7258}
7259
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007260struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7261 struct page *page,
7262 size_t pg_offset, u64 start, u64 len,
7263 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007264{
7265 struct extent_map *em;
7266 struct extent_map *hole_em = NULL;
7267 u64 range_start = start;
7268 u64 end;
7269 u64 found;
7270 u64 found_end;
7271 int err = 0;
7272
7273 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7274 if (IS_ERR(em))
7275 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007276 /*
7277 * If our em maps to:
7278 * - a hole or
7279 * - a pre-alloc extent,
7280 * there might actually be delalloc bytes behind it.
7281 */
7282 if (em->block_start != EXTENT_MAP_HOLE &&
7283 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7284 return em;
7285 else
7286 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007287
7288 /* check to see if we've wrapped (len == -1 or similar) */
7289 end = start + len;
7290 if (end < start)
7291 end = (u64)-1;
7292 else
7293 end -= 1;
7294
7295 em = NULL;
7296
7297 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007298 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007299 end, len, EXTENT_DELALLOC, 1);
7300 found_end = range_start + found;
7301 if (found_end < range_start)
7302 found_end = (u64)-1;
7303
7304 /*
7305 * we didn't find anything useful, return
7306 * the original results from get_extent()
7307 */
7308 if (range_start > end || found_end <= start) {
7309 em = hole_em;
7310 hole_em = NULL;
7311 goto out;
7312 }
7313
7314 /* adjust the range_start to make sure it doesn't
7315 * go backwards from the start they passed in
7316 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307317 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007318 found = found_end - range_start;
7319
7320 if (found > 0) {
7321 u64 hole_start = start;
7322 u64 hole_len = len;
7323
David Sterba172ddd62011-04-21 00:48:27 +02007324 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007325 if (!em) {
7326 err = -ENOMEM;
7327 goto out;
7328 }
7329 /*
7330 * when btrfs_get_extent can't find anything it
7331 * returns one huge hole
7332 *
7333 * make sure what it found really fits our range, and
7334 * adjust to make sure it is based on the start from
7335 * the caller
7336 */
7337 if (hole_em) {
7338 u64 calc_end = extent_map_end(hole_em);
7339
7340 if (calc_end <= start || (hole_em->start > end)) {
7341 free_extent_map(hole_em);
7342 hole_em = NULL;
7343 } else {
7344 hole_start = max(hole_em->start, start);
7345 hole_len = calc_end - hole_start;
7346 }
7347 }
7348 em->bdev = NULL;
7349 if (hole_em && range_start > hole_start) {
7350 /* our hole starts before our delalloc, so we
7351 * have to return just the parts of the hole
7352 * that go until the delalloc starts
7353 */
7354 em->len = min(hole_len,
7355 range_start - hole_start);
7356 em->start = hole_start;
7357 em->orig_start = hole_start;
7358 /*
7359 * don't adjust block start at all,
7360 * it is fixed at EXTENT_MAP_HOLE
7361 */
7362 em->block_start = hole_em->block_start;
7363 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007364 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7365 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007366 } else {
7367 em->start = range_start;
7368 em->len = found;
7369 em->orig_start = range_start;
7370 em->block_start = EXTENT_MAP_DELALLOC;
7371 em->block_len = found;
7372 }
7373 } else if (hole_em) {
7374 return hole_em;
7375 }
7376out:
7377
7378 free_extent_map(hole_em);
7379 if (err) {
7380 free_extent_map(em);
7381 return ERR_PTR(err);
7382 }
7383 return em;
7384}
7385
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007386static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7387 const u64 start,
7388 const u64 len,
7389 const u64 orig_start,
7390 const u64 block_start,
7391 const u64 block_len,
7392 const u64 orig_block_len,
7393 const u64 ram_bytes,
7394 const int type)
7395{
7396 struct extent_map *em = NULL;
7397 int ret;
7398
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007399 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007400 em = create_io_em(inode, start, len, orig_start,
7401 block_start, block_len, orig_block_len,
7402 ram_bytes,
7403 BTRFS_COMPRESS_NONE, /* compress_type */
7404 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007405 if (IS_ERR(em))
7406 goto out;
7407 }
7408 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7409 len, block_len, type);
7410 if (ret) {
7411 if (em) {
7412 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007413 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007414 start + len - 1, 0);
7415 }
7416 em = ERR_PTR(ret);
7417 }
7418 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007419
7420 return em;
7421}
7422
Josef Bacik4b46fce2010-05-23 11:00:55 -04007423static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7424 u64 start, u64 len)
7425{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007426 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007427 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007428 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007429 struct btrfs_key ins;
7430 u64 alloc_hint;
7431 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007432
Josef Bacik4b46fce2010-05-23 11:00:55 -04007433 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007434 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007435 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007436 if (ret)
7437 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007438
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007439 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7440 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007441 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007442 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007443 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007444 btrfs_free_reserved_extent(fs_info, ins.objectid,
7445 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007446
Josef Bacik4b46fce2010-05-23 11:00:55 -04007447 return em;
7448}
7449
Chris Mason46bfbb52010-05-26 11:04:10 -04007450/*
7451 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7452 * block must be cow'd
7453 */
Josef Bacik00361582013-08-14 14:02:47 -04007454noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007455 u64 *orig_start, u64 *orig_block_len,
7456 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007457{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007458 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007459 struct btrfs_path *path;
7460 int ret;
7461 struct extent_buffer *leaf;
7462 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007463 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007464 struct btrfs_file_extent_item *fi;
7465 struct btrfs_key key;
7466 u64 disk_bytenr;
7467 u64 backref_offset;
7468 u64 extent_end;
7469 u64 num_bytes;
7470 int slot;
7471 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007472 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007473
Chris Mason46bfbb52010-05-26 11:04:10 -04007474 path = btrfs_alloc_path();
7475 if (!path)
7476 return -ENOMEM;
7477
David Sterbaf85b7372017-01-20 14:54:07 +01007478 ret = btrfs_lookup_file_extent(NULL, root, path,
7479 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007480 if (ret < 0)
7481 goto out;
7482
7483 slot = path->slots[0];
7484 if (ret == 1) {
7485 if (slot == 0) {
7486 /* can't find the item, must cow */
7487 ret = 0;
7488 goto out;
7489 }
7490 slot--;
7491 }
7492 ret = 0;
7493 leaf = path->nodes[0];
7494 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007495 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007496 key.type != BTRFS_EXTENT_DATA_KEY) {
7497 /* not our file or wrong item type, must cow */
7498 goto out;
7499 }
7500
7501 if (key.offset > offset) {
7502 /* Wrong offset, must cow */
7503 goto out;
7504 }
7505
7506 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7507 found_type = btrfs_file_extent_type(leaf, fi);
7508 if (found_type != BTRFS_FILE_EXTENT_REG &&
7509 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7510 /* not a regular extent, must cow */
7511 goto out;
7512 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007513
7514 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7515 goto out;
7516
Miao Xiee77751a2013-12-27 21:11:50 +08007517 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7518 if (extent_end <= offset)
7519 goto out;
7520
Chris Mason46bfbb52010-05-26 11:04:10 -04007521 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007522 if (disk_bytenr == 0)
7523 goto out;
7524
7525 if (btrfs_file_extent_compression(leaf, fi) ||
7526 btrfs_file_extent_encryption(leaf, fi) ||
7527 btrfs_file_extent_other_encoding(leaf, fi))
7528 goto out;
7529
Chris Mason46bfbb52010-05-26 11:04:10 -04007530 backref_offset = btrfs_file_extent_offset(leaf, fi);
7531
Josef Bacik7ee9e442013-06-21 16:37:03 -04007532 if (orig_start) {
7533 *orig_start = key.offset - backref_offset;
7534 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7535 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7536 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007537
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007538 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007539 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007540
7541 num_bytes = min(offset + *len, extent_end) - offset;
7542 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7543 u64 range_end;
7544
Jeff Mahoneyda170662016-06-15 09:22:56 -04007545 range_end = round_up(offset + num_bytes,
7546 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007547 ret = test_range_bit(io_tree, offset, range_end,
7548 EXTENT_DELALLOC, 0, NULL);
7549 if (ret) {
7550 ret = -EAGAIN;
7551 goto out;
7552 }
7553 }
7554
Josef Bacik1bda19e2013-10-18 12:10:36 -04007555 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007556
7557 /*
7558 * look for other files referencing this extent, if we
7559 * find any we must cow
7560 */
Josef Bacik00361582013-08-14 14:02:47 -04007561
Liu Boe4c3b2d2017-01-30 12:25:28 -08007562 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007563 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007564 if (ret) {
7565 ret = 0;
7566 goto out;
7567 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007568
7569 /*
7570 * adjust disk_bytenr and num_bytes to cover just the bytes
7571 * in this extent we are about to write. If there
7572 * are any csums in that range we have to cow in order
7573 * to keep the csums correct
7574 */
7575 disk_bytenr += backref_offset;
7576 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007577 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7578 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007579 /*
7580 * all of the above have passed, it is safe to overwrite this extent
7581 * without cow
7582 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007583 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007584 ret = 1;
7585out:
7586 btrfs_free_path(path);
7587 return ret;
7588}
7589
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007590bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7591{
7592 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007593 bool found = false;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007594 void **pagep = NULL;
7595 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007596 unsigned long start_idx;
7597 unsigned long end_idx;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007598
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007599 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007600
7601 /*
7602 * end is the last byte in the last page. end == start is legal
7603 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007604 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007605
7606 rcu_read_lock();
7607
7608 /* Most of the code in this while loop is lifted from
7609 * find_get_page. It's been modified to begin searching from a
7610 * page and return just the first page found in that range. If the
7611 * found idx is less than or equal to the end idx then we know that
7612 * a page exists. If no pages are found or if those pages are
7613 * outside of the range then we're fine (yay!) */
7614 while (page == NULL &&
7615 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7616 page = radix_tree_deref_slot(pagep);
7617 if (unlikely(!page))
7618 break;
7619
7620 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007621 if (radix_tree_deref_retry(page)) {
7622 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007623 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007624 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007625 /*
7626 * Otherwise, shmem/tmpfs must be storing a swap entry
7627 * here as an exceptional entry: so return it without
7628 * attempting to raise page count.
7629 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007630 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007631 break; /* TODO: Is this relevant for this use case? */
7632 }
7633
Filipe Manana91405152014-06-05 13:22:24 +01007634 if (!page_cache_get_speculative(page)) {
7635 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007636 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007637 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007638
7639 /*
7640 * Has the page moved?
7641 * This is part of the lockless pagecache protocol. See
7642 * include/linux/pagemap.h for details.
7643 */
7644 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007645 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007646 page = NULL;
7647 }
7648 }
7649
7650 if (page) {
7651 if (page->index <= end_idx)
7652 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007653 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007654 }
7655
7656 rcu_read_unlock();
7657 return found;
7658}
7659
Josef Bacikeb838e72012-07-31 16:28:48 -04007660static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7661 struct extent_state **cached_state, int writing)
7662{
7663 struct btrfs_ordered_extent *ordered;
7664 int ret = 0;
7665
7666 while (1) {
7667 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007668 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007669 /*
7670 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007671 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007672 * extents in this range.
7673 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007674 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007675 lockend - lockstart + 1);
7676
7677 /*
7678 * We need to make sure there are no buffered pages in this
7679 * range either, we could have raced between the invalidate in
7680 * generic_file_direct_write and locking the extent. The
7681 * invalidate needs to happen so that reads after a write do not
7682 * get stale data.
7683 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007684 if (!ordered &&
7685 (!writing ||
7686 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007687 break;
7688
7689 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7690 cached_state, GFP_NOFS);
7691
7692 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007693 /*
7694 * If we are doing a DIO read and the ordered extent we
7695 * found is for a buffered write, we can not wait for it
7696 * to complete and retry, because if we do so we can
7697 * deadlock with concurrent buffered writes on page
7698 * locks. This happens only if our DIO read covers more
7699 * than one extent map, if at this point has already
7700 * created an ordered extent for a previous extent map
7701 * and locked its range in the inode's io tree, and a
7702 * concurrent write against that previous extent map's
7703 * range and this range started (we unlock the ranges
7704 * in the io tree only when the bios complete and
7705 * buffered writes always lock pages before attempting
7706 * to lock range in the io tree).
7707 */
7708 if (writing ||
7709 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7710 btrfs_start_ordered_extent(inode, ordered, 1);
7711 else
7712 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007713 btrfs_put_ordered_extent(ordered);
7714 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007715 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007716 * We could trigger writeback for this range (and wait
7717 * for it to complete) and then invalidate the pages for
7718 * this range (through invalidate_inode_pages2_range()),
7719 * but that can lead us to a deadlock with a concurrent
7720 * call to readpages() (a buffered read or a defrag call
7721 * triggered a readahead) on a page lock due to an
7722 * ordered dio extent we created before but did not have
7723 * yet a corresponding bio submitted (whence it can not
7724 * complete), which makes readpages() wait for that
7725 * ordered extent to complete while holding a lock on
7726 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007727 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007728 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007729 }
7730
Filipe Mananaade77022016-02-18 14:28:55 +00007731 if (ret)
7732 break;
7733
Josef Bacikeb838e72012-07-31 16:28:48 -04007734 cond_resched();
7735 }
7736
7737 return ret;
7738}
7739
Liu Bo6f9994d2017-01-31 07:50:22 -08007740/* The callers of this must take lock_extent() */
7741static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7742 u64 orig_start, u64 block_start,
7743 u64 block_len, u64 orig_block_len,
7744 u64 ram_bytes, int compress_type,
7745 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007746{
7747 struct extent_map_tree *em_tree;
7748 struct extent_map *em;
7749 struct btrfs_root *root = BTRFS_I(inode)->root;
7750 int ret;
7751
Liu Bo6f9994d2017-01-31 07:50:22 -08007752 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7753 type == BTRFS_ORDERED_COMPRESSED ||
7754 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007755 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007756
Josef Bacik69ffb542012-09-11 15:40:07 -04007757 em_tree = &BTRFS_I(inode)->extent_tree;
7758 em = alloc_extent_map();
7759 if (!em)
7760 return ERR_PTR(-ENOMEM);
7761
7762 em->start = start;
7763 em->orig_start = orig_start;
7764 em->len = len;
7765 em->block_len = block_len;
7766 em->block_start = block_start;
7767 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007768 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007769 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007770 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007771 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007772 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007773 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007774 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007775 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7776 em->compress_type = compress_type;
7777 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007778
7779 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007780 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007781 em->start + em->len - 1, 0);
7782 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007783 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007784 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007785 /*
7786 * The caller has taken lock_extent(), who could race with us
7787 * to add em?
7788 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007789 } while (ret == -EEXIST);
7790
7791 if (ret) {
7792 free_extent_map(em);
7793 return ERR_PTR(ret);
7794 }
7795
Liu Bo6f9994d2017-01-31 07:50:22 -08007796 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007797 return em;
7798}
7799
Filipe Manana9c9464c2015-11-04 09:52:04 +00007800static void adjust_dio_outstanding_extents(struct inode *inode,
7801 struct btrfs_dio_data *dio_data,
7802 const u64 len)
7803{
David Sterba823bb202017-01-04 11:09:51 +01007804 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007805
Filipe Manana9c9464c2015-11-04 09:52:04 +00007806 /*
7807 * If we have an outstanding_extents count still set then we're
7808 * within our reservation, otherwise we need to adjust our inode
7809 * counter appropriately.
7810 */
Liu Boc2931662016-12-22 17:13:54 -08007811 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007812 dio_data->outstanding_extents -= num_extents;
7813 } else {
Liu Boc2931662016-12-22 17:13:54 -08007814 /*
7815 * If dio write length has been split due to no large enough
7816 * contiguous space, we need to compensate our inode counter
7817 * appropriately.
7818 */
7819 u64 num_needed = num_extents - dio_data->outstanding_extents;
7820
Filipe Manana9c9464c2015-11-04 09:52:04 +00007821 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007822 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007823 spin_unlock(&BTRFS_I(inode)->lock);
7824 }
7825}
7826
Josef Bacik4b46fce2010-05-23 11:00:55 -04007827static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7828 struct buffer_head *bh_result, int create)
7829{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007830 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007831 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007832 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307833 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007834 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007835 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007836 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007837 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007838 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007839
Miao Xie172a5042013-02-21 02:48:22 -07007840 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007841 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007842 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007843 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007844
Josef Bacikc3298612012-08-03 16:49:19 -04007845 lockstart = start;
7846 lockend = start + len - 1;
7847
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007848 if (current->journal_info) {
7849 /*
7850 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007851 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007852 * confused.
7853 */
chandan50745b02015-08-28 21:10:13 +05307854 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007855 current->journal_info = NULL;
7856 }
7857
Josef Bacikeb838e72012-07-31 16:28:48 -04007858 /*
7859 * If this errors out it's because we couldn't invalidate pagecache for
7860 * this range and we need to fallback to buffered.
7861 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007862 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7863 create)) {
7864 ret = -ENOTBLK;
7865 goto err;
7866 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007867
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007868 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007869 if (IS_ERR(em)) {
7870 ret = PTR_ERR(em);
7871 goto unlock_err;
7872 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007873
7874 /*
7875 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7876 * io. INLINE is special, and we could probably kludge it in here, but
7877 * it's still buffered so for safety lets just fall back to the generic
7878 * buffered path.
7879 *
7880 * For COMPRESSED we _have_ to read the entire extent in so we can
7881 * decompress it, so there will be buffering required no matter what we
7882 * do, so go ahead and fallback to buffered.
7883 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007884 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007885 * to buffered IO. Don't blame me, this is the price we pay for using
7886 * the generic code.
7887 */
7888 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7889 em->block_start == EXTENT_MAP_INLINE) {
7890 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007891 ret = -ENOTBLK;
7892 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007893 }
7894
7895 /* Just a good old fashioned hole, return */
7896 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7897 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7898 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007899 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007900 }
7901
7902 /*
7903 * We don't allocate a new extent in the following cases
7904 *
7905 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7906 * existing extent.
7907 * 2) The extent is marked as PREALLOC. We're good to go here and can
7908 * just use the extent.
7909 *
7910 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007911 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007912 len = min(len, em->len - (start - em->start));
7913 lockstart = start + len;
7914 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007915 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007916
7917 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7918 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7919 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007920 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007921 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007922
7923 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7924 type = BTRFS_ORDERED_PREALLOC;
7925 else
7926 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007927 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007928 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007929
Josef Bacik00361582013-08-14 14:02:47 -04007930 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007931 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007932 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007933 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007934
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007935 em2 = btrfs_create_dio_extent(inode, start, len,
7936 orig_start, block_start,
7937 len, orig_block_len,
7938 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007939 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007940 if (type == BTRFS_ORDERED_PREALLOC) {
7941 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007942 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007943 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007944 if (em2 && IS_ERR(em2)) {
7945 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007946 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007947 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007948 /*
7949 * For inode marked NODATACOW or extent marked PREALLOC,
7950 * use the existing or preallocated extent, so does not
7951 * need to adjust btrfs_space_info's bytes_may_use.
7952 */
7953 btrfs_free_reserved_data_space_noquota(inode,
7954 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007955 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007956 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007957 }
Josef Bacik00361582013-08-14 14:02:47 -04007958
Chris Mason46bfbb52010-05-26 11:04:10 -04007959 /*
7960 * this will cow the extent, reset the len in case we changed
7961 * it above
7962 */
7963 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007964 free_extent_map(em);
7965 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007966 if (IS_ERR(em)) {
7967 ret = PTR_ERR(em);
7968 goto unlock_err;
7969 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007970 len = min(len, em->len - (start - em->start));
7971unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007972 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7973 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007974 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007975 bh_result->b_bdev = em->bdev;
7976 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007977 if (create) {
7978 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7979 set_buffer_new(bh_result);
7980
7981 /*
7982 * Need to update the i_size under the extent lock so buffered
7983 * readers will get the updated i_size when we unlock.
7984 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007985 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007986 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007987
Filipe Manana9c9464c2015-11-04 09:52:04 +00007988 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307989 WARN_ON(dio_data->reserve < len);
7990 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007991 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307992 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007993 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007994
Josef Bacikeb838e72012-07-31 16:28:48 -04007995 /*
7996 * In the case of write we need to clear and unlock the entire range,
7997 * in the case of read we need to unlock only the end area that we
7998 * aren't using if there is any left over space.
7999 */
Liu Bo24c03fa2012-08-22 20:10:38 -06008000 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00008001 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
8002 lockend, unlock_bits, 1, 0,
8003 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06008004 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04008005 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06008006 }
Josef Bacikeb838e72012-07-31 16:28:48 -04008007
Josef Bacik4b46fce2010-05-23 11:00:55 -04008008 free_extent_map(em);
8009
8010 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04008011
8012unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04008013 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
8014 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00008015err:
chandan50745b02015-08-28 21:10:13 +05308016 if (dio_data)
8017 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00008018 /*
8019 * Compensate the delalloc release we do in btrfs_direct_IO() when we
8020 * write less data then expected, so that we don't underflow our inode's
8021 * outstanding extents counter.
8022 */
8023 if (create && dio_data)
8024 adjust_dio_outstanding_extents(inode, dio_data, len);
8025
Josef Bacikeb838e72012-07-31 16:28:48 -04008026 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008027}
8028
Omar Sandoval58efbc92017-08-22 23:45:59 -07008029static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
8030 struct bio *bio,
8031 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08008032{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008033 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008034 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008035
Mike Christie37226b22016-06-05 14:31:52 -05008036 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008037
8038 bio_get(bio);
8039
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008040 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08008041 if (ret)
8042 goto err;
8043
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008044 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008045err:
8046 bio_put(bio);
8047 return ret;
8048}
8049
8050static int btrfs_check_dio_repairable(struct inode *inode,
8051 struct bio *failed_bio,
8052 struct io_failure_record *failrec,
8053 int failed_mirror)
8054{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008055 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08008056 int num_copies;
8057
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008058 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08008059 if (num_copies == 1) {
8060 /*
8061 * we only have a single copy of the data, so don't bother with
8062 * all the retry and error correction code that follows. no
8063 * matter what the error is, it is very likely to persist.
8064 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008065 btrfs_debug(fs_info,
8066 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
8067 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008068 return 0;
8069 }
8070
8071 failrec->failed_mirror = failed_mirror;
8072 failrec->this_mirror++;
8073 if (failrec->this_mirror == failed_mirror)
8074 failrec->this_mirror++;
8075
8076 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008077 btrfs_debug(fs_info,
8078 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
8079 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008080 return 0;
8081 }
8082
8083 return 1;
8084}
8085
Omar Sandoval58efbc92017-08-22 23:45:59 -07008086static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
8087 struct page *page, unsigned int pgoff,
8088 u64 start, u64 end, int failed_mirror,
8089 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08008090{
8091 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04008092 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8093 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008094 struct bio *bio;
8095 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02008096 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07008097 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08008098 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008099 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008100
Mike Christie37226b22016-06-05 14:31:52 -05008101 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008102
8103 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8104 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07008105 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08008106
8107 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8108 failed_mirror);
8109 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008110 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008111 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08008112 }
8113
Liu Bo17347ce2017-05-15 15:33:27 -07008114 segs = bio_segments(failed_bio);
8115 if (segs > 1 ||
8116 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008117 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008118
8119 isector = start - btrfs_io_bio(failed_bio)->logical;
8120 isector >>= inode->i_sb->s_blocksize_bits;
8121 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308122 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05008123 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008124
8125 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02008126 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08008127 read_mode, failrec->this_mirror, failrec->in_validation);
8128
Omar Sandoval58efbc92017-08-22 23:45:59 -07008129 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
8130 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04008131 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008132 bio_put(bio);
8133 }
8134
Omar Sandoval58efbc92017-08-22 23:45:59 -07008135 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08008136}
8137
8138struct btrfs_retry_complete {
8139 struct completion done;
8140 struct inode *inode;
8141 u64 start;
8142 int uptodate;
8143};
8144
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008145static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008146{
8147 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008148 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008149 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008150 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008151 int i;
8152
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008153 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008154 goto end;
8155
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308156 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008157 io_tree = &BTRFS_I(inode)->io_tree;
8158 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8159 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308160
Miao Xie8b110e32014-09-12 18:44:03 +08008161 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008162 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008163 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008164 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8165 io_tree, done->start, bvec->bv_page,
8166 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008167end:
8168 complete(&done->done);
8169 bio_put(bio);
8170}
8171
Omar Sandoval58efbc92017-08-22 23:45:59 -07008172static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8173 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008174{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308175 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008176 struct bio_vec bvec;
8177 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008178 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008179 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308180 unsigned int pgoff;
8181 u32 sectorsize;
8182 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008183 blk_status_t ret;
8184 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008185
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308186 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008187 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308188
Miao Xiec1dc0892014-09-12 18:43:56 +08008189 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008190 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008191 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008192
Liu Bo17347ce2017-05-15 15:33:27 -07008193 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8194 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8195 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308196
8197next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008198 done.uptodate = 0;
8199 done.start = start;
8200 init_completion(&done.done);
8201
Liu Bo17347ce2017-05-15 15:33:27 -07008202 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308203 pgoff, start, start + sectorsize - 1,
8204 io_bio->mirror_num,
8205 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008206 if (ret) {
8207 err = ret;
8208 goto next;
8209 }
Miao Xie8b110e32014-09-12 18:44:03 +08008210
David Sterba9c17f6c2017-07-19 19:26:45 +02008211 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008212
8213 if (!done.uptodate) {
8214 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308215 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008216 }
8217
Liu Bo629ebf42017-05-15 17:20:07 -07008218next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308219 start += sectorsize;
8220
Liu Bo97bf5a52017-04-07 13:11:10 -07008221 nr_sectors--;
8222 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308223 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008224 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308225 goto next_block_or_try_again;
8226 }
Miao Xie8b110e32014-09-12 18:44:03 +08008227 }
8228
Liu Bo629ebf42017-05-15 17:20:07 -07008229 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008230}
8231
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008232static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008233{
8234 struct btrfs_retry_complete *done = bio->bi_private;
8235 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008236 struct extent_io_tree *io_tree, *failure_tree;
8237 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008238 struct bio_vec *bvec;
8239 int uptodate;
8240 int ret;
8241 int i;
8242
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008243 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008244 goto end;
8245
8246 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308247
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308248 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008249 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308250
Josef Bacik7870d082017-05-05 11:57:15 -04008251 io_tree = &BTRFS_I(inode)->io_tree;
8252 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8253
David Sterbac09abff2017-07-13 18:10:07 +02008254 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008255 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008256 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8257 bvec->bv_offset, done->start,
8258 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008259 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008260 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8261 failure_tree, io_tree, done->start,
8262 bvec->bv_page,
8263 btrfs_ino(BTRFS_I(inode)),
8264 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008265 else
8266 uptodate = 0;
8267 }
8268
8269 done->uptodate = uptodate;
8270end:
8271 complete(&done->done);
8272 bio_put(bio);
8273}
8274
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008275static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8276 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008277{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308278 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008279 struct bio_vec bvec;
8280 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008281 struct btrfs_retry_complete done;
8282 u64 start;
8283 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308284 u32 sectorsize;
8285 int nr_sectors;
8286 unsigned int pgoff;
8287 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008288 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008289 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008290 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008291
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308292 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008293 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308294
Omar Sandoval58efbc92017-08-22 23:45:59 -07008295 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008296 start = io_bio->logical;
8297 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008298 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008299
Liu Bo17347ce2017-05-15 15:33:27 -07008300 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8301 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308302
Liu Bo17347ce2017-05-15 15:33:27 -07008303 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308304next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008305 if (uptodate) {
8306 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8307 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8308 bvec.bv_page, pgoff, start, sectorsize);
8309 if (likely(!ret))
8310 goto next;
8311 }
Miao Xie8b110e32014-09-12 18:44:03 +08008312try_again:
8313 done.uptodate = 0;
8314 done.start = start;
8315 init_completion(&done.done);
8316
Omar Sandoval58efbc92017-08-22 23:45:59 -07008317 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8318 pgoff, start, start + sectorsize - 1,
8319 io_bio->mirror_num, btrfs_retry_endio,
8320 &done);
8321 if (status) {
8322 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008323 goto next;
8324 }
8325
David Sterba9c17f6c2017-07-19 19:26:45 +02008326 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008327
8328 if (!done.uptodate) {
8329 /* We might have another mirror, so try again */
8330 goto try_again;
8331 }
8332next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308333 offset += sectorsize;
8334 start += sectorsize;
8335
8336 ASSERT(nr_sectors);
8337
Liu Bo97bf5a52017-04-07 13:11:10 -07008338 nr_sectors--;
8339 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308340 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008341 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308342 goto next_block;
8343 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008344 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008345
8346 return err;
8347}
8348
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008349static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8350 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008351{
8352 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8353
8354 if (skip_csum) {
8355 if (unlikely(err))
8356 return __btrfs_correct_data_nocsum(inode, io_bio);
8357 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008358 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008359 } else {
8360 return __btrfs_subio_endio_read(inode, io_bio, err);
8361 }
8362}
8363
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008364static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008365{
8366 struct btrfs_dio_private *dip = bio->bi_private;
8367 struct inode *inode = dip->inode;
8368 struct bio *dio_bio;
8369 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008370 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008371
Liu Bo99c4e3b92017-09-15 15:06:51 -06008372 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008373 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008374
Josef Bacik4b46fce2010-05-23 11:00:55 -04008375 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008376 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008377 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008378
Josef Bacik4b46fce2010-05-23 11:00:55 -04008379 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008380
Liu Bo99c4e3b92017-09-15 15:06:51 -06008381 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008382 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008383
8384 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008385 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008386 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008387}
8388
Qu Wenruo524272602017-03-08 10:25:52 +08008389static void __endio_write_update_ordered(struct inode *inode,
8390 const u64 offset, const u64 bytes,
8391 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008392{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008393 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008394 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008395 struct btrfs_workqueue *wq;
8396 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008397 u64 ordered_offset = offset;
8398 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008399 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008400 int ret;
8401
Qu Wenruo524272602017-03-08 10:25:52 +08008402 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8403 wq = fs_info->endio_freespace_worker;
8404 func = btrfs_freespace_write_helper;
8405 } else {
8406 wq = fs_info->endio_write_workers;
8407 func = btrfs_endio_write_helper;
8408 }
8409
Chris Mason163cf092010-11-28 19:56:33 -05008410again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008411 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008412 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8413 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008414 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008415 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008416 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008417 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008418
Qu Wenruo524272602017-03-08 10:25:52 +08008419 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8420 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008421out_test:
8422 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008423 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8424 * in the range, we can exit.
8425 */
8426 if (ordered_offset == last_offset)
8427 return;
8428 /*
Chris Mason163cf092010-11-28 19:56:33 -05008429 * our bio might span multiple ordered extents. If we haven't
8430 * completed the accounting for the whole dio, go back and try again
8431 */
Filipe Manana14543772015-11-24 16:23:54 +00008432 if (ordered_offset < offset + bytes) {
8433 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008434 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008435 goto again;
8436 }
Filipe Manana14543772015-11-24 16:23:54 +00008437}
8438
8439static void btrfs_endio_direct_write(struct bio *bio)
8440{
8441 struct btrfs_dio_private *dip = bio->bi_private;
8442 struct bio *dio_bio = dip->dio_bio;
8443
Qu Wenruo524272602017-03-08 10:25:52 +08008444 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008445 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008446
Josef Bacik4b46fce2010-05-23 11:00:55 -04008447 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008448
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008449 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008450 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008451 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008452}
8453
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008454static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008455 struct bio *bio, int mirror_num,
8456 unsigned long bio_flags, u64 offset)
8457{
Josef Bacikc6100a42017-05-05 11:57:13 -04008458 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008459 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008460 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008461 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008462 return 0;
8463}
8464
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008465static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008466{
8467 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008468 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008469
Miao Xie8b110e32014-09-12 18:44:03 +08008470 if (err)
8471 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008472 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008473 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8474 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008475 (unsigned long long)bio->bi_iter.bi_sector,
8476 bio->bi_iter.bi_size, err);
8477
8478 if (dip->subio_endio)
8479 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008480
8481 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008482 dip->errors = 1;
8483
8484 /*
8485 * before atomic variable goto zero, we must make sure
8486 * dip->errors is perceived to be set.
8487 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008488 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008489 }
8490
8491 /* if there are more bios still pending for this dio, just exit */
8492 if (!atomic_dec_and_test(&dip->pending_bios))
8493 goto out;
8494
Chris Mason9be33952013-05-17 18:30:14 -04008495 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008496 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008497 } else {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008498 dip->dio_bio->bi_status = 0;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008499 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008500 }
8501out:
8502 bio_put(bio);
8503}
8504
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008505static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008506 struct btrfs_dio_private *dip,
8507 struct bio *bio,
8508 u64 file_offset)
8509{
8510 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8511 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008512 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008513
8514 /*
8515 * We load all the csum data we need when we submit
8516 * the first bio to reduce the csum tree search and
8517 * contention.
8518 */
8519 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008520 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008521 file_offset);
8522 if (ret)
8523 return ret;
8524 }
8525
8526 if (bio == dip->orig_bio)
8527 return 0;
8528
8529 file_offset -= dip->logical_offset;
8530 file_offset >>= inode->i_sb->s_blocksize_bits;
8531 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8532
8533 return 0;
8534}
8535
Omar Sandoval58efbc92017-08-22 23:45:59 -07008536static inline blk_status_t
8537__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008538 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008539{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008540 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008541 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008542 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008543 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008544
Josef Bacikb812ce22012-11-16 13:56:32 -05008545 if (async_submit)
8546 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8547
Miao Xiee65e1532010-11-22 03:04:43 +00008548 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008549
8550 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008551 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008552 if (ret)
8553 goto err;
8554 }
Miao Xiee65e1532010-11-22 03:04:43 +00008555
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008556 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008557 goto map;
8558
8559 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008560 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8561 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008562 __btrfs_submit_bio_start_direct_io,
8563 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008564 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008565 } else if (write) {
8566 /*
8567 * If we aren't doing async submit, calculate the csum of the
8568 * bio now.
8569 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008570 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008571 if (ret)
8572 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008573 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008574 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008575 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008576 if (ret)
8577 goto err;
8578 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008579map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008580 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008581err:
8582 bio_put(bio);
8583 return ret;
8584}
8585
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008586static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008587{
8588 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008589 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008590 struct bio *bio;
8591 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008592 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008593 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008594 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008595 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008596 u64 submit_len;
8597 int clone_offset = 0;
8598 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308599 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008600 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008601
Kent Overstreet4f024f32013-10-11 15:44:27 -07008602 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008603 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008604 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8605 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008606 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008607 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008608
Liu Bo725130b2017-05-16 09:51:39 -07008609 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008610 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008611 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008612 goto submit;
8613 }
8614
David Woodhouse53b381b2013-01-29 18:40:14 -05008615 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008616 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008617 async_submit = 0;
8618 else
8619 async_submit = 1;
8620
Liu Bo725130b2017-05-16 09:51:39 -07008621 /* bio split */
8622 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008623 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008624 do {
Liu Bo725130b2017-05-16 09:51:39 -07008625 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008626
Liu Bo725130b2017-05-16 09:51:39 -07008627 /*
8628 * This will never fail as it's passing GPF_NOFS and
8629 * the allocation is backed by btrfs_bioset.
8630 */
Liu Boe4770942017-05-16 10:57:14 -07008631 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008632 clone_len);
8633 bio->bi_private = dip;
8634 bio->bi_end_io = btrfs_end_dio_bio;
8635 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008636
Liu Bo725130b2017-05-16 09:51:39 -07008637 ASSERT(submit_len >= clone_len);
8638 submit_len -= clone_len;
8639 if (submit_len == 0)
8640 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008641
Liu Bo725130b2017-05-16 09:51:39 -07008642 /*
8643 * Increase the count before we submit the bio so we know
8644 * the end IO handler won't happen before we increase the
8645 * count. Otherwise, the dip might get freed before we're
8646 * done setting it up.
8647 */
8648 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008649
Linus Torvalds66ba7722017-09-09 13:27:51 -07008650 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008651 async_submit);
8652 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008653 bio_put(bio);
8654 atomic_dec(&dip->pending_bios);
8655 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008656 }
Liu Bo725130b2017-05-16 09:51:39 -07008657
8658 clone_offset += clone_len;
8659 start_sector += clone_len >> 9;
8660 file_offset += clone_len;
8661
8662 map_length = submit_len;
8663 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8664 start_sector << 9, &map_length, NULL, 0);
8665 if (ret)
8666 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008667 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008668
Josef Bacik02f57c72011-04-06 14:25:44 -04008669submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008670 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008671 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008672 return 0;
8673
8674 bio_put(bio);
8675out_err:
8676 dip->errors = 1;
8677 /*
8678 * before atomic variable goto zero, we must
8679 * make sure dip->errors is perceived to be set.
8680 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008681 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008682 if (atomic_dec_and_test(&dip->pending_bios))
8683 bio_io_error(dip->orig_bio);
8684
8685 /* bio_end_io() will handle error, so we needn't return it */
8686 return 0;
8687}
8688
Mike Christie8a4c1e42016-06-05 14:31:50 -05008689static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8690 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008691{
Filipe Manana61de7182015-07-01 12:13:10 +01008692 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008693 struct bio *bio = NULL;
8694 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008695 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008696 int ret = 0;
8697
David Sterba8b6c1d52017-06-02 17:48:13 +02008698 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008699
Miao Xiec1dc0892014-09-12 18:43:56 +08008700 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008701 if (!dip) {
8702 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008703 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008704 }
8705
8706 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008707 dip->inode = inode;
8708 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008709 dip->bytes = dio_bio->bi_iter.bi_size;
8710 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008711 bio->bi_private = dip;
8712 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008713 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008714 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008715 io_bio = btrfs_io_bio(bio);
8716 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008717
Miao Xiec1dc0892014-09-12 18:43:56 +08008718 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008719 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008720 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008721 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008722 dip->subio_endio = btrfs_subio_endio_read;
8723 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008724
Filipe Mananaf28a4922015-12-08 19:23:20 +00008725 /*
8726 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8727 * even if we fail to submit a bio, because in such case we do the
8728 * corresponding error handling below and it must not be done a second
8729 * time by btrfs_direct_IO().
8730 */
8731 if (write) {
8732 struct btrfs_dio_data *dio_data = current->journal_info;
8733
8734 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8735 dip->bytes;
8736 dio_data->unsubmitted_oe_range_start =
8737 dio_data->unsubmitted_oe_range_end;
8738 }
8739
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008740 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008741 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008742 return;
Chris Mason9be33952013-05-17 18:30:14 -04008743
Liu Bo3892ac92017-04-17 15:00:28 -07008744 if (io_bio->end_io)
8745 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008746
Josef Bacik4b46fce2010-05-23 11:00:55 -04008747free_ordered:
8748 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008749 * If we arrived here it means either we failed to submit the dip
8750 * or we either failed to clone the dio_bio or failed to allocate the
8751 * dip. If we cloned the dio_bio and allocated the dip, we can just
8752 * call bio_endio against our io_bio so that we get proper resource
8753 * cleanup if we fail to submit the dip, otherwise, we must do the
8754 * same as btrfs_endio_direct_[write|read] because we can't call these
8755 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008756 */
Liu Bo3892ac92017-04-17 15:00:28 -07008757 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008758 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008759 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008760 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008761 * and all the cleanup and final put for dio_bio (through
8762 * dio_end_io()).
8763 */
8764 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008765 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008766 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008767 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008768 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008769 file_offset,
8770 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008771 false);
Filipe Manana14543772015-11-24 16:23:54 +00008772 else
Filipe Manana61de7182015-07-01 12:13:10 +01008773 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8774 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008775
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008776 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008777 /*
8778 * Releases and cleans up our dio_bio, no need to bio_put()
8779 * nor bio_endio()/bio_io_error() against dio_bio.
8780 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008781 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008782 }
Liu Bo3892ac92017-04-17 15:00:28 -07008783 if (bio)
8784 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008785 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008786}
8787
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008788static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8789 struct kiocb *iocb,
8790 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008791{
8792 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008793 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008794 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008795 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008796
8797 if (offset & blocksize_mask)
8798 goto out;
8799
Al Viro28060d52014-03-22 05:15:17 -04008800 if (iov_iter_alignment(iter) & blocksize_mask)
8801 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008802
Al Viro28060d52014-03-22 05:15:17 -04008803 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008804 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008805 return 0;
8806 /*
8807 * Check to make sure we don't have duplicate iov_base's in this
8808 * iovec, if so return EINVAL, otherwise we'll get csum errors
8809 * when reading back.
8810 */
8811 for (seg = 0; seg < iter->nr_segs; seg++) {
8812 for (i = seg + 1; i < iter->nr_segs; i++) {
8813 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008814 goto out;
8815 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008816 }
8817 retval = 0;
8818out:
8819 return retval;
8820}
Josef Bacikeb838e72012-07-31 16:28:48 -04008821
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008822static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008823{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008824 struct file *file = iocb->ki_filp;
8825 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008826 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308827 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008828 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008829 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008830 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008831 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008832 bool wakeup = true;
8833 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008834 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008835
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008836 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008837 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008838
Jens Axboefe0f07d2015-04-15 17:05:48 -06008839 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008840
Josef Bacik0e267c42013-07-02 10:38:02 -04008841 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008842 * The generic stuff only does filemap_write_and_wait_range, which
8843 * isn't enough if we've written compressed pages to this area, so
8844 * we need to flush the dirty pages again to make absolutely sure
8845 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008846 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008847 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008848 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8849 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008850 filemap_fdatawrite_range(inode->i_mapping, offset,
8851 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008852
Omar Sandoval6f673762015-03-16 04:33:52 -07008853 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008854 /*
8855 * If the write DIO is beyond the EOF, we need update
8856 * the isize, but it is protected by i_mutex. So we can
8857 * not unlock the i_mutex at this case.
8858 */
8859 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008860 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008861 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008862 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008863 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8864 ret = -EAGAIN;
8865 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008866 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008867 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8868 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008869 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008870 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008871 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008872
8873 /*
8874 * We need to know how many extents we reserved so that we can
8875 * do the accounting properly if we go over the number we
8876 * originally calculated. Abuse current->journal_info for this.
8877 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008878 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008879 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008880 dio_data.unsubmitted_oe_range_start = (u64)offset;
8881 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308882 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308883 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008884 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8885 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008886 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008887 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8888 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008889 }
8890
Omar Sandoval17f8c842015-03-16 04:33:50 -07008891 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008892 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008893 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008894 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008895 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308896 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008897 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008898 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308899 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008900 btrfs_delalloc_release_space(inode, data_reserved,
8901 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008902 /*
8903 * On error we might have left some ordered extents
8904 * without submitting corresponding bios for them, so
8905 * cleanup them up to avoid other tasks getting them
8906 * and waiting for them to complete forever.
8907 */
8908 if (dio_data.unsubmitted_oe_range_start <
8909 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008910 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008911 dio_data.unsubmitted_oe_range_start,
8912 dio_data.unsubmitted_oe_range_end -
8913 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008914 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008915 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008916 btrfs_delalloc_release_space(inode, data_reserved,
8917 offset, count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008918 }
Miao Xie38851cc2013-02-08 07:04:11 +00008919out:
Miao Xie2e60a512013-02-08 07:01:08 +00008920 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008921 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008922 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008923 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008924
Qu Wenruo364ecf32017-02-27 15:10:38 +08008925 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008926 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008927}
8928
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008929#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8930
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008931static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8932 __u64 start, __u64 len)
8933{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008934 int ret;
8935
8936 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8937 if (ret)
8938 return ret;
8939
Chris Masonec29ed52011-02-23 16:23:20 -05008940 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008941}
8942
Chris Mason9ebefb182007-06-15 13:50:00 -04008943int btrfs_readpage(struct file *file, struct page *page)
8944{
Chris Masond1310b22008-01-24 16:13:08 -05008945 struct extent_io_tree *tree;
8946 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008947 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008948}
Chris Mason1832a6d2007-12-21 16:27:21 -05008949
Chris Mason39279cc2007-06-12 06:35:45 -04008950static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8951{
Chris Masond1310b22008-01-24 16:13:08 -05008952 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008953 struct inode *inode = page->mapping->host;
8954 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008955
8956 if (current->flags & PF_MEMALLOC) {
8957 redirty_page_for_writepage(wbc, page);
8958 unlock_page(page);
8959 return 0;
8960 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008961
8962 /*
8963 * If we are under memory pressure we will call this directly from the
8964 * VM, we need to make sure we have the inode referenced for the ordered
8965 * extent. If not just return like we didn't do anything.
8966 */
8967 if (!igrab(inode)) {
8968 redirty_page_for_writepage(wbc, page);
8969 return AOP_WRITEPAGE_ACTIVATE;
8970 }
Chris Masond1310b22008-01-24 16:13:08 -05008971 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008972 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8973 btrfs_add_delayed_iput(inode);
8974 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008975}
Chris Mason39279cc2007-06-12 06:35:45 -04008976
Eric Sandeen48a3b632013-04-25 20:41:01 +00008977static int btrfs_writepages(struct address_space *mapping,
8978 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008979{
Chris Masond1310b22008-01-24 16:13:08 -05008980 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008981
Chris Masond1310b22008-01-24 16:13:08 -05008982 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008983 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8984}
8985
Chris Mason3ab2fb52007-11-08 10:59:22 -05008986static int
8987btrfs_readpages(struct file *file, struct address_space *mapping,
8988 struct list_head *pages, unsigned nr_pages)
8989{
Chris Masond1310b22008-01-24 16:13:08 -05008990 struct extent_io_tree *tree;
8991 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008992 return extent_readpages(tree, mapping, pages, nr_pages,
8993 btrfs_get_extent);
8994}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008995static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008996{
Chris Masond1310b22008-01-24 16:13:08 -05008997 struct extent_io_tree *tree;
8998 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008999 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009000
Chris Masond1310b22008-01-24 16:13:08 -05009001 tree = &BTRFS_I(page->mapping->host)->io_tree;
9002 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05009003 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04009004 if (ret == 1) {
9005 ClearPagePrivate(page);
9006 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009007 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04009008 }
9009 return ret;
9010}
Chris Mason39279cc2007-06-12 06:35:45 -04009011
Chris Masone6dcd2d2008-07-17 12:53:50 -04009012static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
9013{
Chris Mason98509cf2008-09-11 15:51:43 -04009014 if (PageWriteback(page) || PageDirty(page))
9015 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01009016 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009017}
9018
Lukas Czernerd47992f2013-05-21 23:17:23 -04009019static void btrfs_invalidatepage(struct page *page, unsigned int offset,
9020 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04009021{
Josef Bacik5fd02042012-05-02 14:00:54 -04009022 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05009023 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009024 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009025 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009026 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009027 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309028 u64 start;
9029 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009030 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04009031
Chris Mason8b62b722009-09-02 16:53:46 -04009032 /*
9033 * we have the page locked, so new writeback can't start,
9034 * and the dirty bit won't be cleared while we are here.
9035 *
9036 * Wait for IO on this page so that we can safely clear
9037 * the PagePrivate2 bit and do ordered accounting
9038 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009039 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04009040
Josef Bacik5fd02042012-05-02 14:00:54 -04009041 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009042 if (offset) {
9043 btrfs_releasepage(page, GFP_NOFS);
9044 return;
9045 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009046
9047 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01009048 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309049again:
9050 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009051 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309052 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009053 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309054 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04009055 /*
9056 * IO on this page will never be started, so we need
9057 * to account for any ordered extents now
9058 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009059 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309060 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009061 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009062 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009063 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
9064 EXTENT_DEFRAG, 1, 0, &cached_state,
9065 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04009066 /*
9067 * whoever cleared the private bit is responsible
9068 * for the finish_ordered_io
9069 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04009070 if (TestClearPagePrivate2(page)) {
9071 struct btrfs_ordered_inode_tree *tree;
9072 u64 new_len;
9073
9074 tree = &BTRFS_I(inode)->ordered_tree;
9075
9076 spin_lock_irq(&tree->lock);
9077 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309078 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04009079 if (new_len < ordered->truncated_len)
9080 ordered->truncated_len = new_len;
9081 spin_unlock_irq(&tree->lock);
9082
9083 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309084 start,
9085 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04009086 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04009087 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009088 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009089 if (!inode_evicting) {
9090 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309091 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009092 &cached_state);
9093 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309094
9095 start = end + 1;
9096 if (start < page_end)
9097 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009098 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009099
Qu Wenruob9d0b382015-09-29 10:35:16 +08009100 /*
9101 * Qgroup reserved space handler
9102 * Page here will be either
9103 * 1) Already written to disk
9104 * In this case, its reserved space is released from data rsv map
9105 * and will be freed by delayed_ref handler finally.
9106 * So even we call qgroup_free_data(), it won't decrease reserved
9107 * space.
9108 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009109 * This means the reserved space should be freed here. However,
9110 * if a truncate invalidates the page (by clearing PageDirty)
9111 * and the page is accounted for while allocating extent
9112 * in btrfs_check_data_free_space() we let delayed_ref to
9113 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009114 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009115 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08009116 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009117 if (!inode_evicting) {
9118 clear_extent_bit(tree, page_start, page_end,
9119 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009120 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9121 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009122 &cached_state, GFP_NOFS);
9123
9124 __btrfs_releasepage(page, GFP_NOFS);
9125 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009126
Chris Mason4a096752008-07-21 10:29:44 -04009127 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009128 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009129 ClearPagePrivate(page);
9130 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009131 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009132 }
Chris Mason39279cc2007-06-12 06:35:45 -04009133}
9134
Chris Mason9ebefb182007-06-15 13:50:00 -04009135/*
9136 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9137 * called from a page fault handler when a page is first dirtied. Hence we must
9138 * be careful to check for EOF conditions here. We set the page up correctly
9139 * for a written page which means we get ENOSPC checking when writing into
9140 * holes and correct delalloc and unwritten extent mapping on filesystems that
9141 * support these features.
9142 *
9143 * We are not allowed to take the i_mutex here so we have to play games to
9144 * protect against truncate races as the page could now be beyond EOF. Because
9145 * vmtruncate() writes the inode size before removing pages, once we have the
9146 * page lock we can determine safely if the page is beyond EOF. If it is not
9147 * beyond EOF, then the page is guaranteed safe against truncation until we
9148 * unlock the page.
9149 */
Dave Jiang11bac802017-02-24 14:56:41 -08009150int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009151{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009152 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009153 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009154 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009155 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9156 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009157 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009158 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009159 char *kaddr;
9160 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009161 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009162 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009163 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309164 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009165 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009166 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309167 u64 end;
9168
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009169 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009170
Jan Karab2b5ef52012-06-12 16:20:45 +02009171 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009172 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009173 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309174 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009175
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309176 /*
9177 * Reserving delalloc space after obtaining the page lock can lead to
9178 * deadlock. For example, if a dirty page is locked by this function
9179 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9180 * dirty page write out, then the btrfs_writepage() function could
9181 * end up waiting indefinitely to get a lock on the page currently
9182 * being processed by btrfs_page_mkwrite() function.
9183 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009184 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309185 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009186 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009187 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009188 reserved = 1;
9189 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009190 if (ret) {
9191 if (ret == -ENOMEM)
9192 ret = VM_FAULT_OOM;
9193 else /* -ENOSPC, -EIO, etc */
9194 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009195 if (reserved)
9196 goto out;
9197 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009198 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009199
Nick Piggin56a76f82009-03-31 15:23:23 -07009200 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009201again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009202 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009203 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009204
Chris Mason9ebefb182007-06-15 13:50:00 -04009205 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009206 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009207 /* page got truncated out from underneath us */
9208 goto out_unlock;
9209 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009210 wait_on_page_writeback(page);
9211
David Sterbaff13db42015-12-03 14:30:40 +01009212 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009213 set_page_extent_mapped(page);
9214
Chris Masoneb84ae02008-07-17 13:53:27 -04009215 /*
9216 * we can't set the delalloc bits if there are pending ordered
9217 * extents. Drop our locks and wait for them to finish
9218 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009219 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9220 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009221 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009222 unlock_extent_cached(io_tree, page_start, page_end,
9223 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009224 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009225 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009226 btrfs_put_ordered_extent(ordered);
9227 goto again;
9228 }
9229
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009230 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009231 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009232 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009233 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309234 end = page_start + reserved_space - 1;
9235 spin_lock(&BTRFS_I(inode)->lock);
9236 BTRFS_I(inode)->outstanding_extents++;
9237 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009238 btrfs_delalloc_release_space(inode, data_reserved,
9239 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309240 }
9241 }
9242
Josef Bacikfbf19082009-10-01 17:10:23 -04009243 /*
Liu Bo54160342016-12-13 12:15:19 -08009244 * page_mkwrite gets called when the page is firstly dirtied after it's
9245 * faulted in, but write(2) could also dirty a page and set delalloc
9246 * bits, thus in this case for space account reason, we still need to
9247 * clear any delalloc bits within this page range since we have to
9248 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009249 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309250 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009251 EXTENT_DIRTY | EXTENT_DELALLOC |
9252 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009253 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009254
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309255 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009256 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009257 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009258 unlock_extent_cached(io_tree, page_start, page_end,
9259 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009260 ret = VM_FAULT_SIGBUS;
9261 goto out_unlock;
9262 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009263 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009264
9265 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009266 if (page_start + PAGE_SIZE > size)
9267 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009268 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009269 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009270
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009271 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009272 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009273 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009274 flush_dcache_page(page);
9275 kunmap(page);
9276 }
Chris Mason247e7432008-07-17 12:53:51 -04009277 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009278 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009279 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009280
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009281 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009282 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009283 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009284
Josef Bacik2ac55d42010-02-03 19:33:23 +00009285 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009286
9287out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009288 if (!ret) {
9289 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009290 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009291 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009292 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009293 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009294out:
Qu Wenruobc42bda2017-02-27 15:10:39 +08009295 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9296 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009297out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009298 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009299 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009300 return ret;
9301}
9302
Josef Bacika41ad392011-01-31 15:30:16 -05009303static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009304{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009305 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009306 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009307 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009308 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009309 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009310 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009311 u64 mask = fs_info->sectorsize - 1;
9312 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009313
Josef Bacik0ef8b722013-10-25 16:13:35 -04009314 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9315 (u64)-1);
9316 if (ret)
9317 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009318
Josef Bacikfcb80c22011-05-03 10:40:22 -04009319 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009320 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009321 * 3 things going on here
9322 *
9323 * 1) We need to reserve space for our orphan item and the space to
9324 * delete our orphan item. Lord knows we don't want to have a dangling
9325 * orphan item because we didn't reserve space to remove it.
9326 *
9327 * 2) We need to reserve space to update our inode.
9328 *
9329 * 3) We need to have something to cache all the space that is going to
9330 * be free'd up by the truncate operation, but also have some slack
9331 * space reserved in case it uses space during the truncate (thank you
9332 * very much snapshotting).
9333 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009334 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009335 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009336 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009337 * doesn't end up using space reserved for updating the inode or
9338 * removing the orphan item. We also need to be able to stop the
9339 * transaction and start a new one, which means we need to be able to
9340 * update the inode several times, and we have no idea of knowing how
9341 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009342 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009343 * Then there is the orphan item, which does indeed need to be held on
9344 * to for the whole operation, and we need nobody to touch this reserved
9345 * space except the orphan code.
9346 *
9347 * So that leaves us with
9348 *
9349 * 1) root->orphan_block_rsv - for the orphan deletion.
9350 * 2) rsv - for the truncate reservation, which we will steal from the
9351 * transaction reservation.
9352 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9353 * updating the inode.
9354 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009355 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009356 if (!rsv)
9357 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009358 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009359 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009360
Josef Bacik907cbce2011-08-08 13:46:15 -04009361 /*
Josef Bacik07127182011-08-19 10:29:59 -04009362 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009363 * 1 for updating the inode.
9364 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009365 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009366 if (IS_ERR(trans)) {
9367 err = PTR_ERR(trans);
9368 goto out;
9369 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009370
Josef Bacik907cbce2011-08-08 13:46:15 -04009371 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009372 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009373 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009374 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009375
Chris Mason5a3f23d2009-03-31 13:27:11 -04009376 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009377 * So if we truncate and then write and fsync we normally would just
9378 * write the extents that changed, which is a problem if we need to
9379 * first truncate that entire inode. So set this flag so we write out
9380 * all of the extents in the inode to the sync log so we're completely
9381 * safe.
9382 */
9383 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009384 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009385
Yan, Zheng80825102009-11-12 09:35:36 +00009386 while (1) {
9387 ret = btrfs_truncate_inode_items(trans, root, inode,
9388 inode->i_size,
9389 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009390 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009391 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009392 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009393 }
Chris Mason39279cc2007-06-12 06:35:45 -04009394
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009395 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009396 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009397 if (ret) {
9398 err = ret;
9399 break;
9400 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009401
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009402 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009403 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009404
9405 trans = btrfs_start_transaction(root, 2);
9406 if (IS_ERR(trans)) {
9407 ret = err = PTR_ERR(trans);
9408 trans = NULL;
9409 break;
9410 }
9411
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009412 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009413 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009414 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009415 BUG_ON(ret); /* shouldn't happen */
9416 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009417 }
9418
9419 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009420 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009421 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009422 if (ret)
9423 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009424 }
9425
Chris Mason917c16b2011-11-08 14:49:59 -05009426 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009427 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009428 ret = btrfs_update_inode(trans, root, inode);
9429 if (ret && !err)
9430 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009431
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009432 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009433 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009434 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009435out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009436 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009437
Josef Bacik3893e332011-01-31 16:03:11 -05009438 if (ret && !err)
9439 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009440
Josef Bacik3893e332011-01-31 16:03:11 -05009441 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009442}
9443
Sven Wegener3b963622008-06-09 21:57:42 -04009444/*
Chris Masond352ac62008-09-29 15:18:18 -04009445 * create a new subvolume directory/inode (helper for the ioctl).
9446 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009447int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009448 struct btrfs_root *new_root,
9449 struct btrfs_root *parent_root,
9450 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009451{
Chris Mason39279cc2007-06-12 06:35:45 -04009452 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009453 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009454 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009455
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009456 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9457 new_dirid, new_dirid,
9458 S_IFDIR | (~current_umask() & S_IRWXUGO),
9459 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009460 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009461 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009462 inode->i_op = &btrfs_dir_inode_operations;
9463 inode->i_fop = &btrfs_dir_file_operations;
9464
Miklos Szeredibfe86842011-10-28 14:13:29 +02009465 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009466 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009467 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009468
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009469 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9470 if (err)
9471 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009472 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009473 new_root->root_key.objectid, err);
9474
Yan, Zheng76dda932009-09-21 16:00:26 -04009475 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009476
Yan, Zheng76dda932009-09-21 16:00:26 -04009477 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009478 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009479}
9480
Chris Mason39279cc2007-06-12 06:35:45 -04009481struct inode *btrfs_alloc_inode(struct super_block *sb)
9482{
9483 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009484 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009485
9486 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9487 if (!ei)
9488 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009489
9490 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009491 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009492 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009493 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009494 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009495 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009496 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009497 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009498 ei->disk_i_size = 0;
9499 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009500 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009501 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009502 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009503 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009504 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009505 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009506
Josef Bacik9e0baf62011-07-15 15:16:44 +00009507 spin_lock_init(&ei->lock);
9508 ei->outstanding_extents = 0;
9509 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009510
Josef Bacik72ac3c02012-05-23 14:13:11 -04009511 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009512 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009513 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009514
Miao Xie16cdcec2011-04-22 18:12:22 +08009515 ei->delayed_node = NULL;
9516
chandan r9cc97d62012-07-04 12:48:07 +05309517 ei->i_otime.tv_sec = 0;
9518 ei->i_otime.tv_nsec = 0;
9519
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009520 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009521 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009522 extent_io_tree_init(&ei->io_tree, inode);
9523 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009524 ei->io_tree.track_uptodate = 1;
9525 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009526 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009527 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009528 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009529 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009530 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009531 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009532 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009533 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009534
9535 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009536}
9537
Josef Bacikaaedb552013-10-11 14:44:09 -04009538#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9539void btrfs_test_destroy_inode(struct inode *inode)
9540{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009541 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009542 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9543}
9544#endif
9545
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009546static void btrfs_i_callback(struct rcu_head *head)
9547{
9548 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009549 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9550}
9551
Chris Mason39279cc2007-06-12 06:35:45 -04009552void btrfs_destroy_inode(struct inode *inode)
9553{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009554 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009555 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009556 struct btrfs_root *root = BTRFS_I(inode)->root;
9557
Al Virob3d9b7a2012-06-09 13:51:19 -04009558 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009559 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009560 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9561 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009562 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009563 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009564 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009565 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009566
Chris Mason5a3f23d2009-03-31 13:27:11 -04009567 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009568 * This can happen where we create an inode, but somebody else also
9569 * created the same inode and we need to destroy the one we already
9570 * created.
9571 */
9572 if (!root)
9573 goto free;
9574
Josef Bacik8a35d952012-05-23 14:26:42 -04009575 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9576 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009577 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009578 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009579 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009580 }
Josef Bacik7b128762008-07-24 12:17:14 -04009581
Chris Masond3977122009-01-05 21:25:51 -05009582 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009583 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9584 if (!ordered)
9585 break;
9586 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009587 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009588 "found ordered extent %llu %llu on inode cleanup",
9589 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009590 btrfs_remove_ordered_extent(inode, ordered);
9591 btrfs_put_ordered_extent(ordered);
9592 btrfs_put_ordered_extent(ordered);
9593 }
9594 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009595 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009596 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009597 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009598free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009599 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009600}
9601
Al Viro45321ac2010-06-07 13:43:19 -04009602int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009603{
9604 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009605
Naohiro Aota6379ef92013-06-06 09:56:34 +00009606 if (root == NULL)
9607 return 1;
9608
Liu Bofa6ac872013-02-20 14:10:23 +00009609 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009610 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009611 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009612 else
Al Viro45321ac2010-06-07 13:43:19 -04009613 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009614}
9615
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009616static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009617{
9618 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9619
9620 inode_init_once(&ei->vfs_inode);
9621}
9622
9623void btrfs_destroy_cachep(void)
9624{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009625 /*
9626 * Make sure all delayed rcu free inodes are flushed before we
9627 * destroy cache.
9628 */
9629 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009630 kmem_cache_destroy(btrfs_inode_cachep);
9631 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009632 kmem_cache_destroy(btrfs_path_cachep);
9633 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009634}
9635
9636int btrfs_init_cachep(void)
9637{
David Sterba837e1972012-09-07 03:00:48 -06009638 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009639 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009640 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9641 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009642 if (!btrfs_inode_cachep)
9643 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009644
David Sterba837e1972012-09-07 03:00:48 -06009645 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009646 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009647 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009648 if (!btrfs_trans_handle_cachep)
9649 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009650
David Sterba837e1972012-09-07 03:00:48 -06009651 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009652 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009653 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009654 if (!btrfs_path_cachep)
9655 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009656
David Sterba837e1972012-09-07 03:00:48 -06009657 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009658 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009659 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009660 if (!btrfs_free_space_cachep)
9661 goto fail;
9662
Chris Mason39279cc2007-06-12 06:35:45 -04009663 return 0;
9664fail:
9665 btrfs_destroy_cachep();
9666 return -ENOMEM;
9667}
9668
David Howellsa528d352017-01-31 16:46:22 +00009669static int btrfs_getattr(const struct path *path, struct kstat *stat,
9670 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009671{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009672 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009673 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009674 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009675 u32 bi_flags = BTRFS_I(inode)->flags;
9676
9677 stat->result_mask |= STATX_BTIME;
9678 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9679 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9680 if (bi_flags & BTRFS_INODE_APPEND)
9681 stat->attributes |= STATX_ATTR_APPEND;
9682 if (bi_flags & BTRFS_INODE_COMPRESS)
9683 stat->attributes |= STATX_ATTR_COMPRESSED;
9684 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9685 stat->attributes |= STATX_ATTR_IMMUTABLE;
9686 if (bi_flags & BTRFS_INODE_NODUMP)
9687 stat->attributes |= STATX_ATTR_NODUMP;
9688
9689 stat->attributes_mask |= (STATX_ATTR_APPEND |
9690 STATX_ATTR_COMPRESSED |
9691 STATX_ATTR_IMMUTABLE |
9692 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009693
Chris Mason39279cc2007-06-12 06:35:45 -04009694 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009695 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009696
9697 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009698 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009699 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009700 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009701 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009702 return 0;
9703}
9704
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009705static int btrfs_rename_exchange(struct inode *old_dir,
9706 struct dentry *old_dentry,
9707 struct inode *new_dir,
9708 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009709{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009710 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009711 struct btrfs_trans_handle *trans;
9712 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009713 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009714 struct inode *new_inode = new_dentry->d_inode;
9715 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009716 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009717 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009718 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9719 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009720 u64 old_idx = 0;
9721 u64 new_idx = 0;
9722 u64 root_objectid;
9723 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009724 bool root_log_pinned = false;
9725 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009726
9727 /* we only allow rename subvolume link between subvolumes */
9728 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9729 return -EXDEV;
9730
9731 /* close the race window with snapshot create/destroy ioctl */
9732 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009733 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009734 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009735 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009736
9737 /*
9738 * We want to reserve the absolute worst case amount of items. So if
9739 * both inodes are subvols and we need to unlink them then that would
9740 * require 4 item modifications, but if they are both normal inodes it
9741 * would require 5 item modifications, so we'll assume their normal
9742 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9743 * should cover the worst case number of items we'll modify.
9744 */
9745 trans = btrfs_start_transaction(root, 12);
9746 if (IS_ERR(trans)) {
9747 ret = PTR_ERR(trans);
9748 goto out_notrans;
9749 }
9750
9751 /*
9752 * We need to find a free sequence number both in the source and
9753 * in the destination directory for the exchange.
9754 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009755 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009756 if (ret)
9757 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009758 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009759 if (ret)
9760 goto out_fail;
9761
9762 BTRFS_I(old_inode)->dir_index = 0ULL;
9763 BTRFS_I(new_inode)->dir_index = 0ULL;
9764
9765 /* Reference for the source. */
9766 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9767 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009768 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009769 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009770 btrfs_pin_log_trans(root);
9771 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009772 ret = btrfs_insert_inode_ref(trans, dest,
9773 new_dentry->d_name.name,
9774 new_dentry->d_name.len,
9775 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009776 btrfs_ino(BTRFS_I(new_dir)),
9777 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009778 if (ret)
9779 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009780 }
9781
9782 /* And now for the dest. */
9783 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9784 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009785 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009786 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009787 btrfs_pin_log_trans(dest);
9788 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009789 ret = btrfs_insert_inode_ref(trans, root,
9790 old_dentry->d_name.name,
9791 old_dentry->d_name.len,
9792 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009793 btrfs_ino(BTRFS_I(old_dir)),
9794 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009795 if (ret)
9796 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009797 }
9798
9799 /* Update inode version and ctime/mtime. */
9800 inode_inc_iversion(old_dir);
9801 inode_inc_iversion(new_dir);
9802 inode_inc_iversion(old_inode);
9803 inode_inc_iversion(new_inode);
9804 old_dir->i_ctime = old_dir->i_mtime = ctime;
9805 new_dir->i_ctime = new_dir->i_mtime = ctime;
9806 old_inode->i_ctime = ctime;
9807 new_inode->i_ctime = ctime;
9808
9809 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009810 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9811 BTRFS_I(old_inode), 1);
9812 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9813 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009814 }
9815
9816 /* src is a subvolume */
9817 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9818 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9819 ret = btrfs_unlink_subvol(trans, root, old_dir,
9820 root_objectid,
9821 old_dentry->d_name.name,
9822 old_dentry->d_name.len);
9823 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009824 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9825 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009826 old_dentry->d_name.name,
9827 old_dentry->d_name.len);
9828 if (!ret)
9829 ret = btrfs_update_inode(trans, root, old_inode);
9830 }
9831 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009832 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009833 goto out_fail;
9834 }
9835
9836 /* dest is a subvolume */
9837 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9838 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9839 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9840 root_objectid,
9841 new_dentry->d_name.name,
9842 new_dentry->d_name.len);
9843 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009844 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9845 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009846 new_dentry->d_name.name,
9847 new_dentry->d_name.len);
9848 if (!ret)
9849 ret = btrfs_update_inode(trans, dest, new_inode);
9850 }
9851 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009852 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009853 goto out_fail;
9854 }
9855
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009856 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009857 new_dentry->d_name.name,
9858 new_dentry->d_name.len, 0, old_idx);
9859 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009860 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009861 goto out_fail;
9862 }
9863
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009864 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009865 old_dentry->d_name.name,
9866 old_dentry->d_name.len, 0, new_idx);
9867 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009868 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009869 goto out_fail;
9870 }
9871
9872 if (old_inode->i_nlink == 1)
9873 BTRFS_I(old_inode)->dir_index = old_idx;
9874 if (new_inode->i_nlink == 1)
9875 BTRFS_I(new_inode)->dir_index = new_idx;
9876
Filipe Manana86e8aa02016-05-05 02:02:27 +01009877 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009878 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009879 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9880 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009881 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009882 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009883 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009884 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009885 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009886 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9887 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009888 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009889 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009890 }
9891out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009892 /*
9893 * If we have pinned a log and an error happened, we unpin tasks
9894 * trying to sync the log and force them to fallback to a transaction
9895 * commit if the log currently contains any of the inodes involved in
9896 * this rename operation (to ensure we do not persist a log with an
9897 * inconsistent state for any of these inodes or leading to any
9898 * inconsistencies when replayed). If the transaction was aborted, the
9899 * abortion reason is propagated to userspace when attempting to commit
9900 * the transaction. If the log does not contain any of these inodes, we
9901 * allow the tasks to sync it.
9902 */
9903 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009904 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9905 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9906 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009907 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009908 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009909 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009910
9911 if (root_log_pinned) {
9912 btrfs_end_log_trans(root);
9913 root_log_pinned = false;
9914 }
9915 if (dest_log_pinned) {
9916 btrfs_end_log_trans(dest);
9917 dest_log_pinned = false;
9918 }
9919 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009920 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009921out_notrans:
9922 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009923 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009924 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009925 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009926
9927 return ret;
9928}
9929
9930static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9931 struct btrfs_root *root,
9932 struct inode *dir,
9933 struct dentry *dentry)
9934{
9935 int ret;
9936 struct inode *inode;
9937 u64 objectid;
9938 u64 index;
9939
9940 ret = btrfs_find_free_ino(root, &objectid);
9941 if (ret)
9942 return ret;
9943
9944 inode = btrfs_new_inode(trans, root, dir,
9945 dentry->d_name.name,
9946 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009947 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009948 objectid,
9949 S_IFCHR | WHITEOUT_MODE,
9950 &index);
9951
9952 if (IS_ERR(inode)) {
9953 ret = PTR_ERR(inode);
9954 return ret;
9955 }
9956
9957 inode->i_op = &btrfs_special_inode_operations;
9958 init_special_inode(inode, inode->i_mode,
9959 WHITEOUT_DEV);
9960
9961 ret = btrfs_init_inode_security(trans, inode, dir,
9962 &dentry->d_name);
9963 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009964 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009965
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009966 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9967 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009968 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009969 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009970
9971 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009972out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009973 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009974 if (ret)
9975 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009976 iput(inode);
9977
Filipe Mananac9901612016-05-05 01:41:57 +01009978 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009979}
9980
Chris Mason39279cc2007-06-12 06:35:45 -04009981static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009982 struct inode *new_dir, struct dentry *new_dentry,
9983 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009984{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009985 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009986 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009987 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009988 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9989 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009990 struct inode *new_inode = d_inode(new_dentry);
9991 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009992 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009993 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009994 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009995 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009996 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009997
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009998 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009999 return -EPERM;
10000
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010001 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +080010002 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -050010003 return -EXDEV;
10004
Li Zefan33345d012011-04-20 10:31:50 +080010005 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010006 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010007 return -ENOTEMPTY;
10008
Chris Mason39279cc2007-06-12 06:35:45 -040010009 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010010 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -040010011 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -050010012
10013
10014 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -040010015 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -050010016 new_dentry->d_name.name,
10017 new_dentry->d_name.len);
10018
10019 if (ret) {
10020 if (ret == -EEXIST) {
10021 /* we shouldn't get
10022 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +053010023 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -050010024 return ret;
10025 }
10026 } else {
10027 /* maybe -EOVERFLOW */
10028 return ret;
10029 }
10030 }
10031 ret = 0;
10032
Chris Mason5a3f23d2009-03-31 13:27:11 -040010033 /*
Chris Mason8d875f92014-08-12 10:47:42 -070010034 * we're using rename to replace one file with another. Start IO on it
10035 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -040010036 */
Chris Mason8d875f92014-08-12 10:47:42 -070010037 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -040010038 filemap_flush(old_inode->i_mapping);
10039
Yan, Zheng76dda932009-09-21 16:00:26 -040010040 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +080010041 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010042 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010043 /*
10044 * We want to reserve the absolute worst case amount of items. So if
10045 * both inodes are subvols and we need to unlink them then that would
10046 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010047 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -040010048 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
10049 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +010010050 * If our rename has the whiteout flag, we need more 5 units for the
10051 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
10052 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -040010053 */
Filipe Manana5062af32016-05-05 10:26:26 +010010054 trans_num_items = 11;
10055 if (flags & RENAME_WHITEOUT)
10056 trans_num_items += 5;
10057 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010058 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010059 ret = PTR_ERR(trans);
10060 goto out_notrans;
10061 }
Chris Mason5f39d392007-10-15 16:14:19 -040010062
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010063 if (dest != root)
10064 btrfs_record_root_in_trans(trans, dest);
10065
Nikolay Borisov877574e2017-02-20 13:50:33 +020010066 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010067 if (ret)
10068 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -040010069
Miao Xie67de1172013-12-26 13:07:06 +080010070 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +080010071 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010072 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010073 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010074 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +010010075 btrfs_pin_log_trans(root);
10076 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -040010077 ret = btrfs_insert_inode_ref(trans, dest,
10078 new_dentry->d_name.name,
10079 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +080010080 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010081 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010082 if (ret)
10083 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010084 }
Chris Mason5a3f23d2009-03-31 13:27:11 -040010085
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010086 inode_inc_iversion(old_dir);
10087 inode_inc_iversion(new_dir);
10088 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010089 old_dir->i_ctime = old_dir->i_mtime =
10090 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010091 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -040010092
Chris Mason12fcfd22009-03-24 10:24:20 -040010093 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +010010094 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10095 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -040010096
Li Zefan33345d012011-04-20 10:31:50 +080010097 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010098 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
10099 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
10100 old_dentry->d_name.name,
10101 old_dentry->d_name.len);
10102 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010103 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10104 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +000010105 old_dentry->d_name.name,
10106 old_dentry->d_name.len);
10107 if (!ret)
10108 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010109 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010110 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010111 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010112 goto out_fail;
10113 }
Chris Mason39279cc2007-06-12 06:35:45 -040010114
10115 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010116 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010117 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010118 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010119 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10120 root_objectid = BTRFS_I(new_inode)->location.objectid;
10121 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10122 root_objectid,
10123 new_dentry->d_name.name,
10124 new_dentry->d_name.len);
10125 BUG_ON(new_inode->i_nlink == 0);
10126 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010127 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10128 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010129 new_dentry->d_name.name,
10130 new_dentry->d_name.len);
10131 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010132 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010133 ret = btrfs_orphan_add(trans,
10134 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010135 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010136 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010137 goto out_fail;
10138 }
Chris Mason39279cc2007-06-12 06:35:45 -040010139 }
Josef Bacikaec74772008-07-24 12:12:38 -040010140
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010141 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010142 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010143 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010144 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010145 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010146 goto out_fail;
10147 }
Chris Mason39279cc2007-06-12 06:35:45 -040010148
Miao Xie67de1172013-12-26 13:07:06 +080010149 if (old_inode->i_nlink == 1)
10150 BTRFS_I(old_inode)->dir_index = index;
10151
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010152 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010153 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010154
David Sterbaf85b7372017-01-20 14:54:07 +010010155 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10156 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010157 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010158 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010159 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010160
10161 if (flags & RENAME_WHITEOUT) {
10162 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10163 old_dentry);
10164
10165 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010166 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010167 goto out_fail;
10168 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010169 }
Chris Mason39279cc2007-06-12 06:35:45 -040010170out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010171 /*
10172 * If we have pinned the log and an error happened, we unpin tasks
10173 * trying to sync the log and force them to fallback to a transaction
10174 * commit if the log currently contains any of the inodes involved in
10175 * this rename operation (to ensure we do not persist a log with an
10176 * inconsistent state for any of these inodes or leading to any
10177 * inconsistencies when replayed). If the transaction was aborted, the
10178 * abortion reason is propagated to userspace when attempting to commit
10179 * the transaction. If the log does not contain any of these inodes, we
10180 * allow the tasks to sync it.
10181 */
10182 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010183 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10184 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10185 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010186 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010187 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010188 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010189
10190 btrfs_end_log_trans(root);
10191 log_pinned = false;
10192 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010193 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010194out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010195 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010196 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010197
Chris Mason39279cc2007-06-12 06:35:45 -040010198 return ret;
10199}
10200
Miklos Szeredi80ace852014-07-23 15:15:32 +020010201static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10202 struct inode *new_dir, struct dentry *new_dentry,
10203 unsigned int flags)
10204{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010205 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010206 return -EINVAL;
10207
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010208 if (flags & RENAME_EXCHANGE)
10209 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10210 new_dentry);
10211
10212 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010213}
10214
Miao Xie8ccf6f192012-10-25 09:28:04 +000010215static void btrfs_run_delalloc_work(struct btrfs_work *work)
10216{
10217 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010218 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010219
10220 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10221 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010222 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010223 filemap_flush(inode->i_mapping);
10224 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10225 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010226 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010227
10228 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010229 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010230 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010231 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010232 complete(&delalloc_work->completion);
10233}
10234
10235struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010236 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010237{
10238 struct btrfs_delalloc_work *work;
10239
David Sterba100d5702015-12-08 14:39:32 +010010240 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010241 if (!work)
10242 return NULL;
10243
10244 init_completion(&work->completion);
10245 INIT_LIST_HEAD(&work->list);
10246 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010247 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010248 WARN_ON_ONCE(!inode);
10249 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10250 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010251
10252 return work;
10253}
10254
10255void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10256{
10257 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010258 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010259}
10260
Chris Masond352ac62008-09-29 15:18:18 -040010261/*
10262 * some fairly slow code that needs optimization. This walks the list
10263 * of all the inodes with pending delalloc and forces them to disk.
10264 */
Miao Xie6c255e62014-03-06 13:55:01 +080010265static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10266 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010267{
Chris Masonea8c2812008-08-04 23:17:27 -040010268 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010269 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010270 struct btrfs_delalloc_work *work, *next;
10271 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010272 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010273 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010274
Miao Xie8ccf6f192012-10-25 09:28:04 +000010275 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010276 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010277
Miao Xie573bfb72014-03-06 13:55:03 +080010278 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010279 spin_lock(&root->delalloc_lock);
10280 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010281 while (!list_empty(&splice)) {
10282 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010283 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010284
Miao Xieeb73c1b2013-05-15 07:48:22 +000010285 list_move_tail(&binode->delalloc_inodes,
10286 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010287 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010288 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010289 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010290 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010291 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010292 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010293
David Sterba651d4942015-11-27 19:24:16 +010010294 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010295 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010296 if (delay_iput)
10297 btrfs_add_delayed_iput(inode);
10298 else
10299 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010300 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010301 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010302 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010303 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010304 btrfs_queue_work(root->fs_info->flush_workers,
10305 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010306 ret++;
10307 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010308 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010309 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010310 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010311 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010312 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010313
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010314out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010315 list_for_each_entry_safe(work, next, &works, list) {
10316 list_del_init(&work->list);
10317 btrfs_wait_and_free_delalloc_work(work);
10318 }
10319
Miao Xieeb73c1b2013-05-15 07:48:22 +000010320 if (!list_empty_careful(&splice)) {
10321 spin_lock(&root->delalloc_lock);
10322 list_splice_tail(&splice, &root->delalloc_inodes);
10323 spin_unlock(&root->delalloc_lock);
10324 }
Miao Xie573bfb72014-03-06 13:55:03 +080010325 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010326 return ret;
10327}
10328
10329int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10330{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010331 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010332 int ret;
10333
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010334 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010335 return -EROFS;
10336
Miao Xie6c255e62014-03-06 13:55:01 +080010337 ret = __start_delalloc_inodes(root, delay_iput, -1);
10338 if (ret > 0)
10339 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010340 /*
10341 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010342 * we have to make sure the IO is actually started and that
10343 * ordered extents get created before we return
10344 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010345 atomic_inc(&fs_info->async_submit_draining);
10346 while (atomic_read(&fs_info->nr_async_submits) ||
10347 atomic_read(&fs_info->async_delalloc_pages)) {
10348 wait_event(fs_info->async_submit_wait,
10349 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10350 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010351 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010352 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010353 return ret;
10354}
10355
Miao Xie6c255e62014-03-06 13:55:01 +080010356int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10357 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010358{
10359 struct btrfs_root *root;
10360 struct list_head splice;
10361 int ret;
10362
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010363 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010364 return -EROFS;
10365
10366 INIT_LIST_HEAD(&splice);
10367
Miao Xie573bfb72014-03-06 13:55:03 +080010368 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010369 spin_lock(&fs_info->delalloc_root_lock);
10370 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010371 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010372 root = list_first_entry(&splice, struct btrfs_root,
10373 delalloc_root);
10374 root = btrfs_grab_fs_root(root);
10375 BUG_ON(!root);
10376 list_move_tail(&root->delalloc_root,
10377 &fs_info->delalloc_roots);
10378 spin_unlock(&fs_info->delalloc_root_lock);
10379
Miao Xie6c255e62014-03-06 13:55:01 +080010380 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010381 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010382 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010383 goto out;
10384
Miao Xie6c255e62014-03-06 13:55:01 +080010385 if (nr != -1) {
10386 nr -= ret;
10387 WARN_ON(nr < 0);
10388 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010389 spin_lock(&fs_info->delalloc_root_lock);
10390 }
10391 spin_unlock(&fs_info->delalloc_root_lock);
10392
Miao Xie6c255e62014-03-06 13:55:01 +080010393 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010394 atomic_inc(&fs_info->async_submit_draining);
10395 while (atomic_read(&fs_info->nr_async_submits) ||
10396 atomic_read(&fs_info->async_delalloc_pages)) {
10397 wait_event(fs_info->async_submit_wait,
10398 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10399 atomic_read(&fs_info->async_delalloc_pages) == 0));
10400 }
10401 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010402out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010403 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010404 spin_lock(&fs_info->delalloc_root_lock);
10405 list_splice_tail(&splice, &fs_info->delalloc_roots);
10406 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010407 }
Miao Xie573bfb72014-03-06 13:55:03 +080010408 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010409 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010410}
10411
Chris Mason39279cc2007-06-12 06:35:45 -040010412static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10413 const char *symname)
10414{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010415 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010416 struct btrfs_trans_handle *trans;
10417 struct btrfs_root *root = BTRFS_I(dir)->root;
10418 struct btrfs_path *path;
10419 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010420 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010421 int err;
10422 int drop_inode = 0;
10423 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010424 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010425 int name_len;
10426 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010427 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010428 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010429 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010430
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010431 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010432 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010433 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010434
Josef Bacik9ed74f22009-09-11 16:12:44 -040010435 /*
10436 * 2 items for inode item and ref
10437 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010438 * 1 item for updating parent inode item
10439 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010440 * 1 item for xattr if selinux is on
10441 */
Filipe Manana9269d122015-12-31 18:16:29 +000010442 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010443 if (IS_ERR(trans))
10444 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010445
Li Zefan581bb052011-04-20 10:06:11 +080010446 err = btrfs_find_free_ino(root, &objectid);
10447 if (err)
10448 goto out_unlock;
10449
Josef Bacikaec74772008-07-24 12:12:38 -040010450 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010451 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10452 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010453 if (IS_ERR(inode)) {
10454 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010455 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010456 }
Chris Mason39279cc2007-06-12 06:35:45 -040010457
Casey Schauflerad19db72011-12-15 10:09:07 -050010458 /*
10459 * If the active LSM wants to access the inode during
10460 * d_instantiate it needs these. Smack checks to see
10461 * if the filesystem supports xattrs by looking at the
10462 * ops vector.
10463 */
10464 inode->i_fop = &btrfs_file_operations;
10465 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010466 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010467 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10468
10469 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10470 if (err)
10471 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010472
Chris Mason39279cc2007-06-12 06:35:45 -040010473 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010474 if (!path) {
10475 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010476 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010477 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010478 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010479 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010480 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010481 datasize = btrfs_file_extent_calc_inline_size(name_len);
10482 err = btrfs_insert_empty_item(trans, root, path, &key,
10483 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010484 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010485 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010486 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010487 }
Chris Mason5f39d392007-10-15 16:14:19 -040010488 leaf = path->nodes[0];
10489 ei = btrfs_item_ptr(leaf, path->slots[0],
10490 struct btrfs_file_extent_item);
10491 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10492 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010493 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010494 btrfs_set_file_extent_encryption(leaf, ei, 0);
10495 btrfs_set_file_extent_compression(leaf, ei, 0);
10496 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10497 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10498
Chris Mason39279cc2007-06-12 06:35:45 -040010499 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010500 write_extent_buffer(leaf, symname, ptr, name_len);
10501 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010502 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010503
Chris Mason39279cc2007-06-12 06:35:45 -040010504 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010505 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010506 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010507 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010508 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010509 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010510 /*
10511 * Last step, add directory indexes for our symlink inode. This is the
10512 * last step to avoid extra cleanup of these indexes if an error happens
10513 * elsewhere above.
10514 */
10515 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010516 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10517 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010518 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010519 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010520 goto out_unlock_inode;
10521 }
10522
10523 unlock_new_inode(inode);
10524 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010525
10526out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010527 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010528 if (drop_inode) {
10529 inode_dec_link_count(inode);
10530 iput(inode);
10531 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010532 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010533 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010534
10535out_unlock_inode:
10536 drop_inode = 1;
10537 unlock_new_inode(inode);
10538 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010539}
Chris Mason16432982008-04-10 10:23:21 -040010540
Josef Bacik0af3d002010-06-21 14:48:16 -040010541static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10542 u64 start, u64 num_bytes, u64 min_size,
10543 loff_t actual_len, u64 *alloc_hint,
10544 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010545{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010546 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010547 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10548 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010549 struct btrfs_root *root = BTRFS_I(inode)->root;
10550 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010551 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010552 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010553 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010554 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010555 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010556 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010557 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010558
Josef Bacik0af3d002010-06-21 14:48:16 -040010559 if (trans)
10560 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010561 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010562 if (own_trans) {
10563 trans = btrfs_start_transaction(root, 3);
10564 if (IS_ERR(trans)) {
10565 ret = PTR_ERR(trans);
10566 break;
10567 }
Yan Zhengd899e052008-10-30 14:25:28 -040010568 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010569
Byongho Leeee221842015-12-15 01:42:10 +090010570 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010571 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010572 /*
10573 * If we are severely fragmented we could end up with really
10574 * small allocations, so if the allocator is returning small
10575 * chunks lets make its job easier by only searching for those
10576 * sized chunks.
10577 */
10578 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010579 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10580 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010581 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010582 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010583 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010584 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010585 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010586 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010587
Josef Bacik0b670dc2015-09-23 17:11:16 -040010588 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010589 ret = insert_reserved_file_extent(trans, inode,
10590 cur_offset, ins.objectid,
10591 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010592 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010593 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010594 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010595 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010596 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010597 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010598 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010599 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010600 break;
10601 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010602
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010603 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010604 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010605
Josef Bacik5dc562c2012-08-17 13:14:17 -040010606 em = alloc_extent_map();
10607 if (!em) {
10608 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10609 &BTRFS_I(inode)->runtime_flags);
10610 goto next;
10611 }
10612
10613 em->start = cur_offset;
10614 em->orig_start = cur_offset;
10615 em->len = ins.offset;
10616 em->block_start = ins.objectid;
10617 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010618 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010619 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010620 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010621 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10622 em->generation = trans->transid;
10623
10624 while (1) {
10625 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010626 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010627 write_unlock(&em_tree->lock);
10628 if (ret != -EEXIST)
10629 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010630 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010631 cur_offset + ins.offset - 1,
10632 0);
10633 }
10634 free_extent_map(em);
10635next:
Yan Zhengd899e052008-10-30 14:25:28 -040010636 num_bytes -= ins.offset;
10637 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010638 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010639
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010640 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010641 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010642 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010643 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010644 (actual_len > inode->i_size) &&
10645 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010646 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010647 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010648 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010649 i_size = cur_offset;
10650 i_size_write(inode, i_size);
10651 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010652 }
10653
Yan Zhengd899e052008-10-30 14:25:28 -040010654 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010655
10656 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010657 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010658 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010659 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010660 break;
10661 }
Yan Zhengd899e052008-10-30 14:25:28 -040010662
Josef Bacik0af3d002010-06-21 14:48:16 -040010663 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010664 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010665 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010666 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010667 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010668 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010669 return ret;
10670}
10671
Josef Bacik0af3d002010-06-21 14:48:16 -040010672int btrfs_prealloc_file_range(struct inode *inode, int mode,
10673 u64 start, u64 num_bytes, u64 min_size,
10674 loff_t actual_len, u64 *alloc_hint)
10675{
10676 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10677 min_size, actual_len, alloc_hint,
10678 NULL);
10679}
10680
10681int btrfs_prealloc_file_range_trans(struct inode *inode,
10682 struct btrfs_trans_handle *trans, int mode,
10683 u64 start, u64 num_bytes, u64 min_size,
10684 loff_t actual_len, u64 *alloc_hint)
10685{
10686 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10687 min_size, actual_len, alloc_hint, trans);
10688}
10689
Chris Masone6dcd2d2008-07-17 12:53:50 -040010690static int btrfs_set_page_dirty(struct page *page)
10691{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010692 return __set_page_dirty_nobuffers(page);
10693}
10694
Al Viro10556cb22011-06-20 19:28:19 -040010695static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010696{
Li Zefanb83cc962010-12-20 16:04:08 +080010697 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010698 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010699
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010700 if (mask & MAY_WRITE &&
10701 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10702 if (btrfs_root_readonly(root))
10703 return -EROFS;
10704 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10705 return -EACCES;
10706 }
Al Viro2830ba72011-06-20 19:16:29 -040010707 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010708}
Chris Mason39279cc2007-06-12 06:35:45 -040010709
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010710static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10711{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010712 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010713 struct btrfs_trans_handle *trans;
10714 struct btrfs_root *root = BTRFS_I(dir)->root;
10715 struct inode *inode = NULL;
10716 u64 objectid;
10717 u64 index;
10718 int ret = 0;
10719
10720 /*
10721 * 5 units required for adding orphan entry
10722 */
10723 trans = btrfs_start_transaction(root, 5);
10724 if (IS_ERR(trans))
10725 return PTR_ERR(trans);
10726
10727 ret = btrfs_find_free_ino(root, &objectid);
10728 if (ret)
10729 goto out;
10730
10731 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010732 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010733 if (IS_ERR(inode)) {
10734 ret = PTR_ERR(inode);
10735 inode = NULL;
10736 goto out;
10737 }
10738
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010739 inode->i_fop = &btrfs_file_operations;
10740 inode->i_op = &btrfs_file_inode_operations;
10741
10742 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010743 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10744
Chris Masonb0d5d102014-09-08 13:08:51 -070010745 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10746 if (ret)
10747 goto out_inode;
10748
10749 ret = btrfs_update_inode(trans, root, inode);
10750 if (ret)
10751 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010752 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010753 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010754 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010755
Filipe Manana5762b5c2014-08-01 00:10:32 +010010756 /*
10757 * We set number of links to 0 in btrfs_new_inode(), and here we set
10758 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10759 * through:
10760 *
10761 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10762 */
10763 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010764 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010765 d_tmpfile(dentry, inode);
10766 mark_inode_dirty(inode);
10767
10768out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010769 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010770 if (ret)
10771 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010772 btrfs_balance_delayed_items(fs_info);
10773 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010774 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010775
10776out_inode:
10777 unlock_new_inode(inode);
10778 goto out;
10779
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010780}
10781
David Sterba20a7db82017-02-17 16:24:29 +010010782__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010783static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010784{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010785 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010786}
10787
Josef Bacikc6100a42017-05-05 11:57:13 -040010788static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10789{
10790 struct inode *inode = private_data;
10791 return btrfs_sb(inode->i_sb);
10792}
10793
10794static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10795 u64 start, u64 end)
10796{
10797 struct inode *inode = private_data;
10798 u64 isize;
10799
10800 isize = i_size_read(inode);
10801 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10802 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10803 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10804 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10805 }
10806}
10807
10808void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10809{
10810 struct inode *inode = private_data;
10811 unsigned long index = start >> PAGE_SHIFT;
10812 unsigned long end_index = end >> PAGE_SHIFT;
10813 struct page *page;
10814
10815 while (index <= end_index) {
10816 page = find_get_page(inode->i_mapping, index);
10817 ASSERT(page); /* Pages should be in the extent_io_tree */
10818 set_page_writeback(page);
10819 put_page(page);
10820 index++;
10821 }
10822}
10823
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010824static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010825 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010826 .lookup = btrfs_lookup,
10827 .create = btrfs_create,
10828 .unlink = btrfs_unlink,
10829 .link = btrfs_link,
10830 .mkdir = btrfs_mkdir,
10831 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010832 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010833 .symlink = btrfs_symlink,
10834 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010835 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010836 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010837 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010838 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010839 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010840 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010841 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010842};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010843static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010844 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010845 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010846 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010847};
Yan, Zheng76dda932009-09-21 16:00:26 -040010848
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010849static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010850 .llseek = generic_file_llseek,
10851 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010852 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010853 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010854 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010855#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010856 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010857#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010858 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010859 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010860};
10861
David Sterba20e55062015-11-19 11:42:28 +010010862static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010863 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010864 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010865 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010866 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010867 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010868 .tree_fs_info = iotree_fs_info,
10869 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010870
10871 /* optional callbacks */
10872 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010873 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010874 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010875 .set_bit_hook = btrfs_set_bit_hook,
10876 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010877 .merge_extent_hook = btrfs_merge_extent_hook,
10878 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010879 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010880};
10881
Chris Mason35054392009-01-21 13:11:13 -050010882/*
10883 * btrfs doesn't support the bmap operation because swapfiles
10884 * use bmap to make a mapping of extents in the file. They assume
10885 * these extents won't change over the life of the file and they
10886 * use the bmap result to do IO directly to the drive.
10887 *
10888 * the btrfs bmap call would return logical addresses that aren't
10889 * suitable for IO and they also will change frequently as COW
10890 * operations happen. So, swapfile + btrfs == corruption.
10891 *
10892 * For now we're avoiding this by dropping bmap.
10893 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010894static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010895 .readpage = btrfs_readpage,
10896 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010897 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010898 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010899 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010900 .invalidatepage = btrfs_invalidatepage,
10901 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010902 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010903 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010904};
10905
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010906static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010907 .readpage = btrfs_readpage,
10908 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010909 .invalidatepage = btrfs_invalidatepage,
10910 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010911};
10912
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010913static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010914 .getattr = btrfs_getattr,
10915 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010916 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010917 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010918 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010919 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010920 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010921 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010922};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010923static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010924 .getattr = btrfs_getattr,
10925 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010926 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010927 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010928 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010929 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010930 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010931};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010932static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010933 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010934 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010935 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010936 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010937 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010938 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010939};
Yan, Zheng76dda932009-09-21 16:00:26 -040010940
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010941const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010942 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010943 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010944};