blob: 08ae94415ed4ced2b556784b655bba24c2bb63ec [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040045#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050046#include <linux/iversion.h>
Chris Mason39279cc2007-06-12 06:35:45 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040052#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040053#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040054#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020055#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040056#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050057#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050058#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080059#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000060#include "backref.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080063#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040064
65struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080066 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040067 struct btrfs_root *root;
68};
69
Filipe Mananaf28a4922015-12-08 19:23:20 +000070struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000071 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040089struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050090struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040091
92#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010093static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040094 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
95 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
96 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
97 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
98 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
99 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
100 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
101};
102
Eric Sandeen3972f262013-01-12 02:57:22 +0000103static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +0000104static int btrfs_truncate(struct inode *inode, bool skip_writeback);
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 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200279static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400280 u64 end, size_t compressed_size,
281 int compress_type,
282 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400283{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200284 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400285 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400286 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400287 u64 isize = i_size_read(inode);
288 u64 actual_end = min(end + 1, isize);
289 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400291 u64 data_len = inline_len;
292 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000293 struct btrfs_path *path;
294 int extent_inserted = 0;
295 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400296
297 if (compressed_size)
298 data_len = compressed_size;
299
300 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400301 actual_end > fs_info->sectorsize ||
302 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400303 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400305 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400306 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400307 return 1;
308 }
309
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 path = btrfs_alloc_path();
311 if (!path)
312 return -ENOMEM;
313
Josef Bacik00361582013-08-14 14:02:47 -0400314 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000315 if (IS_ERR(trans)) {
316 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400317 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000318 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400319 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400320
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000321 if (compressed_size && compressed_pages)
322 extent_item_size = btrfs_file_extent_calc_inline_size(
323 compressed_size);
324 else
325 extent_item_size = btrfs_file_extent_calc_inline_size(
326 inline_len);
327
328 ret = __btrfs_drop_extents(trans, root, inode, path,
329 start, aligned_end, NULL,
330 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400331 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400332 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400333 goto out;
334 }
Chris Masonc8b97812008-10-29 14:49:59 -0400335
336 if (isize > actual_end)
337 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000338 ret = insert_inline_extent(trans, path, extent_inserted,
339 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400340 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000341 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400342 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400343 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400344 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400345 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400346 ret = 1;
347 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400349
Josef Bacikbdc20e62013-02-28 13:23:38 -0500350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200351 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400352out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800353 /*
354 * Don't forget to free the reserved space, as for inlined extent
355 * it won't count as data extent, free them directly here.
356 * And at reserve time, it's always aligned to page size, so
357 * just free one page here.
358 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800359 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000360 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400361 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400362 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400363}
364
Chris Mason771ed682008-11-06 22:02:51 -0500365struct async_extent {
366 u64 start;
367 u64 ram_size;
368 u64 compressed_size;
369 struct page **pages;
370 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800371 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500372 struct list_head list;
373};
374
375struct async_cow {
376 struct inode *inode;
377 struct btrfs_root *root;
378 struct page *locked_page;
379 u64 start;
380 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600381 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500382 struct list_head extents;
383 struct btrfs_work work;
384};
385
386static noinline int add_async_extent(struct async_cow *cow,
387 u64 start, u64 ram_size,
388 u64 compressed_size,
389 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800390 unsigned long nr_pages,
391 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500392{
393 struct async_extent *async_extent;
394
395 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100396 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500397 async_extent->start = start;
398 async_extent->ram_size = ram_size;
399 async_extent->compressed_size = compressed_size;
400 async_extent->pages = pages;
401 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800402 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500403 list_add_tail(&async_extent->list, &cow->extents);
404 return 0;
405}
406
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300407static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400409 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800410
411 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400412 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800413 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200414 /* defrag ioctl */
415 if (BTRFS_I(inode)->defrag_compress)
416 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800417 /* bad compression ratios */
418 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
419 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400420 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800421 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200422 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300423 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800424 return 0;
425}
426
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200427static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800428 u64 start, u64 end, u64 num_bytes, u64 small_write)
429{
430 /* If this is a small write inside eof, kick off a defrag */
431 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200432 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800433 btrfs_add_inode_defrag(NULL, inode);
434}
435
Chris Masonc8b97812008-10-29 14:49:59 -0400436/*
Chris Mason771ed682008-11-06 22:02:51 -0500437 * we create compressed extents in two phases. The first
438 * phase compresses a range of pages that have already been
439 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400440 *
Chris Mason771ed682008-11-06 22:02:51 -0500441 * This is done inside an ordered work queue, and the compression
442 * is spread across many cpus. The actual IO submission is step
443 * two, and the ordered work queue takes care of making sure that
444 * happens in the same order things were put onto the queue by
445 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400446 *
Chris Mason771ed682008-11-06 22:02:51 -0500447 * If this code finds it can't get good compression, it puts an
448 * entry onto the work queue to write the uncompressed bytes. This
449 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300450 * are written in the same order that the flusher thread sent them
451 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400452 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100453static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500454 struct page *locked_page,
455 u64 start, u64 end,
456 struct async_cow *async_cow,
457 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400458{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400459 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400460 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400461 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500462 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400463 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400464 struct page **pages = NULL;
465 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400466 unsigned long total_compressed = 0;
467 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400468 int i;
469 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400470 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400471 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400472
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200473 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
474 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400475
Chris Mason42dc7ba2008-12-15 11:44:56 -0500476 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400477again:
478 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300479 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100480 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
481 nr_pages = min_t(unsigned long, nr_pages,
482 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400483
Chris Masonf03d9301f2009-02-04 09:31:06 -0500484 /*
485 * we don't want to send crud past the end of i_size through
486 * compression, that's just a waste of CPU time. So, if the
487 * end of the file is before the start of our current
488 * requested range of bytes, we bail out to the uncompressed
489 * cleanup code that can deal with all of this.
490 *
491 * It isn't really the fastest way to fix things, but this is a
492 * very uncommon corner.
493 */
494 if (actual_end <= start)
495 goto cleanup_and_bail_uncompressed;
496
Chris Masonc8b97812008-10-29 14:49:59 -0400497 total_compressed = actual_end - start;
498
Shilong Wang4bcbb332014-10-07 18:44:35 -0400499 /*
500 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400501 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400502 */
503 if (total_compressed <= blocksize &&
504 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
505 goto cleanup_and_bail_uncompressed;
506
David Sterba069eac72017-02-14 19:36:54 +0100507 total_compressed = min_t(unsigned long, total_compressed,
508 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400509 total_in = 0;
510 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400511
Chris Mason771ed682008-11-06 22:02:51 -0500512 /*
513 * we do compression for mount -o compress and when the
514 * inode has not been flagged as nocompress. This flag can
515 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400516 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300517 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400518 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100519 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800520 if (!pages) {
521 /* just bail out to the uncompressed code */
522 goto cont;
523 }
Chris Mason179e29e2007-11-01 11:28:41 -0400524
David Sterbaeec63c62017-07-17 19:41:31 +0200525 if (BTRFS_I(inode)->defrag_compress)
526 compress_type = BTRFS_I(inode)->defrag_compress;
527 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200528 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800529
Chris Mason4adaa612013-03-26 13:07:00 -0400530 /*
531 * we need to call clear_page_dirty_for_io on each
532 * page in the range. Otherwise applications with the file
533 * mmap'd can wander in and change the page contents while
534 * we are compressing them.
535 *
536 * If the compression fails for any reason, we set the pages
537 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300538 *
539 * Note that the remaining part is redirtied, the start pointer
540 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400541 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300542 if (!redirty) {
543 extent_range_clear_dirty_for_io(inode, start, end);
544 redirty = 1;
545 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200546
547 /* Compression level is applied here and only here */
548 ret = btrfs_compress_pages(
549 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800550 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100551 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100552 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800553 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100554 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400555
556 if (!ret) {
557 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300558 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100559 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400560 char *kaddr;
561
562 /* zero the tail end of the last page, we might be
563 * sending it down to disk
564 */
565 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800566 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400567 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300568 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800569 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400570 }
571 will_compress = 1;
572 }
573 }
Li Zefan560f7d72011-09-08 10:22:01 +0800574cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400575 if (start == 0) {
576 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300577 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400578 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500579 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400580 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200581 ret = cow_file_range_inline(inode, start, end, 0,
582 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400583 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500584 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200585 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000586 total_compressed,
587 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400588 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100589 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400590 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400591 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
592 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100593 unsigned long page_error_op;
594
Filipe Mananae6eb4312014-10-10 10:45:12 +0100595 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400596
Chris Mason771ed682008-11-06 22:02:51 -0500597 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100598 * inline extent creation worked or returned error,
599 * we don't need to create any more async work items.
600 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400601 *
602 * We use DO_ACCOUNTING here because we need the
603 * delalloc_release_metadata to be done _after_ we drop
604 * our outstanding extent for clearing delalloc for this
605 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500606 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800607 extent_clear_unlock_delalloc(inode, start, end, end,
608 NULL, clear_flags,
609 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400610 PAGE_CLEAR_DIRTY |
611 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100612 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400613 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400614 goto free_pages_out;
615 }
616 }
617
618 if (will_compress) {
619 /*
620 * we aren't doing an inline extent round the compressed size
621 * up to a block size boundary so the allocator does sane
622 * things
623 */
Qu Wenruofda28322013-02-26 08:10:22 +0000624 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400625
626 /*
627 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300628 * win, compare the page count read with the blocks on disk,
629 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400630 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300631 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300632 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700633 *num_added += 1;
634
635 /*
636 * The async work queues will take care of doing actual
637 * allocation on disk for these compressed pages, and
638 * will submit them to the elevator.
639 */
Timofey Titovets11708622017-10-03 18:06:01 +0300640 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100641 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700642 compress_type);
643
Timofey Titovets11708622017-10-03 18:06:01 +0300644 if (start + total_in < end) {
645 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700646 pages = NULL;
647 cond_resched();
648 goto again;
649 }
650 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400651 }
652 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700653 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400654 /*
655 * the compression code ran but failed to make things smaller,
656 * free any pages it allocated and our page pointer array
657 */
David Sterba4d3a8002017-02-14 19:04:07 +0100658 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400659 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300660 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400661 }
662 kfree(pages);
663 pages = NULL;
664 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100665 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400666
667 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400668 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200669 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500670 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500671 }
Chris Masonc8b97812008-10-29 14:49:59 -0400672 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500673cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700674 /*
675 * No compression, but we still need to write the pages in the file
676 * we've been given so far. redirty the locked page if it corresponds
677 * to our extent and set things up for the async work queue to run
678 * cow_file_range to do the normal delalloc dance.
679 */
680 if (page_offset(locked_page) >= start &&
681 page_offset(locked_page) <= end)
682 __set_page_dirty_nobuffers(locked_page);
683 /* unlocked later on in the async handlers */
684
685 if (redirty)
686 extent_range_redirty_for_io(inode, start, end);
687 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
688 BTRFS_COMPRESS_NONE);
689 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500690
Filipe Mananac44f6492014-10-09 21:15:44 +0100691 return;
Chris Mason771ed682008-11-06 22:02:51 -0500692
693free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100694 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500695 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300696 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500697 }
Chris Masond3977122009-01-05 21:25:51 -0500698 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500699}
Chris Mason771ed682008-11-06 22:02:51 -0500700
Filipe Manana40ae8372014-10-06 22:14:24 +0100701static void free_async_extent_pages(struct async_extent *async_extent)
702{
703 int i;
704
705 if (!async_extent->pages)
706 return;
707
708 for (i = 0; i < async_extent->nr_pages; i++) {
709 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300710 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100711 }
712 kfree(async_extent->pages);
713 async_extent->nr_pages = 0;
714 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500715}
716
717/*
718 * phase two of compressed writeback. This is the ordered portion
719 * of the code, which only gets called in the order the work was
720 * queued. We walk all the async extents created by compress_file_range
721 * and send them down to the disk.
722 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100723static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500724 struct async_cow *async_cow)
725{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400726 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500727 struct async_extent *async_extent;
728 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500729 struct btrfs_key ins;
730 struct extent_map *em;
731 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500732 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500733 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500734
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500735again:
Chris Masond3977122009-01-05 21:25:51 -0500736 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500737 async_extent = list_entry(async_cow->extents.next,
738 struct async_extent, list);
739 list_del(&async_extent->list);
740
741 io_tree = &BTRFS_I(inode)->io_tree;
742
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500743retry:
Chris Mason771ed682008-11-06 22:02:51 -0500744 /* did the compression code fall back to uncompressed IO? */
745 if (!async_extent->pages) {
746 int page_started = 0;
747 unsigned long nr_written = 0;
748
749 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000750 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100751 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500752
753 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500754 ret = cow_file_range(inode, async_cow->locked_page,
755 async_extent->start,
756 async_extent->start +
757 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800758 async_extent->start +
759 async_extent->ram_size - 1,
760 &page_started, &nr_written, 0,
761 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500762
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100763 /* JDM XXX */
764
Chris Mason771ed682008-11-06 22:02:51 -0500765 /*
766 * if page_started, cow_file_range inserted an
767 * inline extent and took care of all the unlocking
768 * and IO for us. Otherwise, we need to submit
769 * all those pages down to the drive.
770 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500771 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200772 extent_write_locked_range(inode,
773 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500774 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500775 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500776 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500777 else if (ret)
778 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500779 kfree(async_extent);
780 cond_resched();
781 continue;
782 }
783
784 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100785 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500786
Wang Xiaoguang18513092016-07-25 15:51:40 +0800787 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500788 async_extent->compressed_size,
789 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800790 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500791 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100792 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500793
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400794 if (ret == -ENOSPC) {
795 unlock_extent(io_tree, async_extent->start,
796 async_extent->start +
797 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800798
799 /*
800 * we need to redirty the pages if we decide to
801 * fallback to uncompressed IO, otherwise we
802 * will not submit these pages down to lower
803 * layers.
804 */
805 extent_range_redirty_for_io(inode,
806 async_extent->start,
807 async_extent->start +
808 async_extent->ram_size - 1);
809
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100810 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400811 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500812 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500813 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000814 /*
815 * here we're doing allocation and writeback of the
816 * compressed pages
817 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800818 em = create_io_em(inode, async_extent->start,
819 async_extent->ram_size, /* len */
820 async_extent->start, /* orig_start */
821 ins.objectid, /* block_start */
822 ins.offset, /* block_len */
823 ins.offset, /* orig_block_len */
824 async_extent->ram_size, /* ram_bytes */
825 async_extent->compress_type,
826 BTRFS_ORDERED_COMPRESSED);
827 if (IS_ERR(em))
828 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500829 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800830 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500831
Li Zefan261507a02010-12-17 14:21:50 +0800832 ret = btrfs_add_ordered_extent_compress(inode,
833 async_extent->start,
834 ins.objectid,
835 async_extent->ram_size,
836 ins.offset,
837 BTRFS_ORDERED_COMPRESSED,
838 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100839 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200840 btrfs_drop_extent_cache(BTRFS_I(inode),
841 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100842 async_extent->start +
843 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500844 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100845 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400846 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500847
Chris Mason771ed682008-11-06 22:02:51 -0500848 /*
849 * clear dirty, set writeback and unlock the pages.
850 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400851 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400852 async_extent->start +
853 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800854 async_extent->start +
855 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400856 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
857 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400858 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200859 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500860 async_extent->start,
861 async_extent->ram_size,
862 ins.objectid,
863 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600864 async_extent->nr_pages,
865 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100866 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
867 struct page *p = async_extent->pages[0];
868 const u64 start = async_extent->start;
869 const u64 end = start + async_extent->ram_size - 1;
870
871 p->mapping = inode->i_mapping;
872 tree->ops->writepage_end_io_hook(p, start, end,
873 NULL, 0);
874 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800875 extent_clear_unlock_delalloc(inode, start, end, end,
876 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100877 PAGE_END_WRITEBACK |
878 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100879 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100880 }
Chris Mason771ed682008-11-06 22:02:51 -0500881 alloc_hint = ins.objectid + ins.offset;
882 kfree(async_extent);
883 cond_resched();
884 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100885 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500886out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400887 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400888 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100889out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400890 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500891 async_extent->start +
892 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800893 async_extent->start +
894 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400895 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100896 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400897 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
898 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100899 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
900 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100901 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100902 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500903 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500904}
905
Josef Bacik4b46fce2010-05-23 11:00:55 -0400906static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
907 u64 num_bytes)
908{
909 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
910 struct extent_map *em;
911 u64 alloc_hint = 0;
912
913 read_lock(&em_tree->lock);
914 em = search_extent_mapping(em_tree, start, num_bytes);
915 if (em) {
916 /*
917 * if block start isn't an actual block number then find the
918 * first block in this inode and use that as a hint. If that
919 * block is also bogus then just don't worry about it.
920 */
921 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
922 free_extent_map(em);
923 em = search_extent_mapping(em_tree, 0, 0);
924 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
925 alloc_hint = em->block_start;
926 if (em)
927 free_extent_map(em);
928 } else {
929 alloc_hint = em->block_start;
930 free_extent_map(em);
931 }
932 }
933 read_unlock(&em_tree->lock);
934
935 return alloc_hint;
936}
937
Chris Mason771ed682008-11-06 22:02:51 -0500938/*
939 * when extent_io.c finds a delayed allocation range in the file,
940 * the call backs end up in this code. The basic idea is to
941 * allocate extents on disk for the range, and create ordered data structs
942 * in ram to track those extents.
943 *
944 * locked_page is the page that writepage had locked already. We use
945 * it to make sure we don't do extra locks or unlocks.
946 *
947 * *page_started is set to one if we unlock locked_page and do everything
948 * required to start IO on it. It may be clean and already done with
949 * IO when we return.
950 */
Josef Bacik00361582013-08-14 14:02:47 -0400951static noinline int cow_file_range(struct inode *inode,
952 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800953 u64 start, u64 end, u64 delalloc_end,
954 int *page_started, unsigned long *nr_written,
955 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500956{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400957 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400958 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500959 u64 alloc_hint = 0;
960 u64 num_bytes;
961 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000962 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400963 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500964 struct btrfs_key ins;
965 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000966 unsigned clear_bits;
967 unsigned long page_ops;
968 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500969 int ret = 0;
970
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200971 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400972 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500973 ret = -EINVAL;
974 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400975 }
Chris Mason771ed682008-11-06 22:02:51 -0500976
Qu Wenruofda28322013-02-26 08:10:22 +0000977 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500978 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800979 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500980
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200981 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400982
Chris Mason771ed682008-11-06 22:02:51 -0500983 if (start == 0) {
984 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200985 ret = cow_file_range_inline(inode, start, end, 0,
986 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500987 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400988 /*
989 * We use DO_ACCOUNTING here because we need the
990 * delalloc_release_metadata to be run _after_ we drop
991 * our outstanding extent for clearing delalloc for this
992 * range.
993 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800994 extent_clear_unlock_delalloc(inode, start, end,
995 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400996 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400997 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
998 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400999 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1000 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001001 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001002 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001003 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001004 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001005 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001006 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001007 }
1008 }
Chris Masonc8b97812008-10-29 14:49:59 -04001009
Josef Bacik4b46fce2010-05-23 11:00:55 -04001010 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001011 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1012 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001013
Anand Jain3752d222018-02-15 12:29:38 +08001014 while (num_bytes > 0) {
1015 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001016 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001017 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001018 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001019 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001020 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001021 cur_alloc_size = ins.offset;
1022 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001023
Chris Mason771ed682008-11-06 22:02:51 -05001024 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001025 em = create_io_em(inode, start, ins.offset, /* len */
1026 start, /* orig_start */
1027 ins.objectid, /* block_start */
1028 ins.offset, /* block_len */
1029 ins.offset, /* orig_block_len */
1030 ram_size, /* ram_bytes */
1031 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001032 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001033 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001034 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001035 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001036
Chris Masone6dcd2d2008-07-17 12:53:50 -04001037 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001038 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001039 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001040 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001041
Yan Zheng17d217f2008-12-12 10:03:38 -05001042 if (root->root_key.objectid ==
1043 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1044 ret = btrfs_reloc_clone_csums(inode, start,
1045 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001046 /*
1047 * Only drop cache here, and process as normal.
1048 *
1049 * We must not allow extent_clear_unlock_delalloc()
1050 * at out_unlock label to free meta of this ordered
1051 * extent, as its meta should be freed by
1052 * btrfs_finish_ordered_io().
1053 *
1054 * So we must continue until @start is increased to
1055 * skip current ordered extent.
1056 */
Josef Bacik00361582013-08-14 14:02:47 -04001057 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001058 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1059 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001060 }
1061
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001062 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001063
Chris Masonc8b97812008-10-29 14:49:59 -04001064 /* we're not doing compressed IO, don't unlock the first
1065 * page (which the caller expects to stay locked), don't
1066 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001067 *
1068 * Do set the Private2 bit so we know this page was properly
1069 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001070 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001071 page_ops = unlock ? PAGE_UNLOCK : 0;
1072 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001073
Josef Bacikc2790a22013-07-29 11:20:47 -04001074 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001075 start + ram_size - 1,
1076 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001077 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001078 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001079 if (num_bytes < cur_alloc_size)
1080 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001081 else
Anand Jain3752d222018-02-15 12:29:38 +08001082 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001083 alloc_hint = ins.objectid + ins.offset;
1084 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001085 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001086
1087 /*
1088 * btrfs_reloc_clone_csums() error, since start is increased
1089 * extent_clear_unlock_delalloc() at out_unlock label won't
1090 * free metadata of current ordered extent, we're OK to exit.
1091 */
1092 if (ret)
1093 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001094 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001095out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001096 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001097
Filipe Mananad9f85962014-08-25 10:43:00 +01001098out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001099 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001100out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001101 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001102 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001103out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001104 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1105 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001106 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1107 PAGE_END_WRITEBACK;
1108 /*
1109 * If we reserved an extent for our delalloc range (or a subrange) and
1110 * failed to create the respective ordered extent, then it means that
1111 * when we reserved the extent we decremented the extent's size from
1112 * the data space_info's bytes_may_use counter and incremented the
1113 * space_info's bytes_reserved counter by the same amount. We must make
1114 * sure extent_clear_unlock_delalloc() does not try to decrement again
1115 * the data space_info's bytes_may_use counter, therefore we do not pass
1116 * it the flag EXTENT_CLEAR_DATA_RESV.
1117 */
1118 if (extent_reserved) {
1119 extent_clear_unlock_delalloc(inode, start,
1120 start + cur_alloc_size,
1121 start + cur_alloc_size,
1122 locked_page,
1123 clear_bits,
1124 page_ops);
1125 start += cur_alloc_size;
1126 if (start >= end)
1127 goto out;
1128 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001129 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1130 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001131 clear_bits | EXTENT_CLEAR_DATA_RESV,
1132 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001133 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001134}
Chris Masonc8b97812008-10-29 14:49:59 -04001135
Chris Mason771ed682008-11-06 22:02:51 -05001136/*
1137 * work queue call back to started compression on a file and pages
1138 */
1139static noinline void async_cow_start(struct btrfs_work *work)
1140{
1141 struct async_cow *async_cow;
1142 int num_added = 0;
1143 async_cow = container_of(work, struct async_cow, work);
1144
1145 compress_file_range(async_cow->inode, async_cow->locked_page,
1146 async_cow->start, async_cow->end, async_cow,
1147 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001148 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001149 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001150 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001151 }
Chris Mason771ed682008-11-06 22:02:51 -05001152}
1153
1154/*
1155 * work queue call back to submit previously compressed pages
1156 */
1157static noinline void async_cow_submit(struct btrfs_work *work)
1158{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001159 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001160 struct async_cow *async_cow;
1161 struct btrfs_root *root;
1162 unsigned long nr_pages;
1163
1164 async_cow = container_of(work, struct async_cow, work);
1165
1166 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001167 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001168 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1169 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001170
David Sterbaee863952015-02-16 19:41:40 +01001171 /*
1172 * atomic_sub_return implies a barrier for waitqueue_active
1173 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001174 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001175 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001176 waitqueue_active(&fs_info->async_submit_wait))
1177 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001178
Chris Masond3977122009-01-05 21:25:51 -05001179 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001180 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001181}
Chris Masonc8b97812008-10-29 14:49:59 -04001182
Chris Mason771ed682008-11-06 22:02:51 -05001183static noinline void async_cow_free(struct btrfs_work *work)
1184{
1185 struct async_cow *async_cow;
1186 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001187 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001188 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001189 kfree(async_cow);
1190}
1191
1192static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1193 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001194 unsigned long *nr_written,
1195 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001196{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001197 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001198 struct async_cow *async_cow;
1199 struct btrfs_root *root = BTRFS_I(inode)->root;
1200 unsigned long nr_pages;
1201 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001202
Chris Masona3429ab2009-10-08 12:30:20 -04001203 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001204 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001205 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001206 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001207 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001208 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001209 async_cow->root = root;
1210 async_cow->locked_page = locked_page;
1211 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001212 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001213
Wang Shilongf79707b2014-07-17 11:44:09 +08001214 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001215 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001216 cur_end = end;
1217 else
Byongho Leeee221842015-12-15 01:42:10 +09001218 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001219
1220 async_cow->end = cur_end;
1221 INIT_LIST_HEAD(&async_cow->extents);
1222
Liu Bo9e0af232014-08-15 23:36:53 +08001223 btrfs_init_work(&async_cow->work,
1224 btrfs_delalloc_helper,
1225 async_cow_start, async_cow_submit,
1226 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001227
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001228 nr_pages = (cur_end - start + PAGE_SIZE) >>
1229 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001230 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001231
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001232 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001233
Chris Mason771ed682008-11-06 22:02:51 -05001234 *nr_written += nr_pages;
1235 start = cur_end + 1;
1236 }
1237 *page_started = 1;
1238 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001239}
1240
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001241static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001242 u64 bytenr, u64 num_bytes)
1243{
1244 int ret;
1245 struct btrfs_ordered_sum *sums;
1246 LIST_HEAD(list);
1247
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001248 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001249 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001250 if (ret == 0 && list_empty(&list))
1251 return 0;
1252
1253 while (!list_empty(&list)) {
1254 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1255 list_del(&sums->list);
1256 kfree(sums);
1257 }
Liu Bo58113752018-01-31 17:09:13 -07001258 if (ret < 0)
1259 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001260 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);
Liu Boe8916692018-01-25 11:02:50 -07001333 if (ret < 0) {
1334 if (cow_start != (u64)-1)
1335 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001336 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001337 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 if (ret > 0)
1339 break;
1340 leaf = path->nodes[0];
1341 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001342
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 nocow = 0;
1344 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001345 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001346 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001347
Filipe Manana1d512cb2015-11-09 00:33:58 +00001348 if (found_key.objectid > ino)
1349 break;
1350 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1351 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1352 path->slots[0]++;
1353 goto next_slot;
1354 }
1355 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001356 found_key.offset > end)
1357 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001358
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 if (found_key.offset > cur_offset) {
1360 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001361 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 goto out_check;
1363 }
Chris Masonc31f8832008-01-08 15:46:31 -05001364
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 fi = btrfs_item_ptr(leaf, path->slots[0],
1366 struct btrfs_file_extent_item);
1367 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001368
Josef Bacikcc95bef2013-04-04 14:31:27 -04001369 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001370 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1371 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001372 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001373 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001374 extent_end = found_key.offset +
1375 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001376 disk_num_bytes =
1377 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001378 if (extent_end <= start) {
1379 path->slots[0]++;
1380 goto next_slot;
1381 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001382 if (disk_bytenr == 0)
1383 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001384 if (btrfs_file_extent_compression(leaf, fi) ||
1385 btrfs_file_extent_encryption(leaf, fi) ||
1386 btrfs_file_extent_other_encoding(leaf, fi))
1387 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001388 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1389 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001390 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001391 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001392 ret = btrfs_cross_ref_exist(root, ino,
1393 found_key.offset -
1394 extent_offset, disk_bytenr);
1395 if (ret) {
1396 /*
1397 * ret could be -EIO if the above fails to read
1398 * metadata.
1399 */
1400 if (ret < 0) {
1401 if (cow_start != (u64)-1)
1402 cur_offset = cow_start;
1403 goto error;
1404 }
1405
1406 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001407 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001408 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001409 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001410 disk_bytenr += cur_offset - found_key.offset;
1411 num_bytes = min(end + 1, extent_end) - cur_offset;
1412 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001413 * if there are pending snapshots for this root,
1414 * we fall into common COW way.
1415 */
1416 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001417 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001418 if (!err)
1419 goto out_check;
1420 }
1421 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001422 * force cow if csum exists in the range.
1423 * this ensure that csum for a given extent are
1424 * either valid or do not exist.
1425 */
Liu Bo58113752018-01-31 17:09:13 -07001426 ret = csum_exist_in_range(fs_info, disk_bytenr,
1427 num_bytes);
1428 if (ret) {
Robbie Ko91e1f562016-10-07 10:01:29 +08001429 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001430 btrfs_end_write_no_snapshotting(root);
Liu Bo58113752018-01-31 17:09:13 -07001431
1432 /*
1433 * ret could be -EIO if the above fails to read
1434 * metadata.
1435 */
1436 if (ret < 0) {
1437 if (cow_start != (u64)-1)
1438 cur_offset = cow_start;
1439 goto error;
1440 }
1441 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001442 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001443 }
1444 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1445 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001446 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001447 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001448 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001449 nocow = 1;
1450 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1451 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001452 btrfs_file_extent_inline_len(leaf,
1453 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001454 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001455 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001456 } else {
1457 BUG_ON(1);
1458 }
1459out_check:
1460 if (extent_end <= start) {
1461 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001462 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001463 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001464 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001465 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001466 goto next_slot;
1467 }
1468 if (!nocow) {
1469 if (cow_start == (u64)-1)
1470 cow_start = cur_offset;
1471 cur_offset = extent_end;
1472 if (cur_offset > end)
1473 break;
1474 path->slots[0]++;
1475 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001476 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001477
David Sterbab3b4aa72011-04-21 01:20:15 +02001478 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001479 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001480 ret = cow_file_range(inode, locked_page,
1481 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001482 end, page_started, nr_written, 1,
1483 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001484 if (ret) {
1485 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001486 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001487 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001488 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001489 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001490 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001491 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001492 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001493 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001494
Yan Zhengd899e052008-10-30 14:25:28 -04001495 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001496 u64 orig_start = found_key.offset - extent_offset;
1497
1498 em = create_io_em(inode, cur_offset, num_bytes,
1499 orig_start,
1500 disk_bytenr, /* block_start */
1501 num_bytes, /* block_len */
1502 disk_num_bytes, /* orig_block_len */
1503 ram_bytes, BTRFS_COMPRESS_NONE,
1504 BTRFS_ORDERED_PREALLOC);
1505 if (IS_ERR(em)) {
1506 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001507 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001508 if (nocow)
1509 btrfs_dec_nocow_writers(fs_info,
1510 disk_bytenr);
1511 ret = PTR_ERR(em);
1512 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001513 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001514 free_extent_map(em);
1515 }
1516
1517 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001518 type = BTRFS_ORDERED_PREALLOC;
1519 } else {
1520 type = BTRFS_ORDERED_NOCOW;
1521 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001522
1523 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001524 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001525 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001526 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001527 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001528
Yan, Zhengefa56462010-05-16 10:49:59 -04001529 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001530 BTRFS_DATA_RELOC_TREE_OBJECTID)
1531 /*
1532 * Error handled later, as we must prevent
1533 * extent_clear_unlock_delalloc() in error handler
1534 * from freeing metadata of created ordered extent.
1535 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001536 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1537 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001538
Josef Bacikc2790a22013-07-29 11:20:47 -04001539 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001540 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001541 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001542 EXTENT_DELALLOC |
1543 EXTENT_CLEAR_DATA_RESV,
1544 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1545
Wang Shilonge9894fd2014-03-27 11:12:25 +08001546 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001547 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001548 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001549
1550 /*
1551 * btrfs_reloc_clone_csums() error, now we're OK to call error
1552 * handler, as metadata for created ordered extent will only
1553 * be freed by btrfs_finish_ordered_io().
1554 */
1555 if (ret)
1556 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001557 if (cur_offset > end)
1558 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001559 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001560 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001561
Josef Bacik17ca04a2012-05-31 15:58:55 -04001562 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001563 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001564 cur_offset = end;
1565 }
1566
Yan Zheng80ff3852008-10-30 14:20:02 -04001567 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001568 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1569 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001570 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001571 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001572 }
1573
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001574error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001575 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001576 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001577 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001578 EXTENT_DELALLOC | EXTENT_DEFRAG |
1579 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1580 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001581 PAGE_SET_WRITEBACK |
1582 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001583 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001584 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001585}
1586
Wang Shilong47059d92014-07-03 18:22:07 +08001587static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1588{
1589
1590 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1591 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1592 return 0;
1593
1594 /*
1595 * @defrag_bytes is a hint value, no spinlock held here,
1596 * if is not zero, it means the file is defragging.
1597 * Force cow if given extent needs to be defragged.
1598 */
1599 if (BTRFS_I(inode)->defrag_bytes &&
1600 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1601 EXTENT_DEFRAG, 0, NULL))
1602 return 1;
1603
1604 return 0;
1605}
1606
Chris Masond352ac62008-09-29 15:18:18 -04001607/*
1608 * extent_io.c call back to do delayed allocation processing
1609 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001610static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001611 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001612 unsigned long *nr_written,
1613 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001614{
Josef Bacikc6100a42017-05-05 11:57:13 -04001615 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001616 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001617 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001618 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001619
Wang Shilong47059d92014-07-03 18:22:07 +08001620 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001621 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001622 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001623 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001624 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001625 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001626 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001627 ret = cow_file_range(inode, locked_page, start, end, end,
1628 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001629 } else {
1630 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1631 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001632 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001633 page_started, nr_written,
1634 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001635 }
Qu Wenruo524272602017-03-08 10:25:52 +08001636 if (ret)
1637 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001638 return ret;
1639}
1640
Josef Bacikc6100a42017-05-05 11:57:13 -04001641static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001642 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001643{
Josef Bacikc6100a42017-05-05 11:57:13 -04001644 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001645 u64 size;
1646
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001647 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001648 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001649 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001650
Josef Bacikdcab6a32015-02-11 15:08:59 -05001651 size = orig->end - orig->start + 1;
1652 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001653 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001654 u64 new_size;
1655
1656 /*
Josef Bacikba117212015-03-13 15:01:24 -04001657 * See the explanation in btrfs_merge_extent_hook, the same
1658 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001659 */
1660 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001661 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001662 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001663 num_extents += count_max_extents(new_size);
1664 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001665 return;
1666 }
1667
Josef Bacik9e0baf62011-07-15 15:16:44 +00001668 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001669 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001670 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001671}
1672
1673/*
1674 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1675 * extents so we can keep track of new extents that are just merged onto old
1676 * extents, such as when we are doing sequential writes, so we can properly
1677 * account for the metadata space we'll need.
1678 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001679static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001680 struct extent_state *new,
1681 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001682{
Josef Bacikc6100a42017-05-05 11:57:13 -04001683 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001684 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001685 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001686
Josef Bacik9ed74f22009-09-11 16:12:44 -04001687 /* not delalloc, ignore it */
1688 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001689 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001690
Josef Bacik8461a3d2015-03-13 15:12:08 -04001691 if (new->start > other->start)
1692 new_size = new->end - other->start + 1;
1693 else
1694 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001695
1696 /* we're not bigger than the max, unreserve the space and go */
1697 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1698 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001699 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001700 spin_unlock(&BTRFS_I(inode)->lock);
1701 return;
1702 }
1703
1704 /*
Josef Bacikba117212015-03-13 15:01:24 -04001705 * We have to add up either side to figure out how many extents were
1706 * accounted for before we merged into one big extent. If the number of
1707 * extents we accounted for is <= the amount we need for the new range
1708 * then we can return, otherwise drop. Think of it like this
1709 *
1710 * [ 4k][MAX_SIZE]
1711 *
1712 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1713 * need 2 outstanding extents, on one side we have 1 and the other side
1714 * we have 1 so they are == and we can return. But in this case
1715 *
1716 * [MAX_SIZE+4k][MAX_SIZE+4k]
1717 *
1718 * Each range on their own accounts for 2 extents, but merged together
1719 * they are only 3 extents worth of accounting, so we need to drop in
1720 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001721 */
Josef Bacikba117212015-03-13 15:01:24 -04001722 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001723 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001724 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001725 num_extents += count_max_extents(old_size);
1726 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001727 return;
1728
Josef Bacik9e0baf62011-07-15 15:16:44 +00001729 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001730 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001731 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001732}
1733
Miao Xieeb73c1b2013-05-15 07:48:22 +00001734static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1735 struct inode *inode)
1736{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001737 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1738
Miao Xieeb73c1b2013-05-15 07:48:22 +00001739 spin_lock(&root->delalloc_lock);
1740 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1741 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1742 &root->delalloc_inodes);
1743 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1744 &BTRFS_I(inode)->runtime_flags);
1745 root->nr_delalloc_inodes++;
1746 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001747 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001748 BUG_ON(!list_empty(&root->delalloc_root));
1749 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001750 &fs_info->delalloc_roots);
1751 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001752 }
1753 }
1754 spin_unlock(&root->delalloc_lock);
1755}
1756
1757static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001758 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001759{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001760 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001761
Miao Xieeb73c1b2013-05-15 07:48:22 +00001762 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001763 if (!list_empty(&inode->delalloc_inodes)) {
1764 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001765 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001766 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001767 root->nr_delalloc_inodes--;
1768 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001769 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001770 BUG_ON(list_empty(&root->delalloc_root));
1771 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001772 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001773 }
1774 }
1775 spin_unlock(&root->delalloc_lock);
1776}
1777
Chris Masond352ac62008-09-29 15:18:18 -04001778/*
1779 * extent_io.c set_bit_hook, used to track delayed allocation
1780 * bytes in this file, and to maintain the list of inodes that
1781 * have pending delalloc work to be done.
1782 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001783static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001784 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001785{
Josef Bacikc6100a42017-05-05 11:57:13 -04001786 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001787
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001788 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1789
Wang Shilong47059d92014-07-03 18:22:07 +08001790 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1791 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001792 /*
1793 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001794 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001795 * bit, which is only set or cleared with irqs on
1796 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001797 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001798 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001799 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001800 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001801 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001802
Josef Bacik8b62f872017-10-19 14:15:55 -04001803 spin_lock(&BTRFS_I(inode)->lock);
1804 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1805 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001806
Josef Bacik6a3891c2015-03-16 17:38:52 -04001807 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001808 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001809 return;
1810
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001811 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1812 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001813 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001814 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001815 if (*bits & EXTENT_DEFRAG)
1816 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001817 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001818 &BTRFS_I(inode)->runtime_flags))
1819 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001820 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001821 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001822
1823 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1824 (*bits & EXTENT_DELALLOC_NEW)) {
1825 spin_lock(&BTRFS_I(inode)->lock);
1826 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1827 state->start;
1828 spin_unlock(&BTRFS_I(inode)->lock);
1829 }
Chris Mason291d6732008-01-29 15:55:23 -05001830}
1831
Chris Masond352ac62008-09-29 15:18:18 -04001832/*
1833 * extent_io.c clear_bit_hook, see set_bit_hook for why
1834 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001835static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001836 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001837 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001838{
Josef Bacikc6100a42017-05-05 11:57:13 -04001839 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001840 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001841 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001842 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001843
Filipe Manana4a4b9642017-07-27 19:52:55 +01001844 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1845 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001846 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001847 spin_unlock(&inode->lock);
1848 }
Wang Shilong47059d92014-07-03 18:22:07 +08001849
Chris Mason75eff682008-12-15 15:54:40 -05001850 /*
1851 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001852 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001853 * bit, which is only set or cleared with irqs on
1854 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001855 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001856 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001857 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001858
Josef Bacik8b62f872017-10-19 14:15:55 -04001859 spin_lock(&inode->lock);
1860 btrfs_mod_outstanding_extents(inode, -num_extents);
1861 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001862
Josef Bacikb6d08f02013-09-27 14:57:43 -04001863 /*
1864 * We don't reserve metadata space for space cache inodes so we
1865 * don't need to call dellalloc_release_metadata if there is an
1866 * error.
1867 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001868 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001869 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001870 btrfs_delalloc_release_metadata(inode, len);
1871
Josef Bacik6a3891c2015-03-16 17:38:52 -04001872 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001873 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001874 return;
1875
Filipe Mananaa315e682017-03-06 23:04:20 +00001876 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1877 do_list && !(state->state & EXTENT_NORESERVE) &&
1878 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001879 btrfs_free_reserved_data_space_noquota(
1880 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001881 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001882
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001883 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1884 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001885 spin_lock(&inode->lock);
1886 inode->delalloc_bytes -= len;
1887 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001888 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001889 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001890 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001891 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001892 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001893
1894 if ((state->state & EXTENT_DELALLOC_NEW) &&
1895 (*bits & EXTENT_DELALLOC_NEW)) {
1896 spin_lock(&inode->lock);
1897 ASSERT(inode->new_delalloc_bytes >= len);
1898 inode->new_delalloc_bytes -= len;
1899 spin_unlock(&inode->lock);
1900 }
Chris Mason291d6732008-01-29 15:55:23 -05001901}
1902
Chris Masond352ac62008-09-29 15:18:18 -04001903/*
1904 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1905 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001906 *
1907 * return 1 if page cannot be merged to bio
1908 * return 0 if page can be merged to bio
1909 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001910 */
Mike Christie81a75f672016-06-05 14:31:54 -05001911int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001912 size_t size, struct bio *bio,
1913 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001914{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001915 struct inode *inode = page->mapping->host;
1916 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001917 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001918 u64 length = 0;
1919 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001920 int ret;
1921
Chris Mason771ed682008-11-06 22:02:51 -05001922 if (bio_flags & EXTENT_BIO_COMPRESSED)
1923 return 0;
1924
Kent Overstreet4f024f32013-10-11 15:44:27 -07001925 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001926 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001927 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1928 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001929 if (ret < 0)
1930 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001931 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001932 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001933 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001934}
1935
Chris Masond352ac62008-09-29 15:18:18 -04001936/*
1937 * in order to insert checksums into the metadata in large chunks,
1938 * we wait until bio submission time. All the pages in the bio are
1939 * checksummed and sums are attached onto the ordered extent record.
1940 *
1941 * At IO completion time the cums attached on the ordered extent record
1942 * are inserted into the btree
1943 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001944static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001945 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001946{
Josef Bacikc6100a42017-05-05 11:57:13 -04001947 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001948 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001949
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001950 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001951 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001952 return 0;
1953}
Chris Masone0156402008-04-16 11:15:20 -04001954
Chris Mason4a69a412008-11-06 22:03:00 -05001955/*
1956 * in order to insert checksums into the metadata in large chunks,
1957 * we wait until bio submission time. All the pages in the bio are
1958 * checksummed and sums are attached onto the ordered extent record.
1959 *
1960 * At IO completion time the cums attached on the ordered extent record
1961 * are inserted into the btree
1962 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001963static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001964 int mirror_num, unsigned long bio_flags,
1965 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001966{
Josef Bacikc6100a42017-05-05 11:57:13 -04001967 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001968 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001969 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001970
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001971 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001972 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001973 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001974 bio_endio(bio);
1975 }
Stefan Behrens61891922012-11-05 18:51:52 +01001976 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001977}
1978
Chris Masond352ac62008-09-29 15:18:18 -04001979/*
Chris Masoncad321a2008-12-17 14:51:42 -05001980 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001981 * on write, or reading the csums from the tree before a read.
1982 *
1983 * Rules about async/sync submit,
1984 * a) read: sync submit
1985 *
1986 * b) write without checksum: sync submit
1987 *
1988 * c) write with checksum:
1989 * c-1) if bio is issued by fsync: sync submit
1990 * (sync_writers != 0)
1991 *
1992 * c-2) if root is reloc root: sync submit
1993 * (only in case of buffered IO)
1994 *
1995 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001996 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001997static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001998 int mirror_num, unsigned long bio_flags,
1999 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04002000{
Josef Bacikc6100a42017-05-05 11:57:13 -04002001 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002002 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002003 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302004 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002005 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002006 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002007 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002008
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002009 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002010
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002011 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302012 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002013
Mike Christie37226b22016-06-05 14:31:52 -05002014 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002015 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002016 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002017 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002018
Chris Masond20f7042008-12-08 16:58:54 -05002019 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002020 ret = btrfs_submit_compressed_read(inode, bio,
2021 mirror_num,
2022 bio_flags);
2023 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002024 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002025 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002026 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002027 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002028 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002029 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002030 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002031 /* csum items have already been cloned */
2032 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2033 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002034 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002035 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2036 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002037 __btrfs_submit_bio_start,
2038 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002039 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002040 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002041 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002042 if (ret)
2043 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002044 }
2045
Chris Mason0b86a832008-03-24 15:01:56 -04002046mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002047 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002048
2049out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002050 if (ret) {
2051 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002052 bio_endio(bio);
2053 }
Stefan Behrens61891922012-11-05 18:51:52 +01002054 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002055}
Chris Mason6885f302008-02-20 16:11:05 -05002056
Chris Masond352ac62008-09-29 15:18:18 -04002057/*
2058 * given a list of ordered sums record them in the inode. This happens
2059 * at IO completion time based on sums calculated at bio submission time.
2060 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002061static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002062 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002063{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002064 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002065 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002066
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002067 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002068 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002069 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002070 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002071 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002072 if (ret)
2073 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002074 }
2075 return 0;
2076}
2077
Josef Bacik2ac55d42010-02-03 19:33:23 +00002078int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002079 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002080 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002081{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002082 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002083 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002084 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002085}
2086
Chris Masond352ac62008-09-29 15:18:18 -04002087/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002088struct btrfs_writepage_fixup {
2089 struct page *page;
2090 struct btrfs_work work;
2091};
2092
Christoph Hellwigb2950862008-12-02 09:54:17 -05002093static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002094{
2095 struct btrfs_writepage_fixup *fixup;
2096 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002097 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002098 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002099 struct page *page;
2100 struct inode *inode;
2101 u64 page_start;
2102 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002103 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002104
2105 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2106 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002107again:
Chris Mason247e7432008-07-17 12:53:51 -04002108 lock_page(page);
2109 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2110 ClearPageChecked(page);
2111 goto out_page;
2112 }
2113
2114 inode = page->mapping->host;
2115 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002116 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002117
David Sterbaff13db42015-12-03 14:30:40 +01002118 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002119 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002120
2121 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002122 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002123 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002124
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002125 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002126 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002127 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002128 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002129 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002130 unlock_page(page);
2131 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002132 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002133 goto again;
2134 }
Chris Mason247e7432008-07-17 12:53:51 -04002135
Qu Wenruo364ecf32017-02-27 15:10:38 +08002136 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002137 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002138 if (ret) {
2139 mapping_set_error(page->mapping, ret);
2140 end_extent_writepage(page, ret, page_start, page_end);
2141 ClearPageChecked(page);
2142 goto out;
2143 }
2144
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002145 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2146 &cached_state, 0);
2147 if (ret) {
2148 mapping_set_error(page->mapping, ret);
2149 end_extent_writepage(page, ret, page_start, page_end);
2150 ClearPageChecked(page);
2151 goto out;
2152 }
2153
Chris Mason247e7432008-07-17 12:53:51 -04002154 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002155 set_page_dirty(page);
Josef Bacik8b62f872017-10-19 14:15:55 -04002156 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002157out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002158 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002159 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002160out_page:
2161 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002162 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002163 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002164 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002165}
2166
2167/*
2168 * There are a few paths in the higher layers of the kernel that directly
2169 * set the page dirty bit without asking the filesystem if it is a
2170 * good idea. This causes problems because we want to make sure COW
2171 * properly happens and the data=ordered rules are followed.
2172 *
Chris Masonc8b97812008-10-29 14:49:59 -04002173 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002174 * hasn't been properly setup for IO. We kick off an async process
2175 * to fix it up. The async helper will wait for ordered extents, set
2176 * the delalloc bit and make it safe to write the page.
2177 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002178static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002179{
2180 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002181 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002182 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002183
Chris Mason8b62b722009-09-02 16:53:46 -04002184 /* this page is properly in the ordered list */
2185 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002186 return 0;
2187
2188 if (PageChecked(page))
2189 return -EAGAIN;
2190
2191 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2192 if (!fixup)
2193 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002194
Chris Mason247e7432008-07-17 12:53:51 -04002195 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002196 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002197 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2198 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002199 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002200 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002201 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002202}
2203
Yan Zhengd899e052008-10-30 14:25:28 -04002204static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2205 struct inode *inode, u64 file_pos,
2206 u64 disk_bytenr, u64 disk_num_bytes,
2207 u64 num_bytes, u64 ram_bytes,
2208 u8 compression, u8 encryption,
2209 u16 other_encoding, int extent_type)
2210{
2211 struct btrfs_root *root = BTRFS_I(inode)->root;
2212 struct btrfs_file_extent_item *fi;
2213 struct btrfs_path *path;
2214 struct extent_buffer *leaf;
2215 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002216 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002217 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002218 int ret;
2219
2220 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002221 if (!path)
2222 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002223
Chris Masona1ed8352009-09-11 12:27:37 -04002224 /*
2225 * we may be replacing one extent in the tree with another.
2226 * The new extent is pinned in the extent map, and we don't want
2227 * to drop it from the cache until it is completely in the btree.
2228 *
2229 * So, tell btrfs_drop_extents to leave this extent in the cache.
2230 * the caller is expected to unpin it and allow it to be merged
2231 * with the others.
2232 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002233 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2234 file_pos + num_bytes, NULL, 0,
2235 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002236 if (ret)
2237 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002238
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002239 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002240 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002241 ins.offset = file_pos;
2242 ins.type = BTRFS_EXTENT_DATA_KEY;
2243
2244 path->leave_spinning = 1;
2245 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2246 sizeof(*fi));
2247 if (ret)
2248 goto out;
2249 }
Yan Zhengd899e052008-10-30 14:25:28 -04002250 leaf = path->nodes[0];
2251 fi = btrfs_item_ptr(leaf, path->slots[0],
2252 struct btrfs_file_extent_item);
2253 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2254 btrfs_set_file_extent_type(leaf, fi, extent_type);
2255 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2256 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2257 btrfs_set_file_extent_offset(leaf, fi, 0);
2258 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2259 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2260 btrfs_set_file_extent_compression(leaf, fi, compression);
2261 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2262 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002263
Yan Zhengd899e052008-10-30 14:25:28 -04002264 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002265 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002266
2267 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002268
2269 ins.objectid = disk_bytenr;
2270 ins.offset = disk_num_bytes;
2271 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002272
Qu Wenruo297d7502015-09-08 17:08:37 +08002273 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002274 * Release the reserved range from inode dirty range map, as it is
2275 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002276 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002277 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2278 if (ret < 0)
2279 goto out;
2280 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002281 ret = btrfs_alloc_reserved_file_extent(trans, root,
2282 btrfs_ino(BTRFS_I(inode)),
2283 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002284out:
Yan Zhengd899e052008-10-30 14:25:28 -04002285 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002286
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002287 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002288}
2289
Liu Bo38c227d2013-01-29 03:18:40 +00002290/* snapshot-aware defrag */
2291struct sa_defrag_extent_backref {
2292 struct rb_node node;
2293 struct old_sa_defrag_extent *old;
2294 u64 root_id;
2295 u64 inum;
2296 u64 file_pos;
2297 u64 extent_offset;
2298 u64 num_bytes;
2299 u64 generation;
2300};
2301
2302struct old_sa_defrag_extent {
2303 struct list_head list;
2304 struct new_sa_defrag_extent *new;
2305
2306 u64 extent_offset;
2307 u64 bytenr;
2308 u64 offset;
2309 u64 len;
2310 int count;
2311};
2312
2313struct new_sa_defrag_extent {
2314 struct rb_root root;
2315 struct list_head head;
2316 struct btrfs_path *path;
2317 struct inode *inode;
2318 u64 file_pos;
2319 u64 len;
2320 u64 bytenr;
2321 u64 disk_len;
2322 u8 compress_type;
2323};
2324
2325static int backref_comp(struct sa_defrag_extent_backref *b1,
2326 struct sa_defrag_extent_backref *b2)
2327{
2328 if (b1->root_id < b2->root_id)
2329 return -1;
2330 else if (b1->root_id > b2->root_id)
2331 return 1;
2332
2333 if (b1->inum < b2->inum)
2334 return -1;
2335 else if (b1->inum > b2->inum)
2336 return 1;
2337
2338 if (b1->file_pos < b2->file_pos)
2339 return -1;
2340 else if (b1->file_pos > b2->file_pos)
2341 return 1;
2342
2343 /*
2344 * [------------------------------] ===> (a range of space)
2345 * |<--->| |<---->| =============> (fs/file tree A)
2346 * |<---------------------------->| ===> (fs/file tree B)
2347 *
2348 * A range of space can refer to two file extents in one tree while
2349 * refer to only one file extent in another tree.
2350 *
2351 * So we may process a disk offset more than one time(two extents in A)
2352 * and locate at the same extent(one extent in B), then insert two same
2353 * backrefs(both refer to the extent in B).
2354 */
2355 return 0;
2356}
2357
2358static void backref_insert(struct rb_root *root,
2359 struct sa_defrag_extent_backref *backref)
2360{
2361 struct rb_node **p = &root->rb_node;
2362 struct rb_node *parent = NULL;
2363 struct sa_defrag_extent_backref *entry;
2364 int ret;
2365
2366 while (*p) {
2367 parent = *p;
2368 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2369
2370 ret = backref_comp(backref, entry);
2371 if (ret < 0)
2372 p = &(*p)->rb_left;
2373 else
2374 p = &(*p)->rb_right;
2375 }
2376
2377 rb_link_node(&backref->node, parent, p);
2378 rb_insert_color(&backref->node, root);
2379}
2380
2381/*
2382 * Note the backref might has changed, and in this case we just return 0.
2383 */
2384static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2385 void *ctx)
2386{
2387 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002388 struct old_sa_defrag_extent *old = ctx;
2389 struct new_sa_defrag_extent *new = old->new;
2390 struct btrfs_path *path = new->path;
2391 struct btrfs_key key;
2392 struct btrfs_root *root;
2393 struct sa_defrag_extent_backref *backref;
2394 struct extent_buffer *leaf;
2395 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002396 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002397 int slot;
2398 int ret;
2399 u64 extent_offset;
2400 u64 num_bytes;
2401
2402 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002403 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002404 return 0;
2405
2406 key.objectid = root_id;
2407 key.type = BTRFS_ROOT_ITEM_KEY;
2408 key.offset = (u64)-1;
2409
Liu Bo38c227d2013-01-29 03:18:40 +00002410 root = btrfs_read_fs_root_no_name(fs_info, &key);
2411 if (IS_ERR(root)) {
2412 if (PTR_ERR(root) == -ENOENT)
2413 return 0;
2414 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002415 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002416 inum, offset, root_id);
2417 return PTR_ERR(root);
2418 }
2419
2420 key.objectid = inum;
2421 key.type = BTRFS_EXTENT_DATA_KEY;
2422 if (offset > (u64)-1 << 32)
2423 key.offset = 0;
2424 else
2425 key.offset = offset;
2426
2427 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302428 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002429 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002430 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002431
2432 while (1) {
2433 cond_resched();
2434
2435 leaf = path->nodes[0];
2436 slot = path->slots[0];
2437
2438 if (slot >= btrfs_header_nritems(leaf)) {
2439 ret = btrfs_next_leaf(root, path);
2440 if (ret < 0) {
2441 goto out;
2442 } else if (ret > 0) {
2443 ret = 0;
2444 goto out;
2445 }
2446 continue;
2447 }
2448
2449 path->slots[0]++;
2450
2451 btrfs_item_key_to_cpu(leaf, &key, slot);
2452
2453 if (key.objectid > inum)
2454 goto out;
2455
2456 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2457 continue;
2458
2459 extent = btrfs_item_ptr(leaf, slot,
2460 struct btrfs_file_extent_item);
2461
2462 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2463 continue;
2464
Liu Boe68afa42013-07-01 22:13:26 +08002465 /*
2466 * 'offset' refers to the exact key.offset,
2467 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2468 * (key.offset - extent_offset).
2469 */
2470 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002471 continue;
2472
Liu Boe68afa42013-07-01 22:13:26 +08002473 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002474 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002475
Liu Bo38c227d2013-01-29 03:18:40 +00002476 if (extent_offset >= old->extent_offset + old->offset +
2477 old->len || extent_offset + num_bytes <=
2478 old->extent_offset + old->offset)
2479 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002480 break;
2481 }
2482
2483 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2484 if (!backref) {
2485 ret = -ENOENT;
2486 goto out;
2487 }
2488
2489 backref->root_id = root_id;
2490 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002491 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002492 backref->num_bytes = num_bytes;
2493 backref->extent_offset = extent_offset;
2494 backref->generation = btrfs_file_extent_generation(leaf, extent);
2495 backref->old = old;
2496 backref_insert(&new->root, backref);
2497 old->count++;
2498out:
2499 btrfs_release_path(path);
2500 WARN_ON(ret);
2501 return ret;
2502}
2503
2504static noinline bool record_extent_backrefs(struct btrfs_path *path,
2505 struct new_sa_defrag_extent *new)
2506{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002507 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002508 struct old_sa_defrag_extent *old, *tmp;
2509 int ret;
2510
2511 new->path = path;
2512
2513 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002514 ret = iterate_inodes_from_logical(old->bytenr +
2515 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002516 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002517 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002518 if (ret < 0 && ret != -ENOENT)
2519 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002520
2521 /* no backref to be processed for this extent */
2522 if (!old->count) {
2523 list_del(&old->list);
2524 kfree(old);
2525 }
2526 }
2527
2528 if (list_empty(&new->head))
2529 return false;
2530
2531 return true;
2532}
2533
2534static int relink_is_mergable(struct extent_buffer *leaf,
2535 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002536 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002537{
Liu Bo116e0022013-08-02 16:30:40 +08002538 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002539 return 0;
2540
2541 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2542 return 0;
2543
Liu Bo116e0022013-08-02 16:30:40 +08002544 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2545 return 0;
2546
2547 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002548 btrfs_file_extent_other_encoding(leaf, fi))
2549 return 0;
2550
2551 return 1;
2552}
2553
2554/*
2555 * Note the backref might has changed, and in this case we just return 0.
2556 */
2557static noinline int relink_extent_backref(struct btrfs_path *path,
2558 struct sa_defrag_extent_backref *prev,
2559 struct sa_defrag_extent_backref *backref)
2560{
2561 struct btrfs_file_extent_item *extent;
2562 struct btrfs_file_extent_item *item;
2563 struct btrfs_ordered_extent *ordered;
2564 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002565 struct btrfs_root *root;
2566 struct btrfs_key key;
2567 struct extent_buffer *leaf;
2568 struct old_sa_defrag_extent *old = backref->old;
2569 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002570 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002571 struct inode *inode;
2572 struct extent_state *cached = NULL;
2573 int ret = 0;
2574 u64 start;
2575 u64 len;
2576 u64 lock_start;
2577 u64 lock_end;
2578 bool merge = false;
2579 int index;
2580
2581 if (prev && prev->root_id == backref->root_id &&
2582 prev->inum == backref->inum &&
2583 prev->file_pos + prev->num_bytes == backref->file_pos)
2584 merge = true;
2585
2586 /* step 1: get root */
2587 key.objectid = backref->root_id;
2588 key.type = BTRFS_ROOT_ITEM_KEY;
2589 key.offset = (u64)-1;
2590
Liu Bo38c227d2013-01-29 03:18:40 +00002591 index = srcu_read_lock(&fs_info->subvol_srcu);
2592
2593 root = btrfs_read_fs_root_no_name(fs_info, &key);
2594 if (IS_ERR(root)) {
2595 srcu_read_unlock(&fs_info->subvol_srcu, index);
2596 if (PTR_ERR(root) == -ENOENT)
2597 return 0;
2598 return PTR_ERR(root);
2599 }
Liu Bo38c227d2013-01-29 03:18:40 +00002600
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002601 if (btrfs_root_readonly(root)) {
2602 srcu_read_unlock(&fs_info->subvol_srcu, index);
2603 return 0;
2604 }
2605
Liu Bo38c227d2013-01-29 03:18:40 +00002606 /* step 2: get inode */
2607 key.objectid = backref->inum;
2608 key.type = BTRFS_INODE_ITEM_KEY;
2609 key.offset = 0;
2610
2611 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2612 if (IS_ERR(inode)) {
2613 srcu_read_unlock(&fs_info->subvol_srcu, index);
2614 return 0;
2615 }
2616
2617 srcu_read_unlock(&fs_info->subvol_srcu, index);
2618
2619 /* step 3: relink backref */
2620 lock_start = backref->file_pos;
2621 lock_end = backref->file_pos + backref->num_bytes - 1;
2622 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002623 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002624
2625 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2626 if (ordered) {
2627 btrfs_put_ordered_extent(ordered);
2628 goto out_unlock;
2629 }
2630
2631 trans = btrfs_join_transaction(root);
2632 if (IS_ERR(trans)) {
2633 ret = PTR_ERR(trans);
2634 goto out_unlock;
2635 }
2636
2637 key.objectid = backref->inum;
2638 key.type = BTRFS_EXTENT_DATA_KEY;
2639 key.offset = backref->file_pos;
2640
2641 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2642 if (ret < 0) {
2643 goto out_free_path;
2644 } else if (ret > 0) {
2645 ret = 0;
2646 goto out_free_path;
2647 }
2648
2649 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2650 struct btrfs_file_extent_item);
2651
2652 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2653 backref->generation)
2654 goto out_free_path;
2655
2656 btrfs_release_path(path);
2657
2658 start = backref->file_pos;
2659 if (backref->extent_offset < old->extent_offset + old->offset)
2660 start += old->extent_offset + old->offset -
2661 backref->extent_offset;
2662
2663 len = min(backref->extent_offset + backref->num_bytes,
2664 old->extent_offset + old->offset + old->len);
2665 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2666
2667 ret = btrfs_drop_extents(trans, root, inode, start,
2668 start + len, 1);
2669 if (ret)
2670 goto out_free_path;
2671again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002672 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002673 key.type = BTRFS_EXTENT_DATA_KEY;
2674 key.offset = start;
2675
Liu Boa09a0a72013-03-11 09:20:58 +00002676 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002677 if (merge) {
2678 struct btrfs_file_extent_item *fi;
2679 u64 extent_len;
2680 struct btrfs_key found_key;
2681
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002682 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002683 if (ret < 0)
2684 goto out_free_path;
2685
2686 path->slots[0]--;
2687 leaf = path->nodes[0];
2688 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2689
2690 fi = btrfs_item_ptr(leaf, path->slots[0],
2691 struct btrfs_file_extent_item);
2692 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2693
Liu Bo116e0022013-08-02 16:30:40 +08002694 if (extent_len + found_key.offset == start &&
2695 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002696 btrfs_set_file_extent_num_bytes(leaf, fi,
2697 extent_len + len);
2698 btrfs_mark_buffer_dirty(leaf);
2699 inode_add_bytes(inode, len);
2700
2701 ret = 1;
2702 goto out_free_path;
2703 } else {
2704 merge = false;
2705 btrfs_release_path(path);
2706 goto again;
2707 }
2708 }
2709
2710 ret = btrfs_insert_empty_item(trans, root, path, &key,
2711 sizeof(*extent));
2712 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002713 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002714 goto out_free_path;
2715 }
2716
2717 leaf = path->nodes[0];
2718 item = btrfs_item_ptr(leaf, path->slots[0],
2719 struct btrfs_file_extent_item);
2720 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2721 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2722 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2723 btrfs_set_file_extent_num_bytes(leaf, item, len);
2724 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2725 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2726 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2727 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2728 btrfs_set_file_extent_encryption(leaf, item, 0);
2729 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2730
2731 btrfs_mark_buffer_dirty(leaf);
2732 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002733 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002734
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002735 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002736 new->disk_len, 0,
2737 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002738 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002739 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002740 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002741 goto out_free_path;
2742 }
2743
2744 ret = 1;
2745out_free_path:
2746 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002747 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002748 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002749out_unlock:
2750 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002751 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002752 iput(inode);
2753 return ret;
2754}
2755
Liu Bo6f519562013-10-29 10:45:05 +08002756static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2757{
2758 struct old_sa_defrag_extent *old, *tmp;
2759
2760 if (!new)
2761 return;
2762
2763 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002764 kfree(old);
2765 }
2766 kfree(new);
2767}
2768
Liu Bo38c227d2013-01-29 03:18:40 +00002769static void relink_file_extents(struct new_sa_defrag_extent *new)
2770{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002771 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002772 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002773 struct sa_defrag_extent_backref *backref;
2774 struct sa_defrag_extent_backref *prev = NULL;
2775 struct inode *inode;
2776 struct btrfs_root *root;
2777 struct rb_node *node;
2778 int ret;
2779
2780 inode = new->inode;
2781 root = BTRFS_I(inode)->root;
2782
2783 path = btrfs_alloc_path();
2784 if (!path)
2785 return;
2786
2787 if (!record_extent_backrefs(path, new)) {
2788 btrfs_free_path(path);
2789 goto out;
2790 }
2791 btrfs_release_path(path);
2792
2793 while (1) {
2794 node = rb_first(&new->root);
2795 if (!node)
2796 break;
2797 rb_erase(node, &new->root);
2798
2799 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2800
2801 ret = relink_extent_backref(path, prev, backref);
2802 WARN_ON(ret < 0);
2803
2804 kfree(prev);
2805
2806 if (ret == 1)
2807 prev = backref;
2808 else
2809 prev = NULL;
2810 cond_resched();
2811 }
2812 kfree(prev);
2813
2814 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002815out:
Liu Bo6f519562013-10-29 10:45:05 +08002816 free_sa_defrag_extent(new);
2817
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002818 atomic_dec(&fs_info->defrag_running);
2819 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002820}
2821
2822static struct new_sa_defrag_extent *
2823record_old_file_extents(struct inode *inode,
2824 struct btrfs_ordered_extent *ordered)
2825{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002826 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002827 struct btrfs_root *root = BTRFS_I(inode)->root;
2828 struct btrfs_path *path;
2829 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002830 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002831 struct new_sa_defrag_extent *new;
2832 int ret;
2833
2834 new = kmalloc(sizeof(*new), GFP_NOFS);
2835 if (!new)
2836 return NULL;
2837
2838 new->inode = inode;
2839 new->file_pos = ordered->file_offset;
2840 new->len = ordered->len;
2841 new->bytenr = ordered->start;
2842 new->disk_len = ordered->disk_len;
2843 new->compress_type = ordered->compress_type;
2844 new->root = RB_ROOT;
2845 INIT_LIST_HEAD(&new->head);
2846
2847 path = btrfs_alloc_path();
2848 if (!path)
2849 goto out_kfree;
2850
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002851 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002852 key.type = BTRFS_EXTENT_DATA_KEY;
2853 key.offset = new->file_pos;
2854
2855 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2856 if (ret < 0)
2857 goto out_free_path;
2858 if (ret > 0 && path->slots[0] > 0)
2859 path->slots[0]--;
2860
2861 /* find out all the old extents for the file range */
2862 while (1) {
2863 struct btrfs_file_extent_item *extent;
2864 struct extent_buffer *l;
2865 int slot;
2866 u64 num_bytes;
2867 u64 offset;
2868 u64 end;
2869 u64 disk_bytenr;
2870 u64 extent_offset;
2871
2872 l = path->nodes[0];
2873 slot = path->slots[0];
2874
2875 if (slot >= btrfs_header_nritems(l)) {
2876 ret = btrfs_next_leaf(root, path);
2877 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002878 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002879 else if (ret > 0)
2880 break;
2881 continue;
2882 }
2883
2884 btrfs_item_key_to_cpu(l, &key, slot);
2885
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002886 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002887 break;
2888 if (key.type != BTRFS_EXTENT_DATA_KEY)
2889 break;
2890 if (key.offset >= new->file_pos + new->len)
2891 break;
2892
2893 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2894
2895 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2896 if (key.offset + num_bytes < new->file_pos)
2897 goto next;
2898
2899 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2900 if (!disk_bytenr)
2901 goto next;
2902
2903 extent_offset = btrfs_file_extent_offset(l, extent);
2904
2905 old = kmalloc(sizeof(*old), GFP_NOFS);
2906 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002907 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002908
2909 offset = max(new->file_pos, key.offset);
2910 end = min(new->file_pos + new->len, key.offset + num_bytes);
2911
2912 old->bytenr = disk_bytenr;
2913 old->extent_offset = extent_offset;
2914 old->offset = offset - key.offset;
2915 old->len = end - offset;
2916 old->new = new;
2917 old->count = 0;
2918 list_add_tail(&old->list, &new->head);
2919next:
2920 path->slots[0]++;
2921 cond_resched();
2922 }
2923
2924 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002925 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002926
2927 return new;
2928
Liu Bo38c227d2013-01-29 03:18:40 +00002929out_free_path:
2930 btrfs_free_path(path);
2931out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002932 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002933 return NULL;
2934}
2935
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002936static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002937 u64 start, u64 len)
2938{
2939 struct btrfs_block_group_cache *cache;
2940
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002941 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002942 ASSERT(cache);
2943
2944 spin_lock(&cache->lock);
2945 cache->delalloc_bytes -= len;
2946 spin_unlock(&cache->lock);
2947
2948 btrfs_put_block_group(cache);
2949}
2950
Chris Masond352ac62008-09-29 15:18:18 -04002951/* as ordered data IO finishes, this gets called so we can finish
2952 * an ordered extent if the range of bytes in the file it covers are
2953 * fully written.
2954 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002955static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002956{
Josef Bacik5fd02042012-05-02 14:00:54 -04002957 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002958 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002959 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002960 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002961 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002962 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002963 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002964 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002965 int ret = 0;
2966 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002967 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002968 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002969 bool range_locked = false;
2970 bool clear_new_delalloc_bytes = false;
2971
2972 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2973 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2974 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2975 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002976
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002977 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002978
Josef Bacik5fd02042012-05-02 14:00:54 -04002979 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2980 ret = -EIO;
2981 goto out;
2982 }
2983
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002984 btrfs_free_io_failure_record(BTRFS_I(inode),
2985 ordered_extent->file_offset,
2986 ordered_extent->file_offset +
2987 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002988
Josef Bacik77cef2e2013-08-29 13:57:21 -04002989 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2990 truncated = true;
2991 logical_len = ordered_extent->truncated_len;
2992 /* Truncated the entire extent, don't bother adding */
2993 if (!logical_len)
2994 goto out;
2995 }
2996
Yan, Zhengc2167752009-11-12 09:34:21 +00002997 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002998 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002999
3000 /*
3001 * For mwrite(mmap + memset to write) case, we still reserve
3002 * space for NOCOW range.
3003 * As NOCOW won't cause a new delayed ref, just free the space
3004 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08003005 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08003006 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05003007 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3008 if (nolock)
3009 trans = btrfs_join_transaction_nolock(root);
3010 else
3011 trans = btrfs_join_transaction(root);
3012 if (IS_ERR(trans)) {
3013 ret = PTR_ERR(trans);
3014 trans = NULL;
3015 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003016 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003017 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003018 ret = btrfs_update_inode_fallback(trans, root, inode);
3019 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003020 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003021 goto out;
3022 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003023
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003024 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003025 lock_extent_bits(io_tree, ordered_extent->file_offset,
3026 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003027 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003028
Liu Bo38c227d2013-01-29 03:18:40 +00003029 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3030 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003031 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003032 if (ret) {
3033 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003034 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003035 /* the inode is shared */
3036 new = record_old_file_extents(inode, ordered_extent);
3037
3038 clear_extent_bit(io_tree, ordered_extent->file_offset,
3039 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003040 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003041 }
3042
Josef Bacik0cb59c92010-07-02 12:14:14 -04003043 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003044 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003045 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003046 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003047 if (IS_ERR(trans)) {
3048 ret = PTR_ERR(trans);
3049 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003050 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003051 }
Chris Masona79b7d42014-05-22 16:18:52 -07003052
Josef Bacik69fe2d72017-10-19 14:15:57 -04003053 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003054
Chris Masonc8b97812008-10-29 14:49:59 -04003055 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003056 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003057 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003058 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003059 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3060 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003061 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003062 ordered_extent->file_offset,
3063 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003064 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003065 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003066 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003067 ret = insert_reserved_file_extent(trans, inode,
3068 ordered_extent->file_offset,
3069 ordered_extent->start,
3070 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003071 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003072 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003073 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003074 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003075 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003076 ordered_extent->start,
3077 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003078 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003079 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3080 ordered_extent->file_offset, ordered_extent->len,
3081 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003082 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003083 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003084 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003085 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003086
Nikolay Borisovac01f262018-01-08 10:59:43 +02003087 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3088 if (ret) {
3089 btrfs_abort_transaction(trans, ret);
3090 goto out;
3091 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003092
Josef Bacik6c760c02012-11-09 10:53:21 -05003093 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3094 ret = btrfs_update_inode_fallback(trans, root, inode);
3095 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003096 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003097 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003098 }
3099 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003100out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003101 if (range_locked || clear_new_delalloc_bytes) {
3102 unsigned int clear_bits = 0;
3103
3104 if (range_locked)
3105 clear_bits |= EXTENT_LOCKED;
3106 if (clear_new_delalloc_bytes)
3107 clear_bits |= EXTENT_DELALLOC_NEW;
3108 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3109 ordered_extent->file_offset,
3110 ordered_extent->file_offset +
3111 ordered_extent->len - 1,
3112 clear_bits,
3113 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003114 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003115 }
3116
Miao Xiea698d0752012-09-20 01:51:59 -06003117 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003118 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003119
Josef Bacik77cef2e2013-08-29 13:57:21 -04003120 if (ret || truncated) {
3121 u64 start, end;
3122
3123 if (truncated)
3124 start = ordered_extent->file_offset + logical_len;
3125 else
3126 start = ordered_extent->file_offset;
3127 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003128 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003129
3130 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003131 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003132
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003133 /*
3134 * If the ordered extent had an IOERR or something else went
3135 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003136 * back to the allocator. We only free the extent in the
3137 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003138 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003139 if ((ret || !logical_len) &&
3140 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003141 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003142 btrfs_free_reserved_extent(fs_info,
3143 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003144 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003145 }
3146
3147
Josef Bacik5fd02042012-05-02 14:00:54 -04003148 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003149 * This needs to be done to make sure anybody waiting knows we are done
3150 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003151 */
3152 btrfs_remove_ordered_extent(inode, ordered_extent);
3153
Liu Bo38c227d2013-01-29 03:18:40 +00003154 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003155 if (new) {
3156 if (ret) {
3157 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003158 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003159 } else {
3160 relink_file_extents(new);
3161 }
3162 }
Liu Bo38c227d2013-01-29 03:18:40 +00003163
Chris Masone6dcd2d2008-07-17 12:53:50 -04003164 /* once for us */
3165 btrfs_put_ordered_extent(ordered_extent);
3166 /* once for the tree */
3167 btrfs_put_ordered_extent(ordered_extent);
3168
Josef Bacik5fd02042012-05-02 14:00:54 -04003169 return ret;
3170}
3171
3172static void finish_ordered_fn(struct btrfs_work *work)
3173{
3174 struct btrfs_ordered_extent *ordered_extent;
3175 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3176 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003177}
3178
David Sterbac3988d62017-02-17 15:18:32 +01003179static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003180 struct extent_state *state, int uptodate)
3181{
Josef Bacik5fd02042012-05-02 14:00:54 -04003182 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003183 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003184 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003185 struct btrfs_workqueue *wq;
3186 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003187
liubo1abe9b82011-03-24 11:18:59 +00003188 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3189
Chris Mason8b62b722009-09-02 16:53:46 -04003190 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003191 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3192 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003193 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003194
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003195 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003196 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003197 func = btrfs_freespace_write_helper;
3198 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003199 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003200 func = btrfs_endio_write_helper;
3201 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003202
Liu Bo9e0af232014-08-15 23:36:53 +08003203 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3204 NULL);
3205 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003206}
3207
Miao Xiedc380ae2014-09-12 18:43:55 +08003208static int __readpage_endio_check(struct inode *inode,
3209 struct btrfs_io_bio *io_bio,
3210 int icsum, struct page *page,
3211 int pgoff, u64 start, size_t len)
3212{
3213 char *kaddr;
3214 u32 csum_expected;
3215 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003216
3217 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3218
3219 kaddr = kmap_atomic(page);
3220 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003221 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003222 if (csum != csum_expected)
3223 goto zeroit;
3224
3225 kunmap_atomic(kaddr);
3226 return 0;
3227zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003228 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003229 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003230 memset(kaddr + pgoff, 1, len);
3231 flush_dcache_page(page);
3232 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003233 return -EIO;
3234}
3235
Chris Masond352ac62008-09-29 15:18:18 -04003236/*
Chris Masond352ac62008-09-29 15:18:18 -04003237 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003238 * if there's a match, we allow the bio to finish. If not, the code in
3239 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003240 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003241static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3242 u64 phy_offset, struct page *page,
3243 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003244{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003245 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003246 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003247 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003248 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003249
Chris Masond20f7042008-12-08 16:58:54 -05003250 if (PageChecked(page)) {
3251 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003252 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003253 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003254
3255 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003256 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003257
Yan Zheng17d217f2008-12-12 10:03:38 -05003258 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003259 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003260 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003261 return 0;
3262 }
3263
Miao Xiefacc8a222013-07-25 19:22:34 +08003264 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003265 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3266 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003267}
Chris Masonb888db22007-08-27 16:49:44 -04003268
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003269/*
3270 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3271 *
3272 * @inode: The inode we want to perform iput on
3273 *
3274 * This function uses the generic vfs_inode::i_count to track whether we should
3275 * just decrement it (in case it's > 1) or if this is the last iput then link
3276 * the inode to the delayed iput machinery. Delayed iputs are processed at
3277 * transaction commit time/superblock commit/cleaner kthread.
3278 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003279void btrfs_add_delayed_iput(struct inode *inode)
3280{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003281 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003282 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003283
3284 if (atomic_add_unless(&inode->i_count, -1, 1))
3285 return;
3286
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003287 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003288 ASSERT(list_empty(&binode->delayed_iput));
3289 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003290 spin_unlock(&fs_info->delayed_iput_lock);
3291}
3292
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003293void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003294{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003295
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003296 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003297 while (!list_empty(&fs_info->delayed_iputs)) {
3298 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003299
David Sterba8089fe62015-11-19 14:15:51 +01003300 inode = list_first_entry(&fs_info->delayed_iputs,
3301 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003302 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003303 spin_unlock(&fs_info->delayed_iput_lock);
3304 iput(&inode->vfs_inode);
3305 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003306 }
David Sterba8089fe62015-11-19 14:15:51 +01003307 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003308}
3309
Yan, Zhengd68fc572010-05-16 10:49:58 -04003310/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003311 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003312 * files in the subvolume, it removes orphan item and frees block_rsv
3313 * structure.
3314 */
3315void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3316 struct btrfs_root *root)
3317{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003318 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003319 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003320 int ret;
3321
Josef Bacik8a35d952012-05-23 14:26:42 -04003322 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003323 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3324 return;
3325
Josef Bacik90290e12011-12-02 15:44:12 -05003326 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003327 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003328 spin_unlock(&root->orphan_lock);
3329 return;
3330 }
3331
3332 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3333 spin_unlock(&root->orphan_lock);
3334 return;
3335 }
3336
3337 block_rsv = root->orphan_block_rsv;
3338 root->orphan_block_rsv = NULL;
3339 spin_unlock(&root->orphan_lock);
3340
Miao Xie27cdeb72014-04-02 19:51:05 +08003341 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003342 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003343 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003344 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003345 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003346 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003347 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003348 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3349 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003350 }
3351
Josef Bacik90290e12011-12-02 15:44:12 -05003352 if (block_rsv) {
3353 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003354 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003355 }
3356}
3357
3358/*
Josef Bacik7b128762008-07-24 12:17:14 -04003359 * This creates an orphan entry for the given inode in case something goes
3360 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003361 *
3362 * NOTE: caller of this function should reserve 5 units of metadata for
3363 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003364 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003365int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3366 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003367{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003368 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3369 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003370 struct btrfs_block_rsv *block_rsv = NULL;
3371 int reserve = 0;
3372 int insert = 0;
3373 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003374
Yan, Zhengd68fc572010-05-16 10:49:58 -04003375 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003376 block_rsv = btrfs_alloc_block_rsv(fs_info,
3377 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003378 if (!block_rsv)
3379 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003380 }
3381
Josef Bacik8a35d952012-05-23 14:26:42 -04003382 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003383 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003384#if 0
3385 /*
3386 * For proper ENOSPC handling, we should do orphan
3387 * cleanup when mounting. But this introduces backward
3388 * compatibility issue.
3389 */
3390 if (!xchg(&root->orphan_item_inserted, 1))
3391 insert = 2;
3392 else
3393 insert = 1;
3394#endif
3395 insert = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003396 }
Josef Bacik7b128762008-07-24 12:17:14 -04003397
Josef Bacik72ac3c02012-05-23 14:13:11 -04003398 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003399 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003400 reserve = 1;
Liu Bo3d5adda2018-01-25 11:02:55 -07003401
3402 spin_lock(&root->orphan_lock);
3403 /* If someone has created ->orphan_block_rsv, be happy to use it. */
3404 if (!root->orphan_block_rsv) {
3405 root->orphan_block_rsv = block_rsv;
3406 } else if (block_rsv) {
3407 btrfs_free_block_rsv(fs_info, block_rsv);
3408 block_rsv = NULL;
3409 }
3410
3411 if (insert)
3412 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003413 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003414
Yan, Zhengd68fc572010-05-16 10:49:58 -04003415 /* grab metadata reservation from transaction handle */
3416 if (reserve) {
3417 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003418 ASSERT(!ret);
3419 if (ret) {
Liu Bo1a932ef2018-01-25 11:02:54 -07003420 /*
3421 * dec doesn't need spin_lock as ->orphan_block_rsv
3422 * would be released only if ->orphan_inodes is
3423 * zero.
3424 */
Josef Bacik3b6571c2016-05-27 13:03:04 -04003425 atomic_dec(&root->orphan_inodes);
3426 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003427 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003428 if (insert)
3429 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003430 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003431 return ret;
3432 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003433 }
3434
3435 /* insert an orphan item to track this unlinked/truncated file */
3436 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003437 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003438 if (ret) {
Josef Bacik4ef31a42013-08-13 14:10:08 -04003439 if (reserve) {
3440 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003441 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003442 btrfs_orphan_release_metadata(inode);
3443 }
Liu Bo1a932ef2018-01-25 11:02:54 -07003444 /*
3445 * btrfs_orphan_commit_root may race with us and set
3446 * ->orphan_block_rsv to zero, in order to avoid that,
3447 * decrease ->orphan_inodes after everything is done.
3448 */
3449 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003450 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003451 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003452 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003453 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003454 return ret;
3455 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003456 }
3457 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003458 }
3459
3460 /* insert an orphan item to track subvolume contains orphan files */
3461 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003462 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003463 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003464 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003465 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003466 return ret;
3467 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003468 }
3469 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003470}
3471
3472/*
3473 * We have done the truncate/delete so we can go ahead and remove the orphan
3474 * item for this particular inode.
3475 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003476static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003477 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003478{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003479 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003480 int delete_item = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003481 int ret = 0;
3482
Josef Bacik8a35d952012-05-23 14:26:42 -04003483 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003484 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003485 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003486
Liu Bo1a932ef2018-01-25 11:02:54 -07003487 if (delete_item && trans)
3488 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
3489
Josef Bacik72ac3c02012-05-23 14:13:11 -04003490 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003491 &inode->runtime_flags))
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003492 btrfs_orphan_release_metadata(inode);
3493
Liu Bo1a932ef2018-01-25 11:02:54 -07003494 /*
3495 * btrfs_orphan_commit_root may race with us and set ->orphan_block_rsv
3496 * to zero, in order to avoid that, decrease ->orphan_inodes after
3497 * everything is done.
3498 */
3499 if (delete_item)
3500 atomic_dec(&root->orphan_inodes);
3501
Josef Bacik4ef31a42013-08-13 14:10:08 -04003502 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003503}
3504
3505/*
3506 * this cleans up any orphans that may be left on the list from the last use
3507 * of this root.
3508 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003509int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003510{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003511 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003512 struct btrfs_path *path;
3513 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003514 struct btrfs_key key, found_key;
3515 struct btrfs_trans_handle *trans;
3516 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003517 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003518 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3519
Yan, Zhengd68fc572010-05-16 10:49:58 -04003520 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003521 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003522
3523 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003524 if (!path) {
3525 ret = -ENOMEM;
3526 goto out;
3527 }
David Sterbae4058b52015-11-27 16:31:35 +01003528 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003529
3530 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003531 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003532 key.offset = (u64)-1;
3533
Josef Bacik7b128762008-07-24 12:17:14 -04003534 while (1) {
3535 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003536 if (ret < 0)
3537 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003538
3539 /*
3540 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003541 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003542 * find the key and see if we have stuff that matches
3543 */
3544 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003545 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003546 if (path->slots[0] == 0)
3547 break;
3548 path->slots[0]--;
3549 }
3550
3551 /* pull out the item */
3552 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003553 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3554
3555 /* make sure the item matches what we want */
3556 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3557 break;
David Sterba962a2982014-06-04 18:41:45 +02003558 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003559 break;
3560
3561 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003562 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003563
3564 /*
3565 * this is where we are basically btrfs_lookup, without the
3566 * crossing root thing. we store the inode number in the
3567 * offset of the orphan item.
3568 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003569
3570 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003571 btrfs_err(fs_info,
3572 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003573 ret = -EINVAL;
3574 goto out;
3575 }
3576
3577 last_objectid = found_key.offset;
3578
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003579 found_key.objectid = found_key.offset;
3580 found_key.type = BTRFS_INODE_ITEM_KEY;
3581 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003582 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303583 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003584 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003585 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003586
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003587 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003588 struct btrfs_root *dead_root;
3589 struct btrfs_fs_info *fs_info = root->fs_info;
3590 int is_dead_root = 0;
3591
3592 /*
3593 * this is an orphan in the tree root. Currently these
3594 * could come from 2 sources:
3595 * a) a snapshot deletion in progress
3596 * b) a free space cache inode
3597 * We need to distinguish those two, as the snapshot
3598 * orphan must not get deleted.
3599 * find_dead_roots already ran before us, so if this
3600 * is a snapshot deletion, we should find the root
3601 * in the dead_roots list
3602 */
3603 spin_lock(&fs_info->trans_lock);
3604 list_for_each_entry(dead_root, &fs_info->dead_roots,
3605 root_list) {
3606 if (dead_root->root_key.objectid ==
3607 found_key.objectid) {
3608 is_dead_root = 1;
3609 break;
3610 }
3611 }
3612 spin_unlock(&fs_info->trans_lock);
3613 if (is_dead_root) {
3614 /* prevent this orphan from being found again */
3615 key.offset = found_key.objectid - 1;
3616 continue;
3617 }
3618 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003619 /*
3620 * Inode is already gone but the orphan item is still there,
3621 * kill the orphan item.
3622 */
Filipe Manana67710892016-06-06 11:51:25 +01003623 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003624 trans = btrfs_start_transaction(root, 1);
3625 if (IS_ERR(trans)) {
3626 ret = PTR_ERR(trans);
3627 goto out;
3628 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003629 btrfs_debug(fs_info, "auto deleting %Lu",
3630 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003631 ret = btrfs_del_orphan_item(trans, root,
3632 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003633 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003634 if (ret)
3635 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003636 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003637 }
Josef Bacik7b128762008-07-24 12:17:14 -04003638
Josef Bacik7b128762008-07-24 12:17:14 -04003639 /*
3640 * add this inode to the orphan list so btrfs_orphan_del does
3641 * the proper thing when we hit it
3642 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003643 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3644 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003645 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003646
Josef Bacik7b128762008-07-24 12:17:14 -04003647 /* if we have links, this was a truncate, lets do that */
3648 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303649 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003650 iput(inode);
3651 continue;
3652 }
Josef Bacik7b128762008-07-24 12:17:14 -04003653 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003654
3655 /* 1 for the orphan item deletion. */
3656 trans = btrfs_start_transaction(root, 1);
3657 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003658 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003659 ret = PTR_ERR(trans);
3660 goto out;
3661 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003662 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003663 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003664 if (ret) {
3665 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003666 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003667 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003668
Filipe Manana213e8c52018-02-06 20:40:31 +00003669 ret = btrfs_truncate(inode, false);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003670 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003671 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003672 } else {
3673 nr_unlink++;
3674 }
3675
3676 /* this will do delete_inode and everything for us */
3677 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003678 if (ret)
3679 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003680 }
Miao Xie3254c872011-11-10 20:45:05 -05003681 /* release the path since we're done with it */
3682 btrfs_release_path(path);
3683
Yan, Zhengd68fc572010-05-16 10:49:58 -04003684 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3685
3686 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003687 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003688 (u64)-1);
3689
Miao Xie27cdeb72014-04-02 19:51:05 +08003690 if (root->orphan_block_rsv ||
3691 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003692 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003693 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003694 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003695 }
Josef Bacik7b128762008-07-24 12:17:14 -04003696
3697 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003698 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003699 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003700 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003701
3702out:
3703 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003704 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003705 btrfs_free_path(path);
3706 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003707}
3708
Chris Masond352ac62008-09-29 15:18:18 -04003709/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003710 * very simple check to peek ahead in the leaf looking for xattrs. If we
3711 * don't find any xattrs, we know there can't be any acls.
3712 *
3713 * slot is the slot the inode is in, objectid is the objectid of the inode
3714 */
3715static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003716 int slot, u64 objectid,
3717 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003718{
3719 u32 nritems = btrfs_header_nritems(leaf);
3720 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003721 static u64 xattr_access = 0;
3722 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003723 int scanned = 0;
3724
Josef Bacikf23b5a52013-06-19 10:16:26 -04003725 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003726 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3727 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3728 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3729 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003730 }
3731
Chris Mason46a53cc2009-04-27 11:47:50 -04003732 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003733 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003734 while (slot < nritems) {
3735 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3736
3737 /* we found a different objectid, there must not be acls */
3738 if (found_key.objectid != objectid)
3739 return 0;
3740
3741 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003742 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003743 if (*first_xattr_slot == -1)
3744 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003745 if (found_key.offset == xattr_access ||
3746 found_key.offset == xattr_default)
3747 return 1;
3748 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003749
3750 /*
3751 * we found a key greater than an xattr key, there can't
3752 * be any acls later on
3753 */
3754 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3755 return 0;
3756
3757 slot++;
3758 scanned++;
3759
3760 /*
3761 * it goes inode, inode backrefs, xattrs, extents,
3762 * so if there are a ton of hard links to an inode there can
3763 * be a lot of backrefs. Don't waste time searching too hard,
3764 * this is just an optimization
3765 */
3766 if (scanned >= 8)
3767 break;
3768 }
3769 /* we hit the end of the leaf before we found an xattr or
3770 * something larger than an xattr. We have to assume the inode
3771 * has acls
3772 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003773 if (*first_xattr_slot == -1)
3774 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003775 return 1;
3776}
3777
3778/*
Chris Masond352ac62008-09-29 15:18:18 -04003779 * read an inode from the btree into the in-memory inode
3780 */
Filipe Manana67710892016-06-06 11:51:25 +01003781static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003782{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003783 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003784 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003785 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003786 struct btrfs_inode_item *inode_item;
3787 struct btrfs_root *root = BTRFS_I(inode)->root;
3788 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003789 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003790 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003791 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003792 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003793 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003794 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003795
3796 ret = btrfs_fill_inode(inode, &rdev);
3797 if (!ret)
3798 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003799
3800 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003801 if (!path) {
3802 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003803 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003804 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003805
Chris Mason39279cc2007-06-12 06:35:45 -04003806 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003807
Chris Mason39279cc2007-06-12 06:35:45 -04003808 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003809 if (ret) {
3810 if (ret > 0)
3811 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003812 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003813 }
Chris Mason39279cc2007-06-12 06:35:45 -04003814
Chris Mason5f39d392007-10-15 16:14:19 -04003815 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003816
3817 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003818 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003819
Chris Mason5f39d392007-10-15 16:14:19 -04003820 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3821 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003822 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003823 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003824 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3825 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003826 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003827
David Sterbaa937b972014-12-12 17:39:12 +01003828 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3829 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003830
David Sterbaa937b972014-12-12 17:39:12 +01003831 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3832 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003833
David Sterbaa937b972014-12-12 17:39:12 +01003834 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3835 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003836
chandan r9cc97d62012-07-04 12:48:07 +05303837 BTRFS_I(inode)->i_otime.tv_sec =
3838 btrfs_timespec_sec(leaf, &inode_item->otime);
3839 BTRFS_I(inode)->i_otime.tv_nsec =
3840 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003841
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003842 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003843 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003844 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3845
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003846 inode_set_iversion_queried(inode,
3847 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003848 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003849 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003850 rdev = btrfs_inode_rdev(leaf, inode_item);
3851
Josef Bacikaec74772008-07-24 12:12:38 -04003852 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003853 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003854
3855cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003856 /*
3857 * If we were modified in the current generation and evicted from memory
3858 * and then re-read we need to do a full sync since we don't have any
3859 * idea about which extents were modified before we were evicted from
3860 * cache.
3861 *
3862 * This is required for both inode re-read from disk and delayed inode
3863 * in delayed_nodes_tree.
3864 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003865 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003866 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3867 &BTRFS_I(inode)->runtime_flags);
3868
Filipe Mananabde6c242015-07-24 00:00:19 +01003869 /*
3870 * We don't persist the id of the transaction where an unlink operation
3871 * against the inode was last made. So here we assume the inode might
3872 * have been evicted, and therefore the exact value of last_unlink_trans
3873 * lost, and set it to last_trans to avoid metadata inconsistencies
3874 * between the inode and its parent if the inode is fsync'ed and the log
3875 * replayed. For example, in the scenario:
3876 *
3877 * touch mydir/foo
3878 * ln mydir/foo mydir/bar
3879 * sync
3880 * unlink mydir/bar
3881 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3882 * xfs_io -c fsync mydir/foo
3883 * <power failure>
3884 * mount fs, triggers fsync log replay
3885 *
3886 * We must make sure that when we fsync our inode foo we also log its
3887 * parent inode, otherwise after log replay the parent still has the
3888 * dentry with the "bar" name but our inode foo has a link count of 1
3889 * and doesn't have an inode ref with the name "bar" anymore.
3890 *
3891 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003892 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003893 * transaction commits on fsync if our inode is a directory, or if our
3894 * inode is not a directory, logging its parent unnecessarily.
3895 */
3896 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3897
Miao Xie67de1172013-12-26 13:07:06 +08003898 path->slots[0]++;
3899 if (inode->i_nlink != 1 ||
3900 path->slots[0] >= btrfs_header_nritems(leaf))
3901 goto cache_acl;
3902
3903 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003904 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003905 goto cache_acl;
3906
3907 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3908 if (location.type == BTRFS_INODE_REF_KEY) {
3909 struct btrfs_inode_ref *ref;
3910
3911 ref = (struct btrfs_inode_ref *)ptr;
3912 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3913 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3914 struct btrfs_inode_extref *extref;
3915
3916 extref = (struct btrfs_inode_extref *)ptr;
3917 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3918 extref);
3919 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003920cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003921 /*
3922 * try to precache a NULL acl entry for files that don't have
3923 * any xattrs or acls
3924 */
Li Zefan33345d012011-04-20 10:31:50 +08003925 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003926 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003927 if (first_xattr_slot != -1) {
3928 path->slots[0] = first_xattr_slot;
3929 ret = btrfs_load_inode_props(inode, path);
3930 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003931 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003932 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003933 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003934 root->root_key.objectid, ret);
3935 }
3936 btrfs_free_path(path);
3937
Al Viro72c04902009-06-24 16:58:48 -04003938 if (!maybe_acls)
3939 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003940
Chris Mason39279cc2007-06-12 06:35:45 -04003941 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003942 case S_IFREG:
3943 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003944 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003945 inode->i_fop = &btrfs_file_operations;
3946 inode->i_op = &btrfs_file_inode_operations;
3947 break;
3948 case S_IFDIR:
3949 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003950 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003951 break;
3952 case S_IFLNK:
3953 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003954 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003955 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3956 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003957 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003958 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003959 init_special_inode(inode, inode->i_mode, rdev);
3960 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003961 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003962
3963 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003964 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003965
3966make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003967 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003968 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003969 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003970}
3971
Chris Masond352ac62008-09-29 15:18:18 -04003972/*
3973 * given a leaf and an inode, copy the inode fields into the leaf
3974 */
Chris Masone02119d2008-09-05 16:13:11 -04003975static void fill_inode_item(struct btrfs_trans_handle *trans,
3976 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003977 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003978 struct inode *inode)
3979{
Liu Bo51fab692012-12-27 09:01:21 +00003980 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003981
Liu Bo51fab692012-12-27 09:01:21 +00003982 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003983
Liu Bo51fab692012-12-27 09:01:21 +00003984 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3985 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3986 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3987 &token);
3988 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3989 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003990
David Sterbaa937b972014-12-12 17:39:12 +01003991 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003992 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003993 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003994 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003995
David Sterbaa937b972014-12-12 17:39:12 +01003996 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003997 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003998 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003999 inode->i_mtime.tv_nsec, &token);
4000
David Sterbaa937b972014-12-12 17:39:12 +01004001 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00004002 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01004003 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00004004 inode->i_ctime.tv_nsec, &token);
4005
chandan r9cc97d62012-07-04 12:48:07 +05304006 btrfs_set_token_timespec_sec(leaf, &item->otime,
4007 BTRFS_I(inode)->i_otime.tv_sec, &token);
4008 btrfs_set_token_timespec_nsec(leaf, &item->otime,
4009 BTRFS_I(inode)->i_otime.tv_nsec, &token);
4010
Liu Bo51fab692012-12-27 09:01:21 +00004011 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
4012 &token);
4013 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
4014 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05004015 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
4016 &token);
Liu Bo51fab692012-12-27 09:01:21 +00004017 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
4018 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
4019 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
4020 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04004021}
4022
Chris Masond352ac62008-09-29 15:18:18 -04004023/*
4024 * copy everything in the in-memory inode into the btree.
4025 */
Chris Mason21151332011-11-10 20:39:08 -05004026static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05004027 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004028{
4029 struct btrfs_inode_item *inode_item;
4030 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004031 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004032 int ret;
4033
4034 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004035 if (!path)
4036 return -ENOMEM;
4037
Chris Masonb9473432009-03-13 11:00:37 -04004038 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08004039 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
4040 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004041 if (ret) {
4042 if (ret > 0)
4043 ret = -ENOENT;
4044 goto failed;
4045 }
4046
Chris Mason5f39d392007-10-15 16:14:19 -04004047 leaf = path->nodes[0];
4048 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004049 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004050
Chris Masone02119d2008-09-05 16:13:11 -04004051 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004052 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004053 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004054 ret = 0;
4055failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004056 btrfs_free_path(path);
4057 return ret;
4058}
4059
Chris Masond352ac62008-09-29 15:18:18 -04004060/*
Chris Mason21151332011-11-10 20:39:08 -05004061 * copy everything in the in-memory inode into the btree.
4062 */
4063noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4064 struct btrfs_root *root, struct inode *inode)
4065{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004066 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004067 int ret;
4068
4069 /*
4070 * If the inode is a free space inode, we can deadlock during commit
4071 * if we put it into the delayed code.
4072 *
4073 * The data relocation inode should also be directly updated
4074 * without delay
4075 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004076 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004077 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004078 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004079 btrfs_update_root_times(trans, root);
4080
Chris Mason21151332011-11-10 20:39:08 -05004081 ret = btrfs_delayed_update_inode(trans, root, inode);
4082 if (!ret)
4083 btrfs_set_inode_last_trans(trans, inode);
4084 return ret;
4085 }
4086
4087 return btrfs_update_inode_item(trans, root, inode);
4088}
4089
Josef Bacikbe6aef62012-10-22 15:43:12 -04004090noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4091 struct btrfs_root *root,
4092 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004093{
4094 int ret;
4095
4096 ret = btrfs_update_inode(trans, root, inode);
4097 if (ret == -ENOSPC)
4098 return btrfs_update_inode_item(trans, root, inode);
4099 return ret;
4100}
4101
4102/*
Chris Masond352ac62008-09-29 15:18:18 -04004103 * unlink helper that gets used here in inode.c and in the tree logging
4104 * recovery code. It remove a link in a directory with a given name, and
4105 * also drops the back refs in the inode to the directory
4106 */
Al Viro92986792011-03-04 17:14:37 +00004107static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4108 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004109 struct btrfs_inode *dir,
4110 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004111 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004112{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004113 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004114 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004115 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004116 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004117 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004118 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004119 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004120 u64 ino = btrfs_ino(inode);
4121 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004122
4123 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004124 if (!path) {
4125 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004126 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004127 }
4128
Chris Masonb9473432009-03-13 11:00:37 -04004129 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004130 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004131 name, name_len, -1);
4132 if (IS_ERR(di)) {
4133 ret = PTR_ERR(di);
4134 goto err;
4135 }
4136 if (!di) {
4137 ret = -ENOENT;
4138 goto err;
4139 }
Chris Mason5f39d392007-10-15 16:14:19 -04004140 leaf = path->nodes[0];
4141 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004142 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004143 if (ret)
4144 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004145 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004146
Miao Xie67de1172013-12-26 13:07:06 +08004147 /*
4148 * If we don't have dir index, we have to get it by looking up
4149 * the inode ref, since we get the inode ref, remove it directly,
4150 * it is unnecessary to do delayed deletion.
4151 *
4152 * But if we have dir index, needn't search inode ref to get it.
4153 * Since the inode ref is close to the inode item, it is better
4154 * that we delay to delete it, and just do this deletion when
4155 * we update the inode item.
4156 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004157 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004158 ret = btrfs_delayed_delete_inode_ref(inode);
4159 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004160 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004161 goto skip_backref;
4162 }
4163 }
4164
Li Zefan33345d012011-04-20 10:31:50 +08004165 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4166 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004167 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004168 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004169 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004170 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004171 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004172 goto err;
4173 }
Miao Xie67de1172013-12-26 13:07:06 +08004174skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004175 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004176 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004177 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004178 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004179 }
Chris Mason39279cc2007-06-12 06:35:45 -04004180
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004181 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4182 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004183 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004184 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004185 goto err;
4186 }
Chris Masone02119d2008-09-05 16:13:11 -04004187
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004188 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4189 index);
Chris Mason6418c962010-10-30 07:34:24 -04004190 if (ret == -ENOENT)
4191 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004192 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004193 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004194err:
4195 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004196 if (ret)
4197 goto out;
4198
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004199 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004200 inode_inc_iversion(&inode->vfs_inode);
4201 inode_inc_iversion(&dir->vfs_inode);
4202 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4203 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4204 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004205out:
Chris Mason39279cc2007-06-12 06:35:45 -04004206 return ret;
4207}
4208
Al Viro92986792011-03-04 17:14:37 +00004209int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4210 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004211 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004212 const char *name, int name_len)
4213{
4214 int ret;
4215 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4216 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004217 drop_nlink(&inode->vfs_inode);
4218 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004219 }
4220 return ret;
4221}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004222
4223/*
4224 * helper to start transaction for unlink and rmdir.
4225 *
Josef Bacikd52be812013-05-29 14:54:47 -04004226 * unlink and rmdir are special in btrfs, they do not always free space, so
4227 * if we cannot make our reservations the normal way try and see if there is
4228 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4229 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004230 */
Josef Bacikd52be812013-05-29 14:54:47 -04004231static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004232{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004233 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004234
Josef Bacike70bea52011-10-11 14:18:24 -04004235 /*
4236 * 1 for the possible orphan item
4237 * 1 for the dir item
4238 * 1 for the dir index
4239 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004240 * 1 for the inode
4241 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004242 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004243}
4244
Chris Mason39279cc2007-06-12 06:35:45 -04004245static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4246{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004247 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004248 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004249 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004250 int ret;
4251
Josef Bacikd52be812013-05-29 14:54:47 -04004252 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004253 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004254 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004255
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004256 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4257 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004258
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004259 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4260 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4261 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004262 if (ret)
4263 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004264
Yan, Zhenga22285a2010-05-16 10:48:46 -04004265 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004266 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004267 if (ret)
4268 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004269 }
Josef Bacik7b128762008-07-24 12:17:14 -04004270
Tsutomu Itohb5324022011-07-19 07:27:20 +00004271out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004272 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004273 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004274 return ret;
4275}
4276
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004277int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4278 struct btrfs_root *root,
4279 struct inode *dir, u64 objectid,
4280 const char *name, int name_len)
4281{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004282 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004283 struct btrfs_path *path;
4284 struct extent_buffer *leaf;
4285 struct btrfs_dir_item *di;
4286 struct btrfs_key key;
4287 u64 index;
4288 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004289 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004290
4291 path = btrfs_alloc_path();
4292 if (!path)
4293 return -ENOMEM;
4294
Li Zefan33345d012011-04-20 10:31:50 +08004295 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004296 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004297 if (IS_ERR_OR_NULL(di)) {
4298 if (!di)
4299 ret = -ENOENT;
4300 else
4301 ret = PTR_ERR(di);
4302 goto out;
4303 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004304
4305 leaf = path->nodes[0];
4306 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4307 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4308 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004309 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004310 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004311 goto out;
4312 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004313 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004314
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004315 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4316 root->root_key.objectid, dir_ino,
4317 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004318 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004319 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004320 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004321 goto out;
4322 }
Li Zefan33345d012011-04-20 10:31:50 +08004323 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004324 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004325 if (IS_ERR_OR_NULL(di)) {
4326 if (!di)
4327 ret = -ENOENT;
4328 else
4329 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004330 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004331 goto out;
4332 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004333
4334 leaf = path->nodes[0];
4335 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004336 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004337 index = key.offset;
4338 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004339 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004340
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004341 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004342 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004343 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004344 goto out;
4345 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004346
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004347 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004348 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004349 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004350 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004351 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004352 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004353out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004354 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004355 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004356}
4357
Chris Mason39279cc2007-06-12 06:35:45 -04004358static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4359{
David Howells2b0143b2015-03-17 22:25:59 +00004360 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004361 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004362 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004363 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004364 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004365
David Sterbab3ae2442012-09-13 16:04:34 -06004366 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004367 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004368 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004369 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004370
Josef Bacikd52be812013-05-29 14:54:47 -04004371 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004372 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004373 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004374
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004375 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004376 err = btrfs_unlink_subvol(trans, root, dir,
4377 BTRFS_I(inode)->location.objectid,
4378 dentry->d_name.name,
4379 dentry->d_name.len);
4380 goto out;
4381 }
4382
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004383 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004384 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004385 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004386
Filipe Manana44f714d2016-06-06 16:11:13 +01004387 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4388
Chris Mason39279cc2007-06-12 06:35:45 -04004389 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004390 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4391 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4392 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004393 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004394 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004395 /*
4396 * Propagate the last_unlink_trans value of the deleted dir to
4397 * its parent directory. This is to prevent an unrecoverable
4398 * log tree in the case we do something like this:
4399 * 1) create dir foo
4400 * 2) create snapshot under dir foo
4401 * 3) delete the snapshot
4402 * 4) rmdir foo
4403 * 5) mkdir foo
4404 * 6) fsync foo or some file inside foo
4405 */
4406 if (last_unlink_trans >= trans->transid)
4407 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4408 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004409out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004410 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004411 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004412
Chris Mason39279cc2007-06-12 06:35:45 -04004413 return err;
4414}
4415
Chris Mason28f75a02015-02-04 06:59:29 -08004416static int truncate_space_check(struct btrfs_trans_handle *trans,
4417 struct btrfs_root *root,
4418 u64 bytes_deleted)
4419{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004420 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004421 int ret;
4422
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004423 /*
4424 * This is only used to apply pressure to the enospc system, we don't
4425 * intend to use this reservation at all.
4426 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004427 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004428 bytes_deleted *= fs_info->nodesize;
4429 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004430 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004431 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004432 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004433 trans->transid,
4434 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004435 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004436 }
Chris Mason28f75a02015-02-04 06:59:29 -08004437 return ret;
4438
4439}
4440
Josef Bacikddfae632017-10-19 14:16:02 -04004441/*
4442 * Return this if we need to call truncate_block for the last bit of the
4443 * truncate.
4444 */
4445#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004446
Chris Mason323ac952008-10-01 19:05:46 -04004447/*
Chris Mason39279cc2007-06-12 06:35:45 -04004448 * this can truncate away extent items, csum items and directory items.
4449 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004450 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004451 *
4452 * csum items that cross the new i_size are truncated to the new size
4453 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004454 *
4455 * min_type is the minimum key type to truncate down to. If set to 0, this
4456 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004457 */
Yan, Zheng80825102009-11-12 09:35:36 +00004458int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4459 struct btrfs_root *root,
4460 struct inode *inode,
4461 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004462{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004463 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004464 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004465 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004466 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004467 struct btrfs_key key;
4468 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004469 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004470 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004471 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004472 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004473 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004474 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004475 int found_extent;
4476 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004477 int pending_del_nr = 0;
4478 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004479 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004480 int ret;
4481 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004482 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004483 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004484 bool be_nice = false;
4485 bool should_throttle = false;
4486 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004487
4488 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004489
Chris Mason28ed1342014-12-17 09:41:04 -08004490 /*
4491 * for non-free space inodes and ref cows, we want to back off from
4492 * time to time
4493 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004494 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004495 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004496 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004497
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004498 path = btrfs_alloc_path();
4499 if (!path)
4500 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004501 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004502
Josef Bacik5dc562c2012-08-17 13:14:17 -04004503 /*
4504 * We want to drop from the next block forward in case this new size is
4505 * not block aligned since we will be keeping the last block of the
4506 * extent just the way it is.
4507 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004508 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004509 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004510 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004511 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004512 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004513
Miao Xie16cdcec2011-04-22 18:12:22 +08004514 /*
4515 * This function is also used to drop the items in the log tree before
4516 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4517 * it is used to drop the loged items. So we shouldn't kill the delayed
4518 * items.
4519 */
4520 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004521 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004522
Li Zefan33345d012011-04-20 10:31:50 +08004523 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004524 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004525 key.type = (u8)-1;
4526
Chris Mason85e21ba2008-01-29 15:11:36 -05004527search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004528 /*
4529 * with a 16K leaf size and 128MB extents, you can actually queue
4530 * up a huge file in a single leaf. Most of the time that
4531 * bytes_deleted is > 0, it will be huge by the time we get here
4532 */
Byongho Leeee221842015-12-15 01:42:10 +09004533 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004534 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004535 err = -EAGAIN;
4536 goto error;
4537 }
4538 }
4539
4540
Chris Masonb9473432009-03-13 11:00:37 -04004541 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004542 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004543 if (ret < 0) {
4544 err = ret;
4545 goto out;
4546 }
Chris Masond3977122009-01-05 21:25:51 -05004547
Chris Mason85e21ba2008-01-29 15:11:36 -05004548 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004549 /* there are no items in the tree for us to truncate, we're
4550 * done
4551 */
Yan, Zheng80825102009-11-12 09:35:36 +00004552 if (path->slots[0] == 0)
4553 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004554 path->slots[0]--;
4555 }
4556
Chris Masond3977122009-01-05 21:25:51 -05004557 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004558 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004559 leaf = path->nodes[0];
4560 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004561 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004562
Li Zefan33345d012011-04-20 10:31:50 +08004563 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004564 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004565
Chris Mason85e21ba2008-01-29 15:11:36 -05004566 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004567 break;
4568
Chris Mason5f39d392007-10-15 16:14:19 -04004569 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004570 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004571 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004572 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004573 extent_type = btrfs_file_extent_type(leaf, fi);
4574 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004575 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004576 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004577
4578 trace_btrfs_truncate_show_fi_regular(
4579 BTRFS_I(inode), leaf, fi,
4580 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004581 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004582 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004583 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004584
4585 trace_btrfs_truncate_show_fi_inline(
4586 BTRFS_I(inode), leaf, fi, path->slots[0],
4587 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004588 }
Yan008630c2007-11-07 13:31:09 -05004589 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004590 }
Yan, Zheng80825102009-11-12 09:35:36 +00004591 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004592 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004593 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004594 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004595 break;
4596 if (found_key.offset >= new_size)
4597 del_item = 1;
4598 else
4599 del_item = 0;
4600 }
Chris Mason39279cc2007-06-12 06:35:45 -04004601 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004602 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004603 if (found_type != BTRFS_EXTENT_DATA_KEY)
4604 goto delete;
4605
4606 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004607 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004608 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004609 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004610 u64 orig_num_bytes =
4611 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004612 extent_num_bytes = ALIGN(new_size -
4613 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004614 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004615 btrfs_set_file_extent_num_bytes(leaf, fi,
4616 extent_num_bytes);
4617 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004618 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004619 if (test_bit(BTRFS_ROOT_REF_COWS,
4620 &root->state) &&
4621 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004622 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004623 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004624 } else {
Chris Masondb945352007-10-15 16:15:53 -04004625 extent_num_bytes =
4626 btrfs_file_extent_disk_num_bytes(leaf,
4627 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004628 extent_offset = found_key.offset -
4629 btrfs_file_extent_offset(leaf, fi);
4630
Chris Mason39279cc2007-06-12 06:35:45 -04004631 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004632 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004633 if (extent_start != 0) {
4634 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004635 if (test_bit(BTRFS_ROOT_REF_COWS,
4636 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004637 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004638 }
4639 }
Chris Mason90692182008-02-08 13:49:28 -05004640 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004641 /*
4642 * we can't truncate inline items that have had
4643 * special encodings
4644 */
4645 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004646 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004647 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4648 btrfs_file_extent_compression(leaf, fi) == 0) {
4649 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004650
Josef Bacikddfae632017-10-19 14:16:02 -04004651 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4652 size = btrfs_file_extent_calc_inline_size(size);
4653 btrfs_truncate_item(root->fs_info, path, size, 1);
4654 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004655 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004656 * We have to bail so the last_size is set to
4657 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004658 */
Josef Bacikddfae632017-10-19 14:16:02 -04004659 err = NEED_TRUNCATE_BLOCK;
4660 break;
Chris Mason90692182008-02-08 13:49:28 -05004661 }
Josef Bacikddfae632017-10-19 14:16:02 -04004662
4663 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4664 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004665 }
Chris Mason179e29e2007-11-01 11:28:41 -04004666delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004667 if (del_item)
4668 last_size = found_key.offset;
4669 else
4670 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004671 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004672 if (!pending_del_nr) {
4673 /* no pending yet, add ourselves */
4674 pending_del_slot = path->slots[0];
4675 pending_del_nr = 1;
4676 } else if (pending_del_nr &&
4677 path->slots[0] + 1 == pending_del_slot) {
4678 /* hop on the pending chunk */
4679 pending_del_nr++;
4680 pending_del_slot = path->slots[0];
4681 } else {
Chris Masond3977122009-01-05 21:25:51 -05004682 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004683 }
Chris Mason39279cc2007-06-12 06:35:45 -04004684 } else {
4685 break;
4686 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004687 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004688
Miao Xie27cdeb72014-04-02 19:51:05 +08004689 if (found_extent &&
4690 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004691 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004692 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004693 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004694 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004695 extent_num_bytes, 0,
4696 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004697 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004698 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004699 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4700 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004701 trans->delayed_ref_updates * 2,
4702 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004703 if (be_nice) {
4704 if (truncate_space_check(trans, root,
4705 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004706 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004707 }
4708 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004709 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004710 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004711 }
Chris Mason39279cc2007-06-12 06:35:45 -04004712 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004713
Yan, Zheng80825102009-11-12 09:35:36 +00004714 if (found_type == BTRFS_INODE_ITEM_KEY)
4715 break;
4716
4717 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004718 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004719 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004720 if (pending_del_nr) {
4721 ret = btrfs_del_items(trans, root, path,
4722 pending_del_slot,
4723 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004724 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004725 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004726 goto error;
4727 }
Yan, Zheng80825102009-11-12 09:35:36 +00004728 pending_del_nr = 0;
4729 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004730 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004731 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004732 unsigned long updates = trans->delayed_ref_updates;
4733 if (updates) {
4734 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004735 ret = btrfs_run_delayed_refs(trans,
4736 fs_info,
4737 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004738 if (ret && !err)
4739 err = ret;
4740 }
4741 }
Chris Mason28f75a02015-02-04 06:59:29 -08004742 /*
4743 * if we failed to refill our space rsv, bail out
4744 * and let the transaction restart
4745 */
4746 if (should_end) {
4747 err = -EAGAIN;
4748 goto error;
4749 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004750 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004751 } else {
4752 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004753 }
Chris Mason39279cc2007-06-12 06:35:45 -04004754 }
Yan, Zheng80825102009-11-12 09:35:36 +00004755out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004756 if (pending_del_nr) {
4757 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4758 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004759 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004760 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004761 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004762error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004763 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4764 ASSERT(last_size >= new_size);
4765 if (!err && last_size > new_size)
4766 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004767 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004768 }
Chris Mason28ed1342014-12-17 09:41:04 -08004769
Chris Mason39279cc2007-06-12 06:35:45 -04004770 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004771
Byongho Leeee221842015-12-15 01:42:10 +09004772 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004773 unsigned long updates = trans->delayed_ref_updates;
4774 if (updates) {
4775 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004776 ret = btrfs_run_delayed_refs(trans, fs_info,
4777 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004778 if (ret && !err)
4779 err = ret;
4780 }
4781 }
Yan, Zheng80825102009-11-12 09:35:36 +00004782 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004783}
4784
Chris Masona52d9a82007-08-27 16:49:44 -04004785/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304786 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004787 * @inode - inode that we're zeroing
4788 * @from - the offset to start zeroing
4789 * @len - the length to zero, 0 to zero the entire range respective to the
4790 * offset
4791 * @front - zero up to the offset instead of from the offset on
4792 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304793 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004794 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004795 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304796int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004797 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004798{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004799 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004800 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004801 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4802 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004803 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004804 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004805 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004806 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004807 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304808 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004809 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004810 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004811 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304812 u64 block_start;
4813 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004814
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004815 if (IS_ALIGNED(offset, blocksize) &&
4816 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004817 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304818
Josef Bacik8b62f872017-10-19 14:15:55 -04004819 block_start = round_down(from, blocksize);
4820 block_end = block_start + blocksize - 1;
4821
Qu Wenruo364ecf32017-02-27 15:10:38 +08004822 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004823 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004824 if (ret)
4825 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004826
Chris Mason211c17f2008-05-15 09:13:45 -04004827again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004828 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004829 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004830 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004831 block_start, blocksize);
4832 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004833 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004834 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004835 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004836
Chris Masona52d9a82007-08-27 16:49:44 -04004837 if (!PageUptodate(page)) {
4838 ret = btrfs_readpage(NULL, page);
4839 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004840 if (page->mapping != mapping) {
4841 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004842 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004843 goto again;
4844 }
Chris Masona52d9a82007-08-27 16:49:44 -04004845 if (!PageUptodate(page)) {
4846 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004847 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004848 }
4849 }
Chris Mason211c17f2008-05-15 09:13:45 -04004850 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004851
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304852 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004853 set_page_extent_mapped(page);
4854
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304855 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004856 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304857 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004858 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004859 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004860 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004861 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004862 btrfs_put_ordered_extent(ordered);
4863 goto again;
4864 }
4865
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304866 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004867 EXTENT_DIRTY | EXTENT_DELALLOC |
4868 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004869 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004870
Filipe Mananae3b8a482017-11-04 00:16:59 +00004871 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004872 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004873 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304874 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004875 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004876 goto out_unlock;
4877 }
4878
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304879 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004880 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304881 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004882 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004883 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304884 memset(kaddr + (block_start - page_offset(page)),
4885 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004886 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304887 memset(kaddr + (block_start - page_offset(page)) + offset,
4888 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004889 flush_dcache_page(page);
4890 kunmap(page);
4891 }
Chris Mason247e7432008-07-17 12:53:51 -04004892 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004893 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004894 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004895
Chris Mason89642222008-07-24 09:41:53 -04004896out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004897 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004898 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304899 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004900 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004901 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004902 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004903out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004904 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004905 return ret;
4906}
4907
Josef Bacik16e75492013-10-22 12:18:51 -04004908static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4909 u64 offset, u64 len)
4910{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004911 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004912 struct btrfs_trans_handle *trans;
4913 int ret;
4914
4915 /*
4916 * Still need to make sure the inode looks like it's been updated so
4917 * that any holes get logged if we fsync.
4918 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004919 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4920 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004921 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4922 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4923 return 0;
4924 }
4925
4926 /*
4927 * 1 - for the one we're dropping
4928 * 1 - for the one we're adding
4929 * 1 - for updating the inode.
4930 */
4931 trans = btrfs_start_transaction(root, 3);
4932 if (IS_ERR(trans))
4933 return PTR_ERR(trans);
4934
4935 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4936 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004937 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004938 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004939 return ret;
4940 }
4941
David Sterbaf85b7372017-01-20 14:54:07 +01004942 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4943 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004944 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004945 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004946 else
4947 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004948 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004949 return ret;
4950}
4951
Josef Bacik695a0d02011-03-04 15:46:53 -05004952/*
4953 * This function puts in dummy file extents for the area we're creating a hole
4954 * for. So if we are truncating this file to a larger size we need to insert
4955 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4956 * the range between oldsize and size
4957 */
Josef Bacika41ad392011-01-31 15:30:16 -05004958int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004959{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004960 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004961 struct btrfs_root *root = BTRFS_I(inode)->root;
4962 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004963 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004964 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004965 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004966 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4967 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004968 u64 last_byte;
4969 u64 cur_offset;
4970 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004971 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004972
Josef Bacika71754f2013-06-17 17:14:39 -04004973 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304974 * If our size started in the middle of a block we need to zero out the
4975 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004976 * expose stale data.
4977 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304978 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004979 if (err)
4980 return err;
4981
Yan Zheng9036c102008-10-30 14:19:41 -04004982 if (size <= hole_start)
4983 return 0;
4984
Yan Zheng9036c102008-10-30 14:19:41 -04004985 while (1) {
4986 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004987
David Sterbaff13db42015-12-03 14:30:40 +01004988 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004989 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004990 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004991 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004992 if (!ordered)
4993 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004994 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004995 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004996 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004997 btrfs_put_ordered_extent(ordered);
4998 }
4999
Yan Zheng9036c102008-10-30 14:19:41 -04005000 cur_offset = hole_start;
5001 while (1) {
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02005002 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005003 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005004 if (IS_ERR(em)) {
5005 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005006 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005007 break;
5008 }
Yan Zheng9036c102008-10-30 14:19:41 -04005009 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005010 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005011 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005012 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005013 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005014
Josef Bacik16e75492013-10-22 12:18:51 -04005015 err = maybe_insert_hole(root, inode, cur_offset,
5016 hole_size);
5017 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005018 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005019 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005020 cur_offset + hole_size - 1, 0);
5021 hole_em = alloc_extent_map();
5022 if (!hole_em) {
5023 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5024 &BTRFS_I(inode)->runtime_flags);
5025 goto next;
5026 }
5027 hole_em->start = cur_offset;
5028 hole_em->len = hole_size;
5029 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005030
Josef Bacik5dc562c2012-08-17 13:14:17 -04005031 hole_em->block_start = EXTENT_MAP_HOLE;
5032 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005033 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005034 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005035 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005036 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005037 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005038
5039 while (1) {
5040 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005041 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005042 write_unlock(&em_tree->lock);
5043 if (err != -EEXIST)
5044 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005045 btrfs_drop_extent_cache(BTRFS_I(inode),
5046 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005047 cur_offset +
5048 hole_size - 1, 0);
5049 }
5050 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005051 }
Josef Bacik16e75492013-10-22 12:18:51 -04005052next:
Yan Zheng9036c102008-10-30 14:19:41 -04005053 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005054 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005055 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005056 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005057 break;
5058 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005059 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005060 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005061 return err;
5062}
5063
Eric Sandeen3972f262013-01-12 02:57:22 +00005064static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005065{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005066 struct btrfs_root *root = BTRFS_I(inode)->root;
5067 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005068 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005069 loff_t newsize = attr->ia_size;
5070 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005071 int ret;
5072
Eric Sandeen3972f262013-01-12 02:57:22 +00005073 /*
5074 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5075 * special case where we need to update the times despite not having
5076 * these flags set. For all other operations the VFS set these flags
5077 * explicitly if it wants a timestamp update.
5078 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005079 if (newsize != oldsize) {
5080 inode_inc_iversion(inode);
5081 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5082 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005083 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005084 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005085
Josef Bacika41ad392011-01-31 15:30:16 -05005086 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005087 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005088 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005089 * This is to ensure the snapshot captures a fully consistent
5090 * state of this file - if the snapshot captures this expanding
5091 * truncation, it must capture all writes that happened before
5092 * this truncation.
5093 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005094 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005095 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005096 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005097 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005098 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005099 }
Yan, Zheng80825102009-11-12 09:35:36 +00005100
Miao Xief4a2f4c2011-12-14 20:12:01 -05005101 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005102 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005103 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005104 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005105 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005106
5107 i_size_write(inode, newsize);
5108 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305109 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005110 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005111 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005112 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005113 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005114
Josef Bacika41ad392011-01-31 15:30:16 -05005115 /*
5116 * We're truncating a file that used to have good data down to
5117 * zero. Make sure it gets into the ordered flush list so that
5118 * any new writes get down to disk quickly.
5119 */
5120 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005121 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5122 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005123
Josef Bacikf3fe8202013-01-07 17:03:21 -05005124 /*
5125 * 1 for the orphan item we're going to add
5126 * 1 for the orphan item deletion.
5127 */
5128 trans = btrfs_start_transaction(root, 2);
5129 if (IS_ERR(trans))
5130 return PTR_ERR(trans);
5131
5132 /*
5133 * We need to do this in case we fail at _any_ point during the
5134 * actual truncate. Once we do the truncate_setsize we could
5135 * invalidate pages which forces any outstanding ordered io to
5136 * be instantly completed which will give us extents that need
5137 * to be truncated. If we fail to get an orphan inode down we
5138 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005139 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005140 * will be consistent.
5141 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005142 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005143 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005144 if (ret)
5145 return ret;
5146
Josef Bacika41ad392011-01-31 15:30:16 -05005147 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5148 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005149
5150 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005151 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005152 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005153 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005154
Filipe Manana213e8c52018-02-06 20:40:31 +00005155 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005156 if (ret && inode->i_nlink) {
5157 int err;
5158
Liu Bo19fd2df2016-12-01 13:01:02 -08005159 /* To get a stable disk_i_size */
5160 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5161 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005162 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005163 return err;
5164 }
5165
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005166 /*
5167 * failed to truncate, disk_i_size is only adjusted down
5168 * as we remove extents, so it should represent the true
5169 * size of the inode, so reset the in memory size and
5170 * delete our orphan entry.
5171 */
5172 trans = btrfs_join_transaction(root);
5173 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005174 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005175 return ret;
5176 }
5177 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005178 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005179 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005180 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005181 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005182 }
Yan, Zheng80825102009-11-12 09:35:36 +00005183 }
5184
Josef Bacika41ad392011-01-31 15:30:16 -05005185 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005186}
5187
Chris Mason39279cc2007-06-12 06:35:45 -04005188static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5189{
David Howells2b0143b2015-03-17 22:25:59 +00005190 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005191 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005192 int err;
5193
Li Zefanb83cc962010-12-20 16:04:08 +08005194 if (btrfs_root_readonly(root))
5195 return -EROFS;
5196
Jan Kara31051c82016-05-26 16:55:18 +02005197 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005198 if (err)
5199 return err;
5200
Chris Mason5a3f23d2009-03-31 13:27:11 -04005201 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005202 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005203 if (err)
5204 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005205 }
Yan Zheng9036c102008-10-30 14:19:41 -04005206
Christoph Hellwig10257742010-06-04 11:30:02 +02005207 if (attr->ia_valid) {
5208 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005209 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005210 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005211
Josef Bacik22c44fe2011-11-30 10:45:38 -05005212 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005213 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005214 }
5215
Chris Mason39279cc2007-06-12 06:35:45 -04005216 return err;
5217}
Chris Mason61295eb2008-01-14 16:24:38 -05005218
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005219/*
5220 * While truncating the inode pages during eviction, we get the VFS calling
5221 * btrfs_invalidatepage() against each page of the inode. This is slow because
5222 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5223 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5224 * extent_state structures over and over, wasting lots of time.
5225 *
5226 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5227 * those expensive operations on a per page basis and do only the ordered io
5228 * finishing, while we release here the extent_map and extent_state structures,
5229 * without the excessive merging and splitting.
5230 */
5231static void evict_inode_truncate_pages(struct inode *inode)
5232{
5233 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5234 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5235 struct rb_node *node;
5236
5237 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005238 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005239
5240 write_lock(&map_tree->lock);
5241 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5242 struct extent_map *em;
5243
5244 node = rb_first(&map_tree->map);
5245 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005246 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5247 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005248 remove_extent_mapping(map_tree, em);
5249 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005250 if (need_resched()) {
5251 write_unlock(&map_tree->lock);
5252 cond_resched();
5253 write_lock(&map_tree->lock);
5254 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005255 }
5256 write_unlock(&map_tree->lock);
5257
Filipe Manana6ca07092015-05-26 00:55:42 +01005258 /*
5259 * Keep looping until we have no more ranges in the io tree.
5260 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005261 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5262 * still in progress (unlocked the pages in the bio but did not yet
5263 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005264 * ranges can still be locked and eviction started because before
5265 * submitting those bios, which are executed by a separate task (work
5266 * queue kthread), inode references (inode->i_count) were not taken
5267 * (which would be dropped in the end io callback of each bio).
5268 * Therefore here we effectively end up waiting for those bios and
5269 * anyone else holding locked ranges without having bumped the inode's
5270 * reference count - if we don't do it, when they access the inode's
5271 * io_tree to unlock a range it may be too late, leading to an
5272 * use-after-free issue.
5273 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005274 spin_lock(&io_tree->lock);
5275 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5276 struct extent_state *state;
5277 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005278 u64 start;
5279 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005280
5281 node = rb_first(&io_tree->state);
5282 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005283 start = state->start;
5284 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005285 spin_unlock(&io_tree->lock);
5286
David Sterbaff13db42015-12-03 14:30:40 +01005287 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005288
5289 /*
5290 * If still has DELALLOC flag, the extent didn't reach disk,
5291 * and its reserved space won't be freed by delayed_ref.
5292 * So we need to free its reserved space here.
5293 * (Refer to comment in btrfs_invalidatepage, case 2)
5294 *
5295 * Note, end is the bytenr of last byte, so we need + 1 here.
5296 */
5297 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005298 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005299
Filipe Manana6ca07092015-05-26 00:55:42 +01005300 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005301 EXTENT_LOCKED | EXTENT_DIRTY |
5302 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005303 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005304
Filipe Manana7064dd52014-08-08 02:47:05 +01005305 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005306 spin_lock(&io_tree->lock);
5307 }
5308 spin_unlock(&io_tree->lock);
5309}
5310
Al Virobd555972010-06-07 11:35:40 -04005311void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005312{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005313 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005314 struct btrfs_trans_handle *trans;
5315 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005316 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005317 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005318 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005319 int ret;
5320
liubo1abe9b82011-03-24 11:18:59 +00005321 trace_btrfs_inode_evict(inode);
5322
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005323 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005324 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005325 return;
5326 }
5327
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005328 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005329
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005330 evict_inode_truncate_pages(inode);
5331
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005332 if (inode->i_nlink &&
5333 ((btrfs_root_refs(&root->root_item) != 0 &&
5334 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005335 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005336 goto no_delete;
5337
Chris Mason39279cc2007-06-12 06:35:45 -04005338 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005339 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005340 goto no_delete;
5341 }
Al Virobd555972010-06-07 11:35:40 -04005342 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005343 if (!special_file(inode->i_mode))
5344 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005345
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005346 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005347
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005348 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005349 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005350 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005351 goto no_delete;
5352 }
5353
Yan, Zheng76dda932009-09-21 16:00:26 -04005354 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005355 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5356 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005357 goto no_delete;
5358 }
5359
Nikolay Borisovaa790212017-01-10 20:35:40 +02005360 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005361 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005362 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005363 goto no_delete;
5364 }
5365
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005366 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005367 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005368 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005369 goto no_delete;
5370 }
Josef Bacik4a338542011-08-29 11:01:31 -04005371 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005372 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005373 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005374
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005375 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005376
Josef Bacik4289a662011-08-05 13:22:24 -04005377 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005378 * This is a bit simpler than btrfs_truncate since we've already
5379 * reserved our space for our orphan item in the unlink, so we just
5380 * need to reserve some slack space in case we add bytes and update
5381 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005382 */
Yan, Zheng80825102009-11-12 09:35:36 +00005383 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005384 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5385 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005386
Josef Bacik726c35f2011-09-26 15:46:06 -04005387 /*
5388 * Try and steal from the global reserve since we will
5389 * likely not use this space anyway, we want to try as
5390 * hard as possible to get this to work.
5391 */
5392 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005393 steal_from_global++;
5394 else
5395 steal_from_global = 0;
5396 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005397
Josef Bacik3bce8762015-02-24 12:35:51 -08005398 /*
5399 * steal_from_global == 0: we reserved stuff, hooray!
5400 * steal_from_global == 1: we didn't reserve stuff, boo!
5401 * steal_from_global == 2: we've committed, still not a lot of
5402 * room but maybe we'll have room in the global reserve this
5403 * time.
5404 * steal_from_global == 3: abandon all hope!
5405 */
5406 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005407 btrfs_warn(fs_info,
5408 "Could not get space for a delete, will truncate on mount %d",
5409 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005410 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005411 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005412 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005413 }
5414
Miao Xie0e8c36a2012-12-19 06:59:51 +00005415 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005416 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005417 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005418 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005419 goto no_delete;
5420 }
5421
Josef Bacik3bce8762015-02-24 12:35:51 -08005422 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005423 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005424 * sure there is room to do it, if not we need to commit and try
5425 * again.
5426 */
5427 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005428 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005429 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005430 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005431 else
5432 ret = -ENOSPC;
5433 }
5434
5435 /*
5436 * Couldn't steal from the global reserve, we have too much
5437 * pending stuff built up, commit the transaction and try it
5438 * again.
5439 */
5440 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005441 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005442 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005443 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005444 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005445 goto no_delete;
5446 }
5447 continue;
5448 } else {
5449 steal_from_global = 0;
5450 }
5451
Josef Bacik4289a662011-08-05 13:22:24 -04005452 trans->block_rsv = rsv;
5453
Yan, Zhengd68fc572010-05-16 10:49:58 -04005454 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005455 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005456 break;
5457
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005458 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005459 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005460 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005461 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005462 }
5463
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005464 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005465
Josef Bacik4ef31a42013-08-13 14:10:08 -04005466 /*
5467 * Errors here aren't a big deal, it just means we leave orphan items
5468 * in the tree. They will be cleaned up on the next mount.
5469 */
Yan, Zheng80825102009-11-12 09:35:36 +00005470 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005471 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005472 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005473 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005474 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005475 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005476
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005477 trans->block_rsv = &fs_info->trans_block_rsv;
5478 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005479 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005480 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005481
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005482 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005483 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005484no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005485 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005486 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005487}
5488
5489/*
5490 * this returns the key found in the dir entry in the location pointer.
Su Yue005d6712018-03-05 17:13:37 +08005491 * If no dir entries were found, returns -ENOENT.
5492 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005493 */
5494static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5495 struct btrfs_key *location)
5496{
5497 const char *name = dentry->d_name.name;
5498 int namelen = dentry->d_name.len;
5499 struct btrfs_dir_item *di;
5500 struct btrfs_path *path;
5501 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005502 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005503
5504 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005505 if (!path)
5506 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005507
David Sterbaf85b7372017-01-20 14:54:07 +01005508 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5509 name, namelen, 0);
Su Yue005d6712018-03-05 17:13:37 +08005510 if (!di) {
5511 ret = -ENOENT;
5512 goto out;
5513 }
5514 if (IS_ERR(di)) {
Yan0d9f7f32007-10-25 15:48:28 -04005515 ret = PTR_ERR(di);
Su Yue005d6712018-03-05 17:13:37 +08005516 goto out;
5517 }
Chris Masond3977122009-01-05 21:25:51 -05005518
Chris Mason5f39d392007-10-15 16:14:19 -04005519 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005520 if (location->type != BTRFS_INODE_ITEM_KEY &&
5521 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005522 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005523 btrfs_warn(root->fs_info,
5524"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5525 __func__, name, btrfs_ino(BTRFS_I(dir)),
5526 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005527 }
Chris Mason39279cc2007-06-12 06:35:45 -04005528out:
Chris Mason39279cc2007-06-12 06:35:45 -04005529 btrfs_free_path(path);
5530 return ret;
5531}
5532
5533/*
5534 * when we hit a tree root in a directory, the btrfs part of the inode
5535 * needs to be changed to reflect the root directory of the tree root. This
5536 * is kind of like crossing a mount point.
5537 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005538static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005539 struct inode *dir,
5540 struct dentry *dentry,
5541 struct btrfs_key *location,
5542 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005543{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005544 struct btrfs_path *path;
5545 struct btrfs_root *new_root;
5546 struct btrfs_root_ref *ref;
5547 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005548 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005549 int ret;
5550 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005551
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005552 path = btrfs_alloc_path();
5553 if (!path) {
5554 err = -ENOMEM;
5555 goto out;
5556 }
Chris Mason39279cc2007-06-12 06:35:45 -04005557
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005558 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005559 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5560 key.type = BTRFS_ROOT_REF_KEY;
5561 key.offset = location->objectid;
5562
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005563 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005564 if (ret) {
5565 if (ret < 0)
5566 err = ret;
5567 goto out;
5568 }
Chris Mason39279cc2007-06-12 06:35:45 -04005569
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005570 leaf = path->nodes[0];
5571 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005572 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005573 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5574 goto out;
5575
5576 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5577 (unsigned long)(ref + 1),
5578 dentry->d_name.len);
5579 if (ret)
5580 goto out;
5581
David Sterbab3b4aa72011-04-21 01:20:15 +02005582 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005583
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005584 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005585 if (IS_ERR(new_root)) {
5586 err = PTR_ERR(new_root);
5587 goto out;
5588 }
5589
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005590 *sub_root = new_root;
5591 location->objectid = btrfs_root_dirid(&new_root->root_item);
5592 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005593 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005594 err = 0;
5595out:
5596 btrfs_free_path(path);
5597 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005598}
5599
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005600static void inode_tree_add(struct inode *inode)
5601{
5602 struct btrfs_root *root = BTRFS_I(inode)->root;
5603 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005604 struct rb_node **p;
5605 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005606 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005607 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005608
Al Viro1d3382cb2010-10-23 15:19:20 -04005609 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005610 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005611 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005612 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005613 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005614 while (*p) {
5615 parent = *p;
5616 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5617
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005618 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005619 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005620 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005621 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005622 else {
5623 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005624 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005625 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005626 RB_CLEAR_NODE(parent);
5627 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005628 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005629 }
5630 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005631 rb_link_node(new, parent, p);
5632 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005633 spin_unlock(&root->inode_lock);
5634}
5635
5636static void inode_tree_del(struct inode *inode)
5637{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005638 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005639 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005640 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005641
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005642 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005643 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005644 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005645 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005646 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005647 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005648 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005649
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005650 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005651 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005652 spin_lock(&root->inode_lock);
5653 empty = RB_EMPTY_ROOT(&root->inode_tree);
5654 spin_unlock(&root->inode_lock);
5655 if (empty)
5656 btrfs_add_dead_root(root);
5657 }
5658}
5659
Jeff Mahoney143bede2012-03-01 14:56:26 +01005660void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005661{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005662 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005663 struct rb_node *node;
5664 struct rb_node *prev;
5665 struct btrfs_inode *entry;
5666 struct inode *inode;
5667 u64 objectid = 0;
5668
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005669 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005670 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005671
5672 spin_lock(&root->inode_lock);
5673again:
5674 node = root->inode_tree.rb_node;
5675 prev = NULL;
5676 while (node) {
5677 prev = node;
5678 entry = rb_entry(node, struct btrfs_inode, rb_node);
5679
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005680 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005681 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005682 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005683 node = node->rb_right;
5684 else
5685 break;
5686 }
5687 if (!node) {
5688 while (prev) {
5689 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005690 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005691 node = prev;
5692 break;
5693 }
5694 prev = rb_next(prev);
5695 }
5696 }
5697 while (node) {
5698 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005699 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005700 inode = igrab(&entry->vfs_inode);
5701 if (inode) {
5702 spin_unlock(&root->inode_lock);
5703 if (atomic_read(&inode->i_count) > 1)
5704 d_prune_aliases(inode);
5705 /*
Al Viro45321ac2010-06-07 13:43:19 -04005706 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005707 * the inode cache when its usage count
5708 * hits zero.
5709 */
5710 iput(inode);
5711 cond_resched();
5712 spin_lock(&root->inode_lock);
5713 goto again;
5714 }
5715
5716 if (cond_resched_lock(&root->inode_lock))
5717 goto again;
5718
5719 node = rb_next(node);
5720 }
5721 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005722}
5723
Chris Masone02119d2008-09-05 16:13:11 -04005724static int btrfs_init_locked_inode(struct inode *inode, void *p)
5725{
5726 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005727 inode->i_ino = args->location->objectid;
5728 memcpy(&BTRFS_I(inode)->location, args->location,
5729 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005730 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005731 return 0;
5732}
5733
5734static int btrfs_find_actor(struct inode *inode, void *opaque)
5735{
5736 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005737 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005738 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005739}
5740
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005741static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005742 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005743 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005744{
5745 struct inode *inode;
5746 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005747 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005748
Chris Mason90d3e592014-01-09 17:28:00 -08005749 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005750 args.root = root;
5751
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005752 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005753 btrfs_init_locked_inode,
5754 (void *)&args);
5755 return inode;
5756}
5757
Balaji Rao1a54ef82008-07-21 02:01:04 +05305758/* Get an inode object given its location and corresponding root.
5759 * Returns in *is_new if the inode was read from disk
5760 */
5761struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005762 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305763{
5764 struct inode *inode;
5765
Chris Mason90d3e592014-01-09 17:28:00 -08005766 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305767 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005768 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305769
5770 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005771 int ret;
5772
5773 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005774 if (!is_bad_inode(inode)) {
5775 inode_tree_add(inode);
5776 unlock_new_inode(inode);
5777 if (new)
5778 *new = 1;
5779 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005780 unlock_new_inode(inode);
5781 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005782 ASSERT(ret < 0);
5783 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005784 }
5785 }
5786
Balaji Rao1a54ef82008-07-21 02:01:04 +05305787 return inode;
5788}
5789
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005790static struct inode *new_simple_dir(struct super_block *s,
5791 struct btrfs_key *key,
5792 struct btrfs_root *root)
5793{
5794 struct inode *inode = new_inode(s);
5795
5796 if (!inode)
5797 return ERR_PTR(-ENOMEM);
5798
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005799 BTRFS_I(inode)->root = root;
5800 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005801 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005802
5803 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005804 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005805 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005806 inode->i_fop = &simple_dir_operations;
5807 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005808 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305809 inode->i_atime = inode->i_mtime;
5810 inode->i_ctime = inode->i_mtime;
5811 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005812
5813 return inode;
5814}
5815
Chris Mason3de45862008-11-17 21:02:50 -05005816struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005817{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005818 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005819 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005820 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005821 struct btrfs_root *sub_root = root;
5822 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005823 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005824 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005825
5826 if (dentry->d_name.len > BTRFS_NAME_LEN)
5827 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005828
Jeff Layton39e3c952012-11-28 11:30:53 -05005829 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005830 if (ret < 0)
5831 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005832
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005833 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005834 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005835 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005836 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005837
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005838 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005839 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005840 &location, &sub_root);
5841 if (ret < 0) {
5842 if (ret != -ENOENT)
5843 inode = ERR_PTR(ret);
5844 else
5845 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5846 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005847 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005848 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005849 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005850
Julia Lawall34d19ba2011-01-24 19:55:19 +00005851 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005852 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005853 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005854 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005855 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005856 if (ret) {
5857 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005858 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005859 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005860 }
5861
Chris Mason3de45862008-11-17 21:02:50 -05005862 return inode;
5863}
5864
Nick Pigginfe15ce42011-01-07 17:49:23 +11005865static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005866{
5867 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005868 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005869
Li Zefan848cce02012-02-21 17:04:28 +08005870 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005871 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005872
Li Zefan848cce02012-02-21 17:04:28 +08005873 if (inode) {
5874 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005875 if (btrfs_root_refs(&root->root_item) == 0)
5876 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005877
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005878 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005879 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005880 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005881 return 0;
5882}
5883
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005884static void btrfs_dentry_release(struct dentry *dentry)
5885{
Daeseok Youn944a4512014-04-14 15:37:02 +09005886 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005887}
5888
Chris Mason3de45862008-11-17 21:02:50 -05005889static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005890 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005891{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005892 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005893
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005894 inode = btrfs_lookup_dentry(dir, dentry);
5895 if (IS_ERR(inode)) {
5896 if (PTR_ERR(inode) == -ENOENT)
5897 inode = NULL;
5898 else
5899 return ERR_CAST(inode);
5900 }
5901
Al Viro41d28bc2014-10-12 22:24:21 -04005902 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005903}
5904
Miao Xie16cdcec2011-04-22 18:12:22 +08005905unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005906 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5907};
5908
Josef Bacik23b5ec72017-07-24 15:14:25 -04005909/*
5910 * All this infrastructure exists because dir_emit can fault, and we are holding
5911 * the tree lock when doing readdir. For now just allocate a buffer and copy
5912 * our information into that, and then dir_emit from the buffer. This is
5913 * similar to what NFS does, only we don't keep the buffer around in pagecache
5914 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5915 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5916 * tree lock.
5917 */
5918static int btrfs_opendir(struct inode *inode, struct file *file)
5919{
5920 struct btrfs_file_private *private;
5921
5922 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5923 if (!private)
5924 return -ENOMEM;
5925 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5926 if (!private->filldir_buf) {
5927 kfree(private);
5928 return -ENOMEM;
5929 }
5930 file->private_data = private;
5931 return 0;
5932}
5933
5934struct dir_entry {
5935 u64 ino;
5936 u64 offset;
5937 unsigned type;
5938 int name_len;
5939};
5940
5941static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5942{
5943 while (entries--) {
5944 struct dir_entry *entry = addr;
5945 char *name = (char *)(entry + 1);
5946
5947 ctx->pos = entry->offset;
5948 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5949 entry->type))
5950 return 1;
5951 addr += sizeof(struct dir_entry) + entry->name_len;
5952 ctx->pos++;
5953 }
5954 return 0;
5955}
5956
Al Viro9cdda8d2013-05-22 16:48:09 -04005957static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005958{
Al Viro9cdda8d2013-05-22 16:48:09 -04005959 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005960 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005961 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005962 struct btrfs_dir_item *di;
5963 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005964 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005965 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005966 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005967 struct list_head ins_list;
5968 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005969 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005970 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005971 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005972 char *name_ptr;
5973 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005974 int entries = 0;
5975 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005976 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005977 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005978
Al Viro9cdda8d2013-05-22 16:48:09 -04005979 if (!dir_emit_dots(file, ctx))
5980 return 0;
5981
David Woodhouse49593bf2008-08-17 17:08:36 +01005982 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005983 if (!path)
5984 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005985
Josef Bacik23b5ec72017-07-24 15:14:25 -04005986 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005987 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005988
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005989 INIT_LIST_HEAD(&ins_list);
5990 INIT_LIST_HEAD(&del_list);
5991 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005992
Josef Bacik23b5ec72017-07-24 15:14:25 -04005993again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005994 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005995 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005996 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005997
Chris Mason39279cc2007-06-12 06:35:45 -04005998 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5999 if (ret < 0)
6000 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006001
6002 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006003 struct dir_entry *entry;
6004
Chris Mason5f39d392007-10-15 16:14:19 -04006005 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006006 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006007 if (slot >= btrfs_header_nritems(leaf)) {
6008 ret = btrfs_next_leaf(root, path);
6009 if (ret < 0)
6010 goto err;
6011 else if (ret > 0)
6012 break;
6013 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006014 }
Chris Mason3de45862008-11-17 21:02:50 -05006015
Chris Mason5f39d392007-10-15 16:14:19 -04006016 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6017
6018 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006019 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006020 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006021 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006022 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006023 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006024 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006025 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006026 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006027 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006028 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6029 PAGE_SIZE) {
6030 btrfs_release_path(path);
6031 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6032 if (ret)
6033 goto nopos;
6034 addr = private->filldir_buf;
6035 entries = 0;
6036 total_len = 0;
6037 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006038 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006039
6040 entry = addr;
6041 entry->name_len = name_len;
6042 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006043 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6044 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006045 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006046 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006047 entry->ino = location.objectid;
6048 entry->offset = found_key.offset;
6049 entries++;
6050 addr += sizeof(struct dir_entry) + name_len;
6051 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006052next:
6053 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006054 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006055 btrfs_release_path(path);
6056
6057 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6058 if (ret)
6059 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006060
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006061 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006062 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006063 goto nopos;
6064
Zach Browndb62efb2013-07-11 16:19:42 -07006065 /*
6066 * Stop new entries from being returned after we return the last
6067 * entry.
6068 *
6069 * New directory entries are assigned a strictly increasing
6070 * offset. This means that new entries created during readdir
6071 * are *guaranteed* to be seen in the future by that readdir.
6072 * This has broken buggy programs which operate on names as
6073 * they're returned by readdir. Until we re-use freed offsets
6074 * we have this hack to stop new entries from being returned
6075 * under the assumption that they'll never reach this huge
6076 * offset.
6077 *
6078 * This is being careful not to overflow 32bit loff_t unless the
6079 * last entry requires it because doing so has broken 32bit apps
6080 * in the past.
6081 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006082 if (ctx->pos >= INT_MAX)
6083 ctx->pos = LLONG_MAX;
6084 else
6085 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006086nopos:
6087 ret = 0;
6088err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006089 if (put)
6090 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006091 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006092 return ret;
6093}
6094
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006095int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006096{
6097 struct btrfs_root *root = BTRFS_I(inode)->root;
6098 struct btrfs_trans_handle *trans;
6099 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006100 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006101
Josef Bacik72ac3c02012-05-23 14:13:11 -04006102 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006103 return 0;
6104
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006105 if (btrfs_fs_closing(root->fs_info) &&
6106 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006107 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006108
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006109 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006110 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006111 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006112 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006113 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006114 if (IS_ERR(trans))
6115 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006116 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006117 }
6118 return ret;
6119}
6120
6121/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006122 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006123 * inode changes. But, it is most likely to find the inode in cache.
6124 * FIXME, needs more benchmarking...there are no reasons other than performance
6125 * to keep or drop this code.
6126 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006127static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006128{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006129 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006130 struct btrfs_root *root = BTRFS_I(inode)->root;
6131 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006132 int ret;
6133
Josef Bacik72ac3c02012-05-23 14:13:11 -04006134 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006135 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006136
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006137 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006138 if (IS_ERR(trans))
6139 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006140
6141 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006142 if (ret && ret == -ENOSPC) {
6143 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006144 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006145 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006146 if (IS_ERR(trans))
6147 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006148
Chris Mason94b60442010-05-26 11:02:00 -04006149 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006150 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006151 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006152 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006153 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006154
6155 return ret;
6156}
6157
6158/*
6159 * This is a copy of file_update_time. We need this so we can return error on
6160 * ENOSPC for updating the inode in the case of file write and mmap writes.
6161 */
Josef Bacike41f9412012-03-26 09:46:47 -04006162static int btrfs_update_time(struct inode *inode, struct timespec *now,
6163 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006164{
Alexander Block2bc5565282012-06-15 09:49:33 +02006165 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006166 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006167
6168 if (btrfs_root_readonly(root))
6169 return -EROFS;
6170
Josef Bacike41f9412012-03-26 09:46:47 -04006171 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006172 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006173 if (flags & S_CTIME)
6174 inode->i_ctime = *now;
6175 if (flags & S_MTIME)
6176 inode->i_mtime = *now;
6177 if (flags & S_ATIME)
6178 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006179 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006180}
6181
Chris Masond352ac62008-09-29 15:18:18 -04006182/*
6183 * find the highest existing sequence number in a directory
6184 * and then set the in-memory index_cnt variable to reflect
6185 * free sequence numbers
6186 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006187static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006188{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006189 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006190 struct btrfs_key key, found_key;
6191 struct btrfs_path *path;
6192 struct extent_buffer *leaf;
6193 int ret;
6194
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006195 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006196 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006197 key.offset = (u64)-1;
6198
6199 path = btrfs_alloc_path();
6200 if (!path)
6201 return -ENOMEM;
6202
6203 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6204 if (ret < 0)
6205 goto out;
6206 /* FIXME: we should be able to handle this */
6207 if (ret == 0)
6208 goto out;
6209 ret = 0;
6210
6211 /*
6212 * MAGIC NUMBER EXPLANATION:
6213 * since we search a directory based on f_pos we have to start at 2
6214 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6215 * else has to start at 2
6216 */
6217 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006218 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006219 goto out;
6220 }
6221
6222 path->slots[0]--;
6223
6224 leaf = path->nodes[0];
6225 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6226
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006227 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006228 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006229 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006230 goto out;
6231 }
6232
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006233 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006234out:
6235 btrfs_free_path(path);
6236 return ret;
6237}
6238
Chris Masond352ac62008-09-29 15:18:18 -04006239/*
6240 * helper to find a free sequence number in a given directory. This current
6241 * code is very simple, later versions will do smarter things in the btree
6242 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006243int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006244{
6245 int ret = 0;
6246
Nikolay Borisov877574e2017-02-20 13:50:33 +02006247 if (dir->index_cnt == (u64)-1) {
6248 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006249 if (ret) {
6250 ret = btrfs_set_inode_index_count(dir);
6251 if (ret)
6252 return ret;
6253 }
Josef Bacikaec74772008-07-24 12:12:38 -04006254 }
6255
Nikolay Borisov877574e2017-02-20 13:50:33 +02006256 *index = dir->index_cnt;
6257 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006258
6259 return ret;
6260}
6261
Chris Masonb0d5d102014-09-08 13:08:51 -07006262static int btrfs_insert_inode_locked(struct inode *inode)
6263{
6264 struct btrfs_iget_args args;
6265 args.location = &BTRFS_I(inode)->location;
6266 args.root = BTRFS_I(inode)->root;
6267
6268 return insert_inode_locked4(inode,
6269 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6270 btrfs_find_actor, &args);
6271}
6272
Anand Jain19aee8d2017-07-18 17:37:05 +08006273/*
6274 * Inherit flags from the parent inode.
6275 *
6276 * Currently only the compression flags and the cow flags are inherited.
6277 */
6278static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6279{
6280 unsigned int flags;
6281
6282 if (!dir)
6283 return;
6284
6285 flags = BTRFS_I(dir)->flags;
6286
6287 if (flags & BTRFS_INODE_NOCOMPRESS) {
6288 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6289 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6290 } else if (flags & BTRFS_INODE_COMPRESS) {
6291 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6292 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6293 }
6294
6295 if (flags & BTRFS_INODE_NODATACOW) {
6296 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6297 if (S_ISREG(inode->i_mode))
6298 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6299 }
6300
6301 btrfs_update_iflags(inode);
6302}
6303
Chris Mason39279cc2007-06-12 06:35:45 -04006304static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6305 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006306 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006307 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006308 u64 ref_objectid, u64 objectid,
6309 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006310{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006311 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006312 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006313 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006314 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006315 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006316 struct btrfs_inode_ref *ref;
6317 struct btrfs_key key[2];
6318 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006319 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006320 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006321 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006322
Chris Mason5f39d392007-10-15 16:14:19 -04006323 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006324 if (!path)
6325 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006326
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006327 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006328 if (!inode) {
6329 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006330 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006331 }
Chris Mason39279cc2007-06-12 06:35:45 -04006332
Li Zefan581bb052011-04-20 10:06:11 +08006333 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006334 * O_TMPFILE, set link count to 0, so that after this point,
6335 * we fill in an inode item with the correct link count.
6336 */
6337 if (!name)
6338 set_nlink(inode, 0);
6339
6340 /*
Li Zefan581bb052011-04-20 10:06:11 +08006341 * we have to initialize this early, so we can reclaim the inode
6342 * number if we fail afterwards in this function.
6343 */
6344 inode->i_ino = objectid;
6345
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006346 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006347 trace_btrfs_inode_request(dir);
6348
Nikolay Borisov877574e2017-02-20 13:50:33 +02006349 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006350 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006351 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006352 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006353 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006354 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006355 } else if (dir) {
6356 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006357 }
6358 /*
6359 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006360 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006361 * number
6362 */
6363 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006364 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006365 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006366 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006367 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006368
Josef Bacik5dc562c2012-08-17 13:14:17 -04006369 /*
6370 * We could have gotten an inode number from somebody who was fsynced
6371 * and then removed in this same transaction, so let's just set full
6372 * sync since it will be a full sync anyway and this will blow away the
6373 * old info in the log.
6374 */
6375 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6376
Chris Mason9c583092008-01-29 15:15:18 -05006377 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006378 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006379 key[0].offset = 0;
6380
Chris Mason9c583092008-01-29 15:15:18 -05006381 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006382
6383 if (name) {
6384 /*
6385 * Start new inodes with an inode_ref. This is slightly more
6386 * efficient for small numbers of hard links since they will
6387 * be packed into one item. Extended refs will kick in if we
6388 * add more hard links than can fit in the ref item.
6389 */
6390 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006391 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006392 key[1].offset = ref_objectid;
6393
6394 sizes[1] = name_len + sizeof(*ref);
6395 }
Chris Mason9c583092008-01-29 15:15:18 -05006396
Chris Masonb0d5d102014-09-08 13:08:51 -07006397 location = &BTRFS_I(inode)->location;
6398 location->objectid = objectid;
6399 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006400 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006401
6402 ret = btrfs_insert_inode_locked(inode);
6403 if (ret < 0)
6404 goto fail;
6405
Chris Masonb9473432009-03-13 11:00:37 -04006406 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006407 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006408 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006409 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006410
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006411 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006412 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306413
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006414 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306415 inode->i_atime = inode->i_mtime;
6416 inode->i_ctime = inode->i_mtime;
6417 BTRFS_I(inode)->i_otime = inode->i_mtime;
6418
Chris Mason5f39d392007-10-15 16:14:19 -04006419 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6420 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006421 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006422 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006423 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006424
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006425 if (name) {
6426 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6427 struct btrfs_inode_ref);
6428 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6429 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6430 ptr = (unsigned long)(ref + 1);
6431 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6432 }
Chris Mason9c583092008-01-29 15:15:18 -05006433
Chris Mason5f39d392007-10-15 16:14:19 -04006434 btrfs_mark_buffer_dirty(path->nodes[0]);
6435 btrfs_free_path(path);
6436
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006437 btrfs_inherit_iflags(inode, dir);
6438
Al Viro569254b2011-07-24 17:08:40 -04006439 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006440 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006441 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006442 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006443 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6444 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006445 }
6446
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006447 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006448
6449 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006450 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006451
Alexander Block8ea05e32012-07-25 17:35:53 +02006452 btrfs_update_root_times(trans, root);
6453
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006454 ret = btrfs_inode_inherit_props(trans, inode, dir);
6455 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006456 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006457 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006458 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006459
Chris Mason39279cc2007-06-12 06:35:45 -04006460 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006461
6462fail_unlock:
6463 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006464fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006465 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006466 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006467 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006468 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006469 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006470}
6471
6472static inline u8 btrfs_inode_type(struct inode *inode)
6473{
6474 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6475}
6476
Chris Masond352ac62008-09-29 15:18:18 -04006477/*
6478 * utility function to add 'inode' into 'parent_inode' with
6479 * a give name and a given sequence number.
6480 * if 'add_backref' is true, also insert a backref from the
6481 * inode to the parent directory.
6482 */
Chris Masone02119d2008-09-05 16:13:11 -04006483int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006484 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006485 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006486{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006487 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006488 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006489 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006490 struct btrfs_root *root = parent_inode->root;
6491 u64 ino = btrfs_ino(inode);
6492 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006493
Li Zefan33345d012011-04-20 10:31:50 +08006494 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006495 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006496 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006497 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006498 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006499 key.offset = 0;
6500 }
Chris Mason39279cc2007-06-12 06:35:45 -04006501
Li Zefan33345d012011-04-20 10:31:50 +08006502 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006503 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6504 root->root_key.objectid, parent_ino,
6505 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006506 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006507 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6508 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006509 }
6510
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006511 /* Nothing to clean up yet */
6512 if (ret)
6513 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006514
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006515 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6516 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006517 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006518 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006519 goto fail_dir_item;
6520 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006521 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006522 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006523 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006524
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006525 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006526 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006527 inode_inc_iversion(&parent_inode->vfs_inode);
6528 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6529 current_time(&parent_inode->vfs_inode);
6530 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006531 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006532 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006533 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006534
6535fail_dir_item:
6536 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6537 u64 local_index;
6538 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006539 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6540 root->root_key.objectid, parent_ino,
6541 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006542
6543 } else if (add_backref) {
6544 u64 local_index;
6545 int err;
6546
6547 err = btrfs_del_inode_ref(trans, root, name, name_len,
6548 ino, parent_ino, &local_index);
6549 }
6550 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006551}
6552
6553static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006554 struct btrfs_inode *dir, struct dentry *dentry,
6555 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006556{
Josef Bacika1b075d2010-11-19 20:36:11 +00006557 int err = btrfs_add_link(trans, dir, inode,
6558 dentry->d_name.name, dentry->d_name.len,
6559 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006560 if (err > 0)
6561 err = -EEXIST;
6562 return err;
6563}
6564
Josef Bacik618e21d2007-07-11 10:18:17 -04006565static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006566 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006567{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006568 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006569 struct btrfs_trans_handle *trans;
6570 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006571 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006572 int err;
6573 int drop_inode = 0;
6574 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006575 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006576
Josef Bacik9ed74f22009-09-11 16:12:44 -04006577 /*
6578 * 2 for inode item and ref
6579 * 2 for dir items
6580 * 1 for xattr if selinux is on
6581 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006582 trans = btrfs_start_transaction(root, 5);
6583 if (IS_ERR(trans))
6584 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006585
Li Zefan581bb052011-04-20 10:06:11 +08006586 err = btrfs_find_free_ino(root, &objectid);
6587 if (err)
6588 goto out_unlock;
6589
Josef Bacikaec74772008-07-24 12:12:38 -04006590 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006591 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6592 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006593 if (IS_ERR(inode)) {
6594 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006595 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006596 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006597
Casey Schauflerad19db72011-12-15 10:09:07 -05006598 /*
6599 * If the active LSM wants to access the inode during
6600 * d_instantiate it needs these. Smack checks to see
6601 * if the filesystem supports xattrs by looking at the
6602 * ops vector.
6603 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006604 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006605 init_special_inode(inode, inode->i_mode, rdev);
6606
6607 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006608 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006609 goto out_unlock_inode;
6610
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006611 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6612 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006613 if (err) {
6614 goto out_unlock_inode;
6615 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006616 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006617 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006618 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006619 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006620
Josef Bacik618e21d2007-07-11 10:18:17 -04006621out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006622 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006623 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006624 if (drop_inode) {
6625 inode_dec_link_count(inode);
6626 iput(inode);
6627 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006628 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006629
6630out_unlock_inode:
6631 drop_inode = 1;
6632 unlock_new_inode(inode);
6633 goto out_unlock;
6634
Josef Bacik618e21d2007-07-11 10:18:17 -04006635}
6636
Chris Mason39279cc2007-06-12 06:35:45 -04006637static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006638 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006639{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006640 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006641 struct btrfs_trans_handle *trans;
6642 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006643 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006644 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006645 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006646 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006647 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006648
Josef Bacik9ed74f22009-09-11 16:12:44 -04006649 /*
6650 * 2 for inode item and ref
6651 * 2 for dir items
6652 * 1 for xattr if selinux is on
6653 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006654 trans = btrfs_start_transaction(root, 5);
6655 if (IS_ERR(trans))
6656 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006657
Li Zefan581bb052011-04-20 10:06:11 +08006658 err = btrfs_find_free_ino(root, &objectid);
6659 if (err)
6660 goto out_unlock;
6661
Josef Bacikaec74772008-07-24 12:12:38 -04006662 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006663 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6664 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006665 if (IS_ERR(inode)) {
6666 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006667 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006668 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006669 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006670 /*
6671 * If the active LSM wants to access the inode during
6672 * d_instantiate it needs these. Smack checks to see
6673 * if the filesystem supports xattrs by looking at the
6674 * ops vector.
6675 */
6676 inode->i_fop = &btrfs_file_operations;
6677 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006678 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006679
6680 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6681 if (err)
6682 goto out_unlock_inode;
6683
6684 err = btrfs_update_inode(trans, root, inode);
6685 if (err)
6686 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006687
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006688 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6689 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006690 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006691 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006692
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006693 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006694 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006695 d_instantiate(dentry, inode);
6696
Chris Mason39279cc2007-06-12 06:35:45 -04006697out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006698 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006699 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006700 inode_dec_link_count(inode);
6701 iput(inode);
6702 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006703 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006704 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006705
6706out_unlock_inode:
6707 unlock_new_inode(inode);
6708 goto out_unlock;
6709
Chris Mason39279cc2007-06-12 06:35:45 -04006710}
6711
6712static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6713 struct dentry *dentry)
6714{
Filipe Manana271dba42016-01-05 16:24:05 +00006715 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006716 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006717 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006718 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006719 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006720 int err;
6721 int drop_inode = 0;
6722
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006723 /* do not allow sys_link's with other subvols of the same device */
6724 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006725 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006726
Mark Fashehf1863732012-08-08 11:32:27 -07006727 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006728 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006729
Nikolay Borisov877574e2017-02-20 13:50:33 +02006730 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006731 if (err)
6732 goto fail;
6733
Yan, Zhenga22285a2010-05-16 10:48:46 -04006734 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006735 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006736 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006737 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006738 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006739 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006740 if (IS_ERR(trans)) {
6741 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006742 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006743 goto fail;
6744 }
Chris Mason5f39d392007-10-15 16:14:19 -04006745
Miao Xie67de1172013-12-26 13:07:06 +08006746 /* There are several dir indexes for this inode, clear the cache. */
6747 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006748 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006749 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006750 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006751 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006752 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006753
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006754 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6755 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006756
Yan, Zhenga5719522009-09-24 09:17:31 -04006757 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006758 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006759 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006760 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006761 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006762 if (err)
6763 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006764 if (inode->i_nlink == 1) {
6765 /*
6766 * If new hard link count is 1, it's a file created
6767 * with open(2) O_TMPFILE flag.
6768 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006769 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006770 if (err)
6771 goto fail;
6772 }
Al Viro08c422c2011-12-23 07:58:13 -05006773 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006774 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006775 }
Chris Mason39279cc2007-06-12 06:35:45 -04006776
Chris Mason1832a6d2007-12-21 16:27:21 -05006777fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006778 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006779 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006780 if (drop_inode) {
6781 inode_dec_link_count(inode);
6782 iput(inode);
6783 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006784 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006785 return err;
6786}
6787
Al Viro18bb1db2011-07-26 01:41:39 -04006788static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006789{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006790 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006791 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006792 struct btrfs_trans_handle *trans;
6793 struct btrfs_root *root = BTRFS_I(dir)->root;
6794 int err = 0;
6795 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006796 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006797 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006798
Josef Bacik9ed74f22009-09-11 16:12:44 -04006799 /*
6800 * 2 items for inode and ref
6801 * 2 items for dir items
6802 * 1 for xattr if selinux is on
6803 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006804 trans = btrfs_start_transaction(root, 5);
6805 if (IS_ERR(trans))
6806 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006807
Li Zefan581bb052011-04-20 10:06:11 +08006808 err = btrfs_find_free_ino(root, &objectid);
6809 if (err)
6810 goto out_fail;
6811
Josef Bacikaec74772008-07-24 12:12:38 -04006812 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006813 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6814 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006815 if (IS_ERR(inode)) {
6816 err = PTR_ERR(inode);
6817 goto out_fail;
6818 }
Chris Mason5f39d392007-10-15 16:14:19 -04006819
Chris Mason39279cc2007-06-12 06:35:45 -04006820 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006821 /* these must be set before we unlock the inode */
6822 inode->i_op = &btrfs_dir_inode_operations;
6823 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006824
Eric Paris2a7dba32011-02-01 11:05:39 -05006825 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006826 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006827 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006828
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006829 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006830 err = btrfs_update_inode(trans, root, inode);
6831 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006832 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006833
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006834 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6835 dentry->d_name.name,
6836 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006837 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006838 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006839
Chris Mason39279cc2007-06-12 06:35:45 -04006840 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006841 /*
6842 * mkdir is special. We're unlocking after we call d_instantiate
6843 * to avoid a race with nfsd calling d_instantiate.
6844 */
6845 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006846 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006847
6848out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006849 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006850 if (drop_on_err) {
6851 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006852 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006853 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006854 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006855 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006856
6857out_fail_inode:
6858 unlock_new_inode(inode);
6859 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006860}
6861
Chris Masonc8b97812008-10-29 14:49:59 -04006862static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006863 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006864 size_t pg_offset, u64 extent_offset,
6865 struct btrfs_file_extent_item *item)
6866{
6867 int ret;
6868 struct extent_buffer *leaf = path->nodes[0];
6869 char *tmp;
6870 size_t max_size;
6871 unsigned long inline_size;
6872 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006873 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006874
6875 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006876 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006877 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6878 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006879 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006880 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006881 if (!tmp)
6882 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006883 ptr = btrfs_file_extent_inline_start(item);
6884
6885 read_extent_buffer(leaf, tmp, ptr, inline_size);
6886
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006887 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006888 ret = btrfs_decompress(compress_type, tmp, page,
6889 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006890
6891 /*
6892 * decompression code contains a memset to fill in any space between the end
6893 * of the uncompressed data and the end of max_size in case the decompressed
6894 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6895 * the end of an inline extent and the beginning of the next block, so we
6896 * cover that region here.
6897 */
6898
6899 if (max_size + pg_offset < PAGE_SIZE) {
6900 char *map = kmap(page);
6901 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6902 kunmap(page);
6903 }
Chris Masonc8b97812008-10-29 14:49:59 -04006904 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006905 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006906}
6907
Chris Masond352ac62008-09-29 15:18:18 -04006908/*
6909 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006910 * the ugly parts come from merging extents from the disk with the in-ram
6911 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006912 * where the in-ram extents might be locked pending data=ordered completion.
6913 *
6914 * This also copies inline extents directly into the page.
6915 */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006916struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6917 struct page *page,
6918 size_t pg_offset, u64 start, u64 len,
6919 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006920{
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006921 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006922 int ret;
6923 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006924 u64 extent_start = 0;
6925 u64 extent_end = 0;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006926 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006927 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006928 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006929 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006930 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006931 struct extent_buffer *leaf;
6932 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006933 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006934 struct extent_map_tree *em_tree = &inode->extent_tree;
6935 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006936 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006937
Chris Mason890871b2009-09-02 16:24:52 -04006938 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006939 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006940 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006941 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006942 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006943
Chris Masona52d9a82007-08-27 16:49:44 -04006944 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006945 if (em->start > start || em->start + em->len <= start)
6946 free_extent_map(em);
6947 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006948 free_extent_map(em);
6949 else
6950 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006951 }
David Sterba172ddd62011-04-21 00:48:27 +02006952 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006953 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006954 err = -ENOMEM;
6955 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006956 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006957 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006958 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006959 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006960 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006961 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006962
6963 if (!path) {
6964 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006965 if (!path) {
6966 err = -ENOMEM;
6967 goto out;
6968 }
6969 /*
6970 * Chances are we'll be called again, so go ahead and do
6971 * readahead
6972 */
David Sterbae4058b52015-11-27 16:31:35 +01006973 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006974 }
6975
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006976 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006977 if (ret < 0) {
6978 err = ret;
6979 goto out;
6980 }
6981
6982 if (ret != 0) {
6983 if (path->slots[0] == 0)
6984 goto not_found;
6985 path->slots[0]--;
6986 }
6987
Chris Mason5f39d392007-10-15 16:14:19 -04006988 leaf = path->nodes[0];
6989 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006990 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006991 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006992 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006993 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006994 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006995 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006996 /*
6997 * If we backup past the first extent we want to move forward
6998 * and see if there is an extent in front of us, otherwise we'll
6999 * say there is a hole for our whole search range which can
7000 * cause problems.
7001 */
7002 extent_end = start;
7003 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007004 }
7005
Chris Mason5f39d392007-10-15 16:14:19 -04007006 found_type = btrfs_file_extent_type(leaf, item);
7007 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007008 if (found_type == BTRFS_FILE_EXTENT_REG ||
7009 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007010 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007011 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007012
7013 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7014 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007015 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7016 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007017 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007018 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007019 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007020
7021 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7022 path->slots[0],
7023 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007024 }
Josef Bacik25a50342013-10-14 12:08:38 -04007025next:
Yan Zheng9036c102008-10-30 14:19:41 -04007026 if (start >= extent_end) {
7027 path->slots[0]++;
7028 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7029 ret = btrfs_next_leaf(root, path);
7030 if (ret < 0) {
7031 err = ret;
7032 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007033 }
Yan Zheng9036c102008-10-30 14:19:41 -04007034 if (ret > 0)
7035 goto not_found;
7036 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007037 }
Yan Zheng9036c102008-10-30 14:19:41 -04007038 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7039 if (found_key.objectid != objectid ||
7040 found_key.type != BTRFS_EXTENT_DATA_KEY)
7041 goto not_found;
7042 if (start + len <= found_key.offset)
7043 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007044 if (start > found_key.offset)
7045 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007046 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007047 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007048 em->len = found_key.offset - start;
7049 goto not_found_em;
7050 }
7051
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007052 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007053 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007054
Yan Zhengd899e052008-10-30 14:25:28 -04007055 if (found_type == BTRFS_FILE_EXTENT_REG ||
7056 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007057 goto insert;
7058 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007059 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007060 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007061 size_t size;
7062 size_t extent_offset;
7063 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007064
Filipe Manana7ffbb592014-06-09 03:48:05 +01007065 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007066 goto out;
Yan689f9342007-10-29 11:41:07 -04007067
Chris Mason514ac8a2014-01-03 21:07:00 -08007068 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007069 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007070 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7071 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007072 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007073 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007074 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007075 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007076 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007077 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007078 if (btrfs_file_extent_compression(leaf, item) !=
7079 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007080 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007081 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007082 if (ret) {
7083 err = ret;
7084 goto out;
7085 }
Chris Masonc8b97812008-10-29 14:49:59 -04007086 } else {
7087 map = kmap(page);
7088 read_extent_buffer(leaf, map + pg_offset, ptr,
7089 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007090 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007091 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007092 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007093 copy_size);
7094 }
Chris Masonc8b97812008-10-29 14:49:59 -04007095 kunmap(page);
7096 }
Chris Mason179e29e2007-11-01 11:28:41 -04007097 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007098 }
Chris Masond1310b22008-01-24 16:13:08 -05007099 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007100 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007101 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007102 }
7103not_found:
7104 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007105 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007106 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007107not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007108 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007109insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007110 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007111 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007112 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007113 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7114 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007115 err = -EIO;
7116 goto out;
7117 }
Chris Masond1310b22008-01-24 16:13:08 -05007118
7119 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007120 write_lock(&em_tree->lock);
Liu Bo7b4df0582018-01-05 12:51:10 -07007121 err = btrfs_add_extent_mapping(em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007122 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007123out:
liubo1abe9b82011-03-24 11:18:59 +00007124
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007125 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007126
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007127 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007128 if (err) {
7129 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007130 return ERR_PTR(err);
7131 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007132 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007133 return em;
7134}
7135
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007136struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7137 struct page *page,
7138 size_t pg_offset, u64 start, u64 len,
7139 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007140{
7141 struct extent_map *em;
7142 struct extent_map *hole_em = NULL;
7143 u64 range_start = start;
7144 u64 end;
7145 u64 found;
7146 u64 found_end;
7147 int err = 0;
7148
7149 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7150 if (IS_ERR(em))
7151 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007152 /*
7153 * If our em maps to:
7154 * - a hole or
7155 * - a pre-alloc extent,
7156 * there might actually be delalloc bytes behind it.
7157 */
7158 if (em->block_start != EXTENT_MAP_HOLE &&
7159 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7160 return em;
7161 else
7162 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007163
7164 /* check to see if we've wrapped (len == -1 or similar) */
7165 end = start + len;
7166 if (end < start)
7167 end = (u64)-1;
7168 else
7169 end -= 1;
7170
7171 em = NULL;
7172
7173 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007174 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007175 end, len, EXTENT_DELALLOC, 1);
7176 found_end = range_start + found;
7177 if (found_end < range_start)
7178 found_end = (u64)-1;
7179
7180 /*
7181 * we didn't find anything useful, return
7182 * the original results from get_extent()
7183 */
7184 if (range_start > end || found_end <= start) {
7185 em = hole_em;
7186 hole_em = NULL;
7187 goto out;
7188 }
7189
7190 /* adjust the range_start to make sure it doesn't
7191 * go backwards from the start they passed in
7192 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307193 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007194 found = found_end - range_start;
7195
7196 if (found > 0) {
7197 u64 hole_start = start;
7198 u64 hole_len = len;
7199
David Sterba172ddd62011-04-21 00:48:27 +02007200 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007201 if (!em) {
7202 err = -ENOMEM;
7203 goto out;
7204 }
7205 /*
7206 * when btrfs_get_extent can't find anything it
7207 * returns one huge hole
7208 *
7209 * make sure what it found really fits our range, and
7210 * adjust to make sure it is based on the start from
7211 * the caller
7212 */
7213 if (hole_em) {
7214 u64 calc_end = extent_map_end(hole_em);
7215
7216 if (calc_end <= start || (hole_em->start > end)) {
7217 free_extent_map(hole_em);
7218 hole_em = NULL;
7219 } else {
7220 hole_start = max(hole_em->start, start);
7221 hole_len = calc_end - hole_start;
7222 }
7223 }
7224 em->bdev = NULL;
7225 if (hole_em && range_start > hole_start) {
7226 /* our hole starts before our delalloc, so we
7227 * have to return just the parts of the hole
7228 * that go until the delalloc starts
7229 */
7230 em->len = min(hole_len,
7231 range_start - hole_start);
7232 em->start = hole_start;
7233 em->orig_start = hole_start;
7234 /*
7235 * don't adjust block start at all,
7236 * it is fixed at EXTENT_MAP_HOLE
7237 */
7238 em->block_start = hole_em->block_start;
7239 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007240 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7241 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007242 } else {
7243 em->start = range_start;
7244 em->len = found;
7245 em->orig_start = range_start;
7246 em->block_start = EXTENT_MAP_DELALLOC;
7247 em->block_len = found;
7248 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007249 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007250 return hole_em;
7251 }
7252out:
7253
7254 free_extent_map(hole_em);
7255 if (err) {
7256 free_extent_map(em);
7257 return ERR_PTR(err);
7258 }
7259 return em;
7260}
7261
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007262static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7263 const u64 start,
7264 const u64 len,
7265 const u64 orig_start,
7266 const u64 block_start,
7267 const u64 block_len,
7268 const u64 orig_block_len,
7269 const u64 ram_bytes,
7270 const int type)
7271{
7272 struct extent_map *em = NULL;
7273 int ret;
7274
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007275 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007276 em = create_io_em(inode, start, len, orig_start,
7277 block_start, block_len, orig_block_len,
7278 ram_bytes,
7279 BTRFS_COMPRESS_NONE, /* compress_type */
7280 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007281 if (IS_ERR(em))
7282 goto out;
7283 }
7284 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7285 len, block_len, type);
7286 if (ret) {
7287 if (em) {
7288 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007289 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007290 start + len - 1, 0);
7291 }
7292 em = ERR_PTR(ret);
7293 }
7294 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007295
7296 return em;
7297}
7298
Josef Bacik4b46fce2010-05-23 11:00:55 -04007299static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7300 u64 start, u64 len)
7301{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007302 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007303 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007304 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007305 struct btrfs_key ins;
7306 u64 alloc_hint;
7307 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007308
Josef Bacik4b46fce2010-05-23 11:00:55 -04007309 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007310 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007311 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007312 if (ret)
7313 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007314
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007315 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7316 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007317 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007318 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007319 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007320 btrfs_free_reserved_extent(fs_info, ins.objectid,
7321 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007322
Josef Bacik4b46fce2010-05-23 11:00:55 -04007323 return em;
7324}
7325
Chris Mason46bfbb52010-05-26 11:04:10 -04007326/*
7327 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7328 * block must be cow'd
7329 */
Josef Bacik00361582013-08-14 14:02:47 -04007330noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007331 u64 *orig_start, u64 *orig_block_len,
7332 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007333{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007334 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007335 struct btrfs_path *path;
7336 int ret;
7337 struct extent_buffer *leaf;
7338 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007339 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007340 struct btrfs_file_extent_item *fi;
7341 struct btrfs_key key;
7342 u64 disk_bytenr;
7343 u64 backref_offset;
7344 u64 extent_end;
7345 u64 num_bytes;
7346 int slot;
7347 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007348 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007349
Chris Mason46bfbb52010-05-26 11:04:10 -04007350 path = btrfs_alloc_path();
7351 if (!path)
7352 return -ENOMEM;
7353
David Sterbaf85b7372017-01-20 14:54:07 +01007354 ret = btrfs_lookup_file_extent(NULL, root, path,
7355 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007356 if (ret < 0)
7357 goto out;
7358
7359 slot = path->slots[0];
7360 if (ret == 1) {
7361 if (slot == 0) {
7362 /* can't find the item, must cow */
7363 ret = 0;
7364 goto out;
7365 }
7366 slot--;
7367 }
7368 ret = 0;
7369 leaf = path->nodes[0];
7370 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007371 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007372 key.type != BTRFS_EXTENT_DATA_KEY) {
7373 /* not our file or wrong item type, must cow */
7374 goto out;
7375 }
7376
7377 if (key.offset > offset) {
7378 /* Wrong offset, must cow */
7379 goto out;
7380 }
7381
7382 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7383 found_type = btrfs_file_extent_type(leaf, fi);
7384 if (found_type != BTRFS_FILE_EXTENT_REG &&
7385 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7386 /* not a regular extent, must cow */
7387 goto out;
7388 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007389
7390 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7391 goto out;
7392
Miao Xiee77751a2013-12-27 21:11:50 +08007393 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7394 if (extent_end <= offset)
7395 goto out;
7396
Chris Mason46bfbb52010-05-26 11:04:10 -04007397 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007398 if (disk_bytenr == 0)
7399 goto out;
7400
7401 if (btrfs_file_extent_compression(leaf, fi) ||
7402 btrfs_file_extent_encryption(leaf, fi) ||
7403 btrfs_file_extent_other_encoding(leaf, fi))
7404 goto out;
7405
Chris Mason46bfbb52010-05-26 11:04:10 -04007406 backref_offset = btrfs_file_extent_offset(leaf, fi);
7407
Josef Bacik7ee9e442013-06-21 16:37:03 -04007408 if (orig_start) {
7409 *orig_start = key.offset - backref_offset;
7410 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7411 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7412 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007413
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007414 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007415 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007416
7417 num_bytes = min(offset + *len, extent_end) - offset;
7418 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7419 u64 range_end;
7420
Jeff Mahoneyda170662016-06-15 09:22:56 -04007421 range_end = round_up(offset + num_bytes,
7422 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007423 ret = test_range_bit(io_tree, offset, range_end,
7424 EXTENT_DELALLOC, 0, NULL);
7425 if (ret) {
7426 ret = -EAGAIN;
7427 goto out;
7428 }
7429 }
7430
Josef Bacik1bda19e2013-10-18 12:10:36 -04007431 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007432
7433 /*
7434 * look for other files referencing this extent, if we
7435 * find any we must cow
7436 */
Josef Bacik00361582013-08-14 14:02:47 -04007437
Liu Boe4c3b2d2017-01-30 12:25:28 -08007438 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007439 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007440 if (ret) {
7441 ret = 0;
7442 goto out;
7443 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007444
7445 /*
7446 * adjust disk_bytenr and num_bytes to cover just the bytes
7447 * in this extent we are about to write. If there
7448 * are any csums in that range we have to cow in order
7449 * to keep the csums correct
7450 */
7451 disk_bytenr += backref_offset;
7452 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007453 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7454 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007455 /*
7456 * all of the above have passed, it is safe to overwrite this extent
7457 * without cow
7458 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007459 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007460 ret = 1;
7461out:
7462 btrfs_free_path(path);
7463 return ret;
7464}
7465
Josef Bacikeb838e72012-07-31 16:28:48 -04007466static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7467 struct extent_state **cached_state, int writing)
7468{
7469 struct btrfs_ordered_extent *ordered;
7470 int ret = 0;
7471
7472 while (1) {
7473 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007474 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007475 /*
7476 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007477 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007478 * extents in this range.
7479 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007480 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007481 lockend - lockstart + 1);
7482
7483 /*
7484 * We need to make sure there are no buffered pages in this
7485 * range either, we could have raced between the invalidate in
7486 * generic_file_direct_write and locking the extent. The
7487 * invalidate needs to happen so that reads after a write do not
7488 * get stale data.
7489 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007490 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007491 (!writing || !filemap_range_has_page(inode->i_mapping,
7492 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007493 break;
7494
7495 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007496 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007497
7498 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007499 /*
7500 * If we are doing a DIO read and the ordered extent we
7501 * found is for a buffered write, we can not wait for it
7502 * to complete and retry, because if we do so we can
7503 * deadlock with concurrent buffered writes on page
7504 * locks. This happens only if our DIO read covers more
7505 * than one extent map, if at this point has already
7506 * created an ordered extent for a previous extent map
7507 * and locked its range in the inode's io tree, and a
7508 * concurrent write against that previous extent map's
7509 * range and this range started (we unlock the ranges
7510 * in the io tree only when the bios complete and
7511 * buffered writes always lock pages before attempting
7512 * to lock range in the io tree).
7513 */
7514 if (writing ||
7515 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7516 btrfs_start_ordered_extent(inode, ordered, 1);
7517 else
7518 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007519 btrfs_put_ordered_extent(ordered);
7520 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007521 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007522 * We could trigger writeback for this range (and wait
7523 * for it to complete) and then invalidate the pages for
7524 * this range (through invalidate_inode_pages2_range()),
7525 * but that can lead us to a deadlock with a concurrent
7526 * call to readpages() (a buffered read or a defrag call
7527 * triggered a readahead) on a page lock due to an
7528 * ordered dio extent we created before but did not have
7529 * yet a corresponding bio submitted (whence it can not
7530 * complete), which makes readpages() wait for that
7531 * ordered extent to complete while holding a lock on
7532 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007533 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007534 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007535 }
7536
Filipe Mananaade77022016-02-18 14:28:55 +00007537 if (ret)
7538 break;
7539
Josef Bacikeb838e72012-07-31 16:28:48 -04007540 cond_resched();
7541 }
7542
7543 return ret;
7544}
7545
Liu Bo6f9994d2017-01-31 07:50:22 -08007546/* The callers of this must take lock_extent() */
7547static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7548 u64 orig_start, u64 block_start,
7549 u64 block_len, u64 orig_block_len,
7550 u64 ram_bytes, int compress_type,
7551 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007552{
7553 struct extent_map_tree *em_tree;
7554 struct extent_map *em;
7555 struct btrfs_root *root = BTRFS_I(inode)->root;
7556 int ret;
7557
Liu Bo6f9994d2017-01-31 07:50:22 -08007558 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7559 type == BTRFS_ORDERED_COMPRESSED ||
7560 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007561 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007562
Josef Bacik69ffb542012-09-11 15:40:07 -04007563 em_tree = &BTRFS_I(inode)->extent_tree;
7564 em = alloc_extent_map();
7565 if (!em)
7566 return ERR_PTR(-ENOMEM);
7567
7568 em->start = start;
7569 em->orig_start = orig_start;
7570 em->len = len;
7571 em->block_len = block_len;
7572 em->block_start = block_start;
7573 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007574 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007575 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007576 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007577 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007578 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007579 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007580 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007581 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7582 em->compress_type = compress_type;
7583 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007584
7585 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007586 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007587 em->start + em->len - 1, 0);
7588 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007589 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007590 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007591 /*
7592 * The caller has taken lock_extent(), who could race with us
7593 * to add em?
7594 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007595 } while (ret == -EEXIST);
7596
7597 if (ret) {
7598 free_extent_map(em);
7599 return ERR_PTR(ret);
7600 }
7601
Liu Bo6f9994d2017-01-31 07:50:22 -08007602 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007603 return em;
7604}
7605
Josef Bacik4b46fce2010-05-23 11:00:55 -04007606static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7607 struct buffer_head *bh_result, int create)
7608{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007609 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007610 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007611 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307612 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007613 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007614 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007615 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007616 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007617 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007618
Miao Xie172a5042013-02-21 02:48:22 -07007619 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007620 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007621 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007622 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007623
Josef Bacikc3298612012-08-03 16:49:19 -04007624 lockstart = start;
7625 lockend = start + len - 1;
7626
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007627 if (current->journal_info) {
7628 /*
7629 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007630 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007631 * confused.
7632 */
chandan50745b02015-08-28 21:10:13 +05307633 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007634 current->journal_info = NULL;
7635 }
7636
Josef Bacikeb838e72012-07-31 16:28:48 -04007637 /*
7638 * If this errors out it's because we couldn't invalidate pagecache for
7639 * this range and we need to fallback to buffered.
7640 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007641 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7642 create)) {
7643 ret = -ENOTBLK;
7644 goto err;
7645 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007646
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007647 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007648 if (IS_ERR(em)) {
7649 ret = PTR_ERR(em);
7650 goto unlock_err;
7651 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007652
7653 /*
7654 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7655 * io. INLINE is special, and we could probably kludge it in here, but
7656 * it's still buffered so for safety lets just fall back to the generic
7657 * buffered path.
7658 *
7659 * For COMPRESSED we _have_ to read the entire extent in so we can
7660 * decompress it, so there will be buffering required no matter what we
7661 * do, so go ahead and fallback to buffered.
7662 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007663 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007664 * to buffered IO. Don't blame me, this is the price we pay for using
7665 * the generic code.
7666 */
7667 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7668 em->block_start == EXTENT_MAP_INLINE) {
7669 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007670 ret = -ENOTBLK;
7671 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007672 }
7673
7674 /* Just a good old fashioned hole, return */
7675 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7676 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7677 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007678 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007679 }
7680
7681 /*
7682 * We don't allocate a new extent in the following cases
7683 *
7684 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7685 * existing extent.
7686 * 2) The extent is marked as PREALLOC. We're good to go here and can
7687 * just use the extent.
7688 *
7689 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007690 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007691 len = min(len, em->len - (start - em->start));
7692 lockstart = start + len;
7693 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007694 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007695
7696 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7697 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7698 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007699 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007700 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007701
7702 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7703 type = BTRFS_ORDERED_PREALLOC;
7704 else
7705 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007706 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007707 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007708
Josef Bacik00361582013-08-14 14:02:47 -04007709 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007710 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007711 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007712 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007713
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007714 em2 = btrfs_create_dio_extent(inode, start, len,
7715 orig_start, block_start,
7716 len, orig_block_len,
7717 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007718 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007719 if (type == BTRFS_ORDERED_PREALLOC) {
7720 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007721 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007722 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007723 if (em2 && IS_ERR(em2)) {
7724 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007725 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007726 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007727 /*
7728 * For inode marked NODATACOW or extent marked PREALLOC,
7729 * use the existing or preallocated extent, so does not
7730 * need to adjust btrfs_space_info's bytes_may_use.
7731 */
7732 btrfs_free_reserved_data_space_noquota(inode,
7733 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007734 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007735 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007736 }
Josef Bacik00361582013-08-14 14:02:47 -04007737
Chris Mason46bfbb52010-05-26 11:04:10 -04007738 /*
7739 * this will cow the extent, reset the len in case we changed
7740 * it above
7741 */
7742 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007743 free_extent_map(em);
7744 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007745 if (IS_ERR(em)) {
7746 ret = PTR_ERR(em);
7747 goto unlock_err;
7748 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007749 len = min(len, em->len - (start - em->start));
7750unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007751 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7752 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007753 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007754 bh_result->b_bdev = em->bdev;
7755 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007756 if (create) {
7757 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7758 set_buffer_new(bh_result);
7759
7760 /*
7761 * Need to update the i_size under the extent lock so buffered
7762 * readers will get the updated i_size when we unlock.
7763 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007764 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007765 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007766
chandan50745b02015-08-28 21:10:13 +05307767 WARN_ON(dio_data->reserve < len);
7768 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007769 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307770 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007771 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007772
Josef Bacikeb838e72012-07-31 16:28:48 -04007773 /*
7774 * In the case of write we need to clear and unlock the entire range,
7775 * in the case of read we need to unlock only the end area that we
7776 * aren't using if there is any left over space.
7777 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007778 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007779 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7780 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007781 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007782 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007783 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007784 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007785
Josef Bacik4b46fce2010-05-23 11:00:55 -04007786 free_extent_map(em);
7787
7788 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007789
7790unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007791 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007792 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007793err:
chandan50745b02015-08-28 21:10:13 +05307794 if (dio_data)
7795 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007796 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007797}
7798
Omar Sandoval58efbc92017-08-22 23:45:59 -07007799static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7800 struct bio *bio,
7801 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007802{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007803 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007804 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007805
Mike Christie37226b22016-06-05 14:31:52 -05007806 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007807
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007808 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007809 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007810 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007811
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007812 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007813
Miao Xie8b110e32014-09-12 18:44:03 +08007814 return ret;
7815}
7816
7817static int btrfs_check_dio_repairable(struct inode *inode,
7818 struct bio *failed_bio,
7819 struct io_failure_record *failrec,
7820 int failed_mirror)
7821{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007822 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007823 int num_copies;
7824
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007825 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007826 if (num_copies == 1) {
7827 /*
7828 * we only have a single copy of the data, so don't bother with
7829 * all the retry and error correction code that follows. no
7830 * matter what the error is, it is very likely to persist.
7831 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007832 btrfs_debug(fs_info,
7833 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7834 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007835 return 0;
7836 }
7837
7838 failrec->failed_mirror = failed_mirror;
7839 failrec->this_mirror++;
7840 if (failrec->this_mirror == failed_mirror)
7841 failrec->this_mirror++;
7842
7843 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007844 btrfs_debug(fs_info,
7845 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7846 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007847 return 0;
7848 }
7849
7850 return 1;
7851}
7852
Omar Sandoval58efbc92017-08-22 23:45:59 -07007853static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7854 struct page *page, unsigned int pgoff,
7855 u64 start, u64 end, int failed_mirror,
7856 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007857{
7858 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007859 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7860 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007861 struct bio *bio;
7862 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007863 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007864 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007865 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007866 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007867 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007868
Mike Christie37226b22016-06-05 14:31:52 -05007869 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007870
7871 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7872 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007873 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007874
7875 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7876 failed_mirror);
7877 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007878 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007879 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007880 }
7881
Liu Bo17347ce2017-05-15 15:33:27 -07007882 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007883 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007884 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007885 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007886 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007887
7888 isector = start - btrfs_io_bio(failed_bio)->logical;
7889 isector >>= inode->i_sb->s_blocksize_bits;
7890 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307891 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05007892 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007893
7894 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007895 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007896 read_mode, failrec->this_mirror, failrec->in_validation);
7897
Omar Sandoval58efbc92017-08-22 23:45:59 -07007898 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7899 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007900 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007901 bio_put(bio);
7902 }
7903
Omar Sandoval58efbc92017-08-22 23:45:59 -07007904 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007905}
7906
7907struct btrfs_retry_complete {
7908 struct completion done;
7909 struct inode *inode;
7910 u64 start;
7911 int uptodate;
7912};
7913
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007914static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007915{
7916 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007917 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007918 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007919 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007920 int i;
7921
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007922 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007923 goto end;
7924
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307925 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007926 io_tree = &BTRFS_I(inode)->io_tree;
7927 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007928 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307929
Miao Xie8b110e32014-09-12 18:44:03 +08007930 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007931 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007932 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007933 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7934 io_tree, done->start, bvec->bv_page,
7935 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007936end:
7937 complete(&done->done);
7938 bio_put(bio);
7939}
7940
Omar Sandoval58efbc92017-08-22 23:45:59 -07007941static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7942 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007943{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307944 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007945 struct bio_vec bvec;
7946 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007947 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007948 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307949 unsigned int pgoff;
7950 u32 sectorsize;
7951 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007952 blk_status_t ret;
7953 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007954
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307955 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007956 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307957
Miao Xiec1dc0892014-09-12 18:43:56 +08007958 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007959 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007960 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007961
Liu Bo17347ce2017-05-15 15:33:27 -07007962 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7963 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7964 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307965
7966next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007967 done.uptodate = 0;
7968 done.start = start;
7969 init_completion(&done.done);
7970
Liu Bo17347ce2017-05-15 15:33:27 -07007971 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307972 pgoff, start, start + sectorsize - 1,
7973 io_bio->mirror_num,
7974 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007975 if (ret) {
7976 err = ret;
7977 goto next;
7978 }
Miao Xie8b110e32014-09-12 18:44:03 +08007979
David Sterba9c17f6c2017-07-19 19:26:45 +02007980 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007981
7982 if (!done.uptodate) {
7983 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307984 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007985 }
7986
Liu Bo629ebf42017-05-15 17:20:07 -07007987next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307988 start += sectorsize;
7989
Liu Bo97bf5a52017-04-07 13:11:10 -07007990 nr_sectors--;
7991 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307992 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007993 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307994 goto next_block_or_try_again;
7995 }
Miao Xie8b110e32014-09-12 18:44:03 +08007996 }
7997
Liu Bo629ebf42017-05-15 17:20:07 -07007998 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007999}
8000
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008001static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008002{
8003 struct btrfs_retry_complete *done = bio->bi_private;
8004 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008005 struct extent_io_tree *io_tree, *failure_tree;
8006 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008007 struct bio_vec *bvec;
8008 int uptodate;
8009 int ret;
8010 int i;
8011
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008012 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008013 goto end;
8014
8015 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308016
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308017 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08008018 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308019
Josef Bacik7870d082017-05-05 11:57:15 -04008020 io_tree = &BTRFS_I(inode)->io_tree;
8021 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8022
David Sterbac09abff2017-07-13 18:10:07 +02008023 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008024 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008025 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8026 bvec->bv_offset, done->start,
8027 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008028 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008029 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8030 failure_tree, io_tree, done->start,
8031 bvec->bv_page,
8032 btrfs_ino(BTRFS_I(inode)),
8033 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008034 else
8035 uptodate = 0;
8036 }
8037
8038 done->uptodate = uptodate;
8039end:
8040 complete(&done->done);
8041 bio_put(bio);
8042}
8043
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008044static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8045 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008046{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308047 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008048 struct bio_vec bvec;
8049 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008050 struct btrfs_retry_complete done;
8051 u64 start;
8052 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308053 u32 sectorsize;
8054 int nr_sectors;
8055 unsigned int pgoff;
8056 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008057 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008058 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008059 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008060
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308061 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008062 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308063
Omar Sandoval58efbc92017-08-22 23:45:59 -07008064 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008065 start = io_bio->logical;
8066 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008067 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008068
Liu Bo17347ce2017-05-15 15:33:27 -07008069 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8070 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308071
Liu Bo17347ce2017-05-15 15:33:27 -07008072 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308073next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008074 if (uptodate) {
8075 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8076 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8077 bvec.bv_page, pgoff, start, sectorsize);
8078 if (likely(!ret))
8079 goto next;
8080 }
Miao Xie8b110e32014-09-12 18:44:03 +08008081try_again:
8082 done.uptodate = 0;
8083 done.start = start;
8084 init_completion(&done.done);
8085
Omar Sandoval58efbc92017-08-22 23:45:59 -07008086 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8087 pgoff, start, start + sectorsize - 1,
8088 io_bio->mirror_num, btrfs_retry_endio,
8089 &done);
8090 if (status) {
8091 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008092 goto next;
8093 }
8094
David Sterba9c17f6c2017-07-19 19:26:45 +02008095 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008096
8097 if (!done.uptodate) {
8098 /* We might have another mirror, so try again */
8099 goto try_again;
8100 }
8101next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308102 offset += sectorsize;
8103 start += sectorsize;
8104
8105 ASSERT(nr_sectors);
8106
Liu Bo97bf5a52017-04-07 13:11:10 -07008107 nr_sectors--;
8108 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308109 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008110 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308111 goto next_block;
8112 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008113 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008114
8115 return err;
8116}
8117
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008118static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8119 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008120{
8121 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8122
8123 if (skip_csum) {
8124 if (unlikely(err))
8125 return __btrfs_correct_data_nocsum(inode, io_bio);
8126 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008127 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008128 } else {
8129 return __btrfs_subio_endio_read(inode, io_bio, err);
8130 }
8131}
8132
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008133static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008134{
8135 struct btrfs_dio_private *dip = bio->bi_private;
8136 struct inode *inode = dip->inode;
8137 struct bio *dio_bio;
8138 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008139 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008140
Liu Bo99c4e3b2017-09-15 15:06:51 -06008141 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008142 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008143
Josef Bacik4b46fce2010-05-23 11:00:55 -04008144 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008145 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008146 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008147
Josef Bacik4b46fce2010-05-23 11:00:55 -04008148 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008149
Liu Bo99c4e3b2017-09-15 15:06:51 -06008150 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008151 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008152
8153 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008154 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008155 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008156}
8157
Qu Wenruo524272602017-03-08 10:25:52 +08008158static void __endio_write_update_ordered(struct inode *inode,
8159 const u64 offset, const u64 bytes,
8160 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008161{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008162 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008163 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008164 struct btrfs_workqueue *wq;
8165 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008166 u64 ordered_offset = offset;
8167 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008168 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008169 int ret;
8170
Qu Wenruo524272602017-03-08 10:25:52 +08008171 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8172 wq = fs_info->endio_freespace_worker;
8173 func = btrfs_freespace_write_helper;
8174 } else {
8175 wq = fs_info->endio_write_workers;
8176 func = btrfs_endio_write_helper;
8177 }
8178
Chris Mason163cf092010-11-28 19:56:33 -05008179again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008180 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008181 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8182 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008183 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008184 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008185 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008186 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008187
Qu Wenruo524272602017-03-08 10:25:52 +08008188 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8189 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008190out_test:
8191 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008192 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8193 * in the range, we can exit.
8194 */
8195 if (ordered_offset == last_offset)
8196 return;
8197 /*
Chris Mason163cf092010-11-28 19:56:33 -05008198 * our bio might span multiple ordered extents. If we haven't
8199 * completed the accounting for the whole dio, go back and try again
8200 */
Filipe Manana14543772015-11-24 16:23:54 +00008201 if (ordered_offset < offset + bytes) {
8202 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008203 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008204 goto again;
8205 }
Filipe Manana14543772015-11-24 16:23:54 +00008206}
8207
8208static void btrfs_endio_direct_write(struct bio *bio)
8209{
8210 struct btrfs_dio_private *dip = bio->bi_private;
8211 struct bio *dio_bio = dip->dio_bio;
8212
Qu Wenruo524272602017-03-08 10:25:52 +08008213 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008214 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008215
Josef Bacik4b46fce2010-05-23 11:00:55 -04008216 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008217
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008218 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008219 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008220 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008221}
8222
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008223static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008224 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008225{
Josef Bacikc6100a42017-05-05 11:57:13 -04008226 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008227 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008228 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008229 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008230 return 0;
8231}
8232
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008233static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008234{
8235 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008236 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008237
Miao Xie8b110e32014-09-12 18:44:03 +08008238 if (err)
8239 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008240 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008241 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8242 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008243 (unsigned long long)bio->bi_iter.bi_sector,
8244 bio->bi_iter.bi_size, err);
8245
8246 if (dip->subio_endio)
8247 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008248
8249 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008250 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008251 * We want to perceive the errors flag being set before
8252 * decrementing the reference count. We don't need a barrier
8253 * since atomic operations with a return value are fully
8254 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008255 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008256 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008257 }
8258
8259 /* if there are more bios still pending for this dio, just exit */
8260 if (!atomic_dec_and_test(&dip->pending_bios))
8261 goto out;
8262
Chris Mason9be33952013-05-17 18:30:14 -04008263 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008264 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008265 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008266 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008267 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008268 }
8269out:
8270 bio_put(bio);
8271}
8272
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008273static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008274 struct btrfs_dio_private *dip,
8275 struct bio *bio,
8276 u64 file_offset)
8277{
8278 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8279 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008280 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008281
8282 /*
8283 * We load all the csum data we need when we submit
8284 * the first bio to reduce the csum tree search and
8285 * contention.
8286 */
8287 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008288 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008289 file_offset);
8290 if (ret)
8291 return ret;
8292 }
8293
8294 if (bio == dip->orig_bio)
8295 return 0;
8296
8297 file_offset -= dip->logical_offset;
8298 file_offset >>= inode->i_sb->s_blocksize_bits;
8299 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8300
8301 return 0;
8302}
8303
Omar Sandoval58efbc92017-08-22 23:45:59 -07008304static inline blk_status_t
8305__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008306 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008307{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008308 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008309 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008310 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008311 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008312
Liu Bo4c274bc2017-11-01 17:19:27 -06008313 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008314 if (async_submit)
8315 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8316
Josef Bacik5fd02042012-05-02 14:00:54 -04008317 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008318 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008319 if (ret)
8320 goto err;
8321 }
Miao Xiee65e1532010-11-22 03:04:43 +00008322
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008323 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008324 goto map;
8325
8326 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008327 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8328 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008329 __btrfs_submit_bio_start_direct_io,
8330 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008331 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008332 } else if (write) {
8333 /*
8334 * If we aren't doing async submit, calculate the csum of the
8335 * bio now.
8336 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008337 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008338 if (ret)
8339 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008340 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008341 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008342 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008343 if (ret)
8344 goto err;
8345 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008346map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008347 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008348err:
Miao Xiee65e1532010-11-22 03:04:43 +00008349 return ret;
8350}
8351
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008352static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008353{
8354 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008355 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008356 struct bio *bio;
8357 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008358 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008359 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008360 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008361 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008362 u64 submit_len;
8363 int clone_offset = 0;
8364 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308365 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008366 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008367
Kent Overstreet4f024f32013-10-11 15:44:27 -07008368 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008369 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008370 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8371 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008372 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008373 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008374
Liu Bo725130b2017-05-16 09:51:39 -07008375 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008376 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008377 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008378 goto submit;
8379 }
8380
David Woodhouse53b381b2013-01-29 18:40:14 -05008381 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008382 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008383 async_submit = 0;
8384 else
8385 async_submit = 1;
8386
Liu Bo725130b2017-05-16 09:51:39 -07008387 /* bio split */
8388 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008389 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008390 do {
Liu Bo725130b2017-05-16 09:51:39 -07008391 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008392
Liu Bo725130b2017-05-16 09:51:39 -07008393 /*
8394 * This will never fail as it's passing GPF_NOFS and
8395 * the allocation is backed by btrfs_bioset.
8396 */
Liu Boe4770942017-05-16 10:57:14 -07008397 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008398 clone_len);
8399 bio->bi_private = dip;
8400 bio->bi_end_io = btrfs_end_dio_bio;
8401 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008402
Liu Bo725130b2017-05-16 09:51:39 -07008403 ASSERT(submit_len >= clone_len);
8404 submit_len -= clone_len;
8405 if (submit_len == 0)
8406 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008407
Liu Bo725130b2017-05-16 09:51:39 -07008408 /*
8409 * Increase the count before we submit the bio so we know
8410 * the end IO handler won't happen before we increase the
8411 * count. Otherwise, the dip might get freed before we're
8412 * done setting it up.
8413 */
8414 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008415
Linus Torvalds66ba7722017-09-09 13:27:51 -07008416 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008417 async_submit);
8418 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008419 bio_put(bio);
8420 atomic_dec(&dip->pending_bios);
8421 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008422 }
Liu Bo725130b2017-05-16 09:51:39 -07008423
8424 clone_offset += clone_len;
8425 start_sector += clone_len >> 9;
8426 file_offset += clone_len;
8427
8428 map_length = submit_len;
8429 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8430 start_sector << 9, &map_length, NULL, 0);
8431 if (ret)
8432 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008433 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008434
Josef Bacik02f57c72011-04-06 14:25:44 -04008435submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008436 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008437 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008438 return 0;
8439
8440 bio_put(bio);
8441out_err:
8442 dip->errors = 1;
8443 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008444 * Before atomic variable goto zero, we must make sure dip->errors is
8445 * perceived to be set. This ordering is ensured by the fact that an
8446 * atomic operations with a return value are fully ordered as per
8447 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008448 */
Miao Xiee65e1532010-11-22 03:04:43 +00008449 if (atomic_dec_and_test(&dip->pending_bios))
8450 bio_io_error(dip->orig_bio);
8451
8452 /* bio_end_io() will handle error, so we needn't return it */
8453 return 0;
8454}
8455
Mike Christie8a4c1e42016-06-05 14:31:50 -05008456static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8457 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008458{
Filipe Manana61de7182015-07-01 12:13:10 +01008459 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008460 struct bio *bio = NULL;
8461 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008462 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008463 int ret = 0;
8464
David Sterba8b6c1d52017-06-02 17:48:13 +02008465 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008466
Miao Xiec1dc0892014-09-12 18:43:56 +08008467 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008468 if (!dip) {
8469 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008470 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008471 }
8472
8473 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008474 dip->inode = inode;
8475 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008476 dip->bytes = dio_bio->bi_iter.bi_size;
8477 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008478 bio->bi_private = dip;
8479 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008480 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008481 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008482 io_bio = btrfs_io_bio(bio);
8483 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008484
Miao Xiec1dc0892014-09-12 18:43:56 +08008485 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008486 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008487 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008488 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008489 dip->subio_endio = btrfs_subio_endio_read;
8490 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008491
Filipe Mananaf28a4922015-12-08 19:23:20 +00008492 /*
8493 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8494 * even if we fail to submit a bio, because in such case we do the
8495 * corresponding error handling below and it must not be done a second
8496 * time by btrfs_direct_IO().
8497 */
8498 if (write) {
8499 struct btrfs_dio_data *dio_data = current->journal_info;
8500
8501 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8502 dip->bytes;
8503 dio_data->unsubmitted_oe_range_start =
8504 dio_data->unsubmitted_oe_range_end;
8505 }
8506
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008507 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008508 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008509 return;
Chris Mason9be33952013-05-17 18:30:14 -04008510
Liu Bo3892ac92017-04-17 15:00:28 -07008511 if (io_bio->end_io)
8512 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008513
Josef Bacik4b46fce2010-05-23 11:00:55 -04008514free_ordered:
8515 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008516 * If we arrived here it means either we failed to submit the dip
8517 * or we either failed to clone the dio_bio or failed to allocate the
8518 * dip. If we cloned the dio_bio and allocated the dip, we can just
8519 * call bio_endio against our io_bio so that we get proper resource
8520 * cleanup if we fail to submit the dip, otherwise, we must do the
8521 * same as btrfs_endio_direct_[write|read] because we can't call these
8522 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008523 */
Liu Bo3892ac92017-04-17 15:00:28 -07008524 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008525 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008526 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008527 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008528 * and all the cleanup and final put for dio_bio (through
8529 * dio_end_io()).
8530 */
8531 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008532 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008533 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008534 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008535 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008536 file_offset,
8537 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008538 false);
Filipe Manana14543772015-11-24 16:23:54 +00008539 else
Filipe Manana61de7182015-07-01 12:13:10 +01008540 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8541 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008542
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008543 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008544 /*
8545 * Releases and cleans up our dio_bio, no need to bio_put()
8546 * nor bio_endio()/bio_io_error() against dio_bio.
8547 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008548 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008549 }
Liu Bo3892ac92017-04-17 15:00:28 -07008550 if (bio)
8551 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008552 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008553}
8554
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008555static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008556 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008557{
8558 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008559 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008560 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008561 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008562
8563 if (offset & blocksize_mask)
8564 goto out;
8565
Al Viro28060d52014-03-22 05:15:17 -04008566 if (iov_iter_alignment(iter) & blocksize_mask)
8567 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008568
Al Viro28060d52014-03-22 05:15:17 -04008569 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008570 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008571 return 0;
8572 /*
8573 * Check to make sure we don't have duplicate iov_base's in this
8574 * iovec, if so return EINVAL, otherwise we'll get csum errors
8575 * when reading back.
8576 */
8577 for (seg = 0; seg < iter->nr_segs; seg++) {
8578 for (i = seg + 1; i < iter->nr_segs; i++) {
8579 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008580 goto out;
8581 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008582 }
8583 retval = 0;
8584out:
8585 return retval;
8586}
Josef Bacikeb838e72012-07-31 16:28:48 -04008587
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008588static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008589{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008590 struct file *file = iocb->ki_filp;
8591 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008592 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308593 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008594 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008595 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008596 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008597 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008598 bool wakeup = true;
8599 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008600 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008601
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008602 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008603 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008604
Jens Axboefe0f07d2015-04-15 17:05:48 -06008605 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008606
Josef Bacik0e267c42013-07-02 10:38:02 -04008607 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008608 * The generic stuff only does filemap_write_and_wait_range, which
8609 * isn't enough if we've written compressed pages to this area, so
8610 * we need to flush the dirty pages again to make absolutely sure
8611 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008612 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008613 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008614 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8615 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008616 filemap_fdatawrite_range(inode->i_mapping, offset,
8617 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008618
Omar Sandoval6f673762015-03-16 04:33:52 -07008619 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008620 /*
8621 * If the write DIO is beyond the EOF, we need update
8622 * the isize, but it is protected by i_mutex. So we can
8623 * not unlock the i_mutex at this case.
8624 */
8625 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008626 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008627 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008628 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008629 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8630 ret = -EAGAIN;
8631 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008632 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008633 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8634 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008635 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008636 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008637
8638 /*
8639 * We need to know how many extents we reserved so that we can
8640 * do the accounting properly if we go over the number we
8641 * originally calculated. Abuse current->journal_info for this.
8642 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008643 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008644 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008645 dio_data.unsubmitted_oe_range_start = (u64)offset;
8646 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308647 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308648 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008649 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8650 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008651 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008652 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8653 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008654 }
8655
Omar Sandoval17f8c842015-03-16 04:33:50 -07008656 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008657 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008658 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008659 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008660 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308661 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008662 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008663 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308664 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008665 btrfs_delalloc_release_space(inode, data_reserved,
8666 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008667 /*
8668 * On error we might have left some ordered extents
8669 * without submitting corresponding bios for them, so
8670 * cleanup them up to avoid other tasks getting them
8671 * and waiting for them to complete forever.
8672 */
8673 if (dio_data.unsubmitted_oe_range_start <
8674 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008675 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008676 dio_data.unsubmitted_oe_range_start,
8677 dio_data.unsubmitted_oe_range_end -
8678 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008679 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008680 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008681 btrfs_delalloc_release_space(inode, data_reserved,
8682 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008683 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008684 }
Miao Xie38851cc2013-02-08 07:04:11 +00008685out:
Miao Xie2e60a512013-02-08 07:01:08 +00008686 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008687 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008688 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008689 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008690
Qu Wenruo364ecf32017-02-27 15:10:38 +08008691 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008692 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008693}
8694
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008695#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8696
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008697static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8698 __u64 start, __u64 len)
8699{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008700 int ret;
8701
8702 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8703 if (ret)
8704 return ret;
8705
David Sterba2135fb92017-06-23 04:09:57 +02008706 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008707}
8708
Chris Mason9ebefb182007-06-15 13:50:00 -04008709int btrfs_readpage(struct file *file, struct page *page)
8710{
Chris Masond1310b22008-01-24 16:13:08 -05008711 struct extent_io_tree *tree;
8712 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008713 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008714}
Chris Mason1832a6d2007-12-21 16:27:21 -05008715
Chris Mason39279cc2007-06-12 06:35:45 -04008716static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8717{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008718 struct inode *inode = page->mapping->host;
8719 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008720
8721 if (current->flags & PF_MEMALLOC) {
8722 redirty_page_for_writepage(wbc, page);
8723 unlock_page(page);
8724 return 0;
8725 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008726
8727 /*
8728 * If we are under memory pressure we will call this directly from the
8729 * VM, we need to make sure we have the inode referenced for the ordered
8730 * extent. If not just return like we didn't do anything.
8731 */
8732 if (!igrab(inode)) {
8733 redirty_page_for_writepage(wbc, page);
8734 return AOP_WRITEPAGE_ACTIVATE;
8735 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008736 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008737 btrfs_add_delayed_iput(inode);
8738 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008739}
Chris Mason39279cc2007-06-12 06:35:45 -04008740
Eric Sandeen48a3b632013-04-25 20:41:01 +00008741static int btrfs_writepages(struct address_space *mapping,
8742 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008743{
Chris Masond1310b22008-01-24 16:13:08 -05008744 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008745
Chris Masond1310b22008-01-24 16:13:08 -05008746 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba43317592017-06-23 03:46:07 +02008747 return extent_writepages(tree, mapping, wbc);
Chris Masonb293f022007-11-01 19:45:34 -04008748}
8749
Chris Mason3ab2fb52007-11-08 10:59:22 -05008750static int
8751btrfs_readpages(struct file *file, struct address_space *mapping,
8752 struct list_head *pages, unsigned nr_pages)
8753{
Chris Masond1310b22008-01-24 16:13:08 -05008754 struct extent_io_tree *tree;
8755 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba09325842017-06-23 04:09:57 +02008756 return extent_readpages(tree, mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008757}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008758static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008759{
Chris Masond1310b22008-01-24 16:13:08 -05008760 struct extent_io_tree *tree;
8761 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008762 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008763
Chris Masond1310b22008-01-24 16:13:08 -05008764 tree = &BTRFS_I(page->mapping->host)->io_tree;
8765 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008766 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008767 if (ret == 1) {
8768 ClearPagePrivate(page);
8769 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008770 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008771 }
8772 return ret;
8773}
Chris Mason39279cc2007-06-12 06:35:45 -04008774
Chris Masone6dcd2d2008-07-17 12:53:50 -04008775static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8776{
Chris Mason98509cf2008-09-11 15:51:43 -04008777 if (PageWriteback(page) || PageDirty(page))
8778 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008779 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008780}
8781
Lukas Czernerd47992f2013-05-21 23:17:23 -04008782static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8783 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008784{
Josef Bacik5fd02042012-05-02 14:00:54 -04008785 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008786 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008787 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008788 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008789 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008790 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308791 u64 start;
8792 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008793 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008794
Chris Mason8b62b722009-09-02 16:53:46 -04008795 /*
8796 * we have the page locked, so new writeback can't start,
8797 * and the dirty bit won't be cleared while we are here.
8798 *
8799 * Wait for IO on this page so that we can safely clear
8800 * the PagePrivate2 bit and do ordered accounting
8801 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008802 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008803
Josef Bacik5fd02042012-05-02 14:00:54 -04008804 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008805 if (offset) {
8806 btrfs_releasepage(page, GFP_NOFS);
8807 return;
8808 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008809
8810 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008811 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308812again:
8813 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008814 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308815 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008816 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308817 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008818 /*
8819 * IO on this page will never be started, so we need
8820 * to account for any ordered extents now
8821 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008822 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308823 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008824 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008825 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008826 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008827 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008828 /*
8829 * whoever cleared the private bit is responsible
8830 * for the finish_ordered_io
8831 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008832 if (TestClearPagePrivate2(page)) {
8833 struct btrfs_ordered_inode_tree *tree;
8834 u64 new_len;
8835
8836 tree = &BTRFS_I(inode)->ordered_tree;
8837
8838 spin_lock_irq(&tree->lock);
8839 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308840 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008841 if (new_len < ordered->truncated_len)
8842 ordered->truncated_len = new_len;
8843 spin_unlock_irq(&tree->lock);
8844
8845 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308846 start,
8847 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008848 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008849 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008850 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008851 if (!inode_evicting) {
8852 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308853 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008854 &cached_state);
8855 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308856
8857 start = end + 1;
8858 if (start < page_end)
8859 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008860 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008861
Qu Wenruob9d0b382015-09-29 10:35:16 +08008862 /*
8863 * Qgroup reserved space handler
8864 * Page here will be either
8865 * 1) Already written to disk
8866 * In this case, its reserved space is released from data rsv map
8867 * and will be freed by delayed_ref handler finally.
8868 * So even we call qgroup_free_data(), it won't decrease reserved
8869 * space.
8870 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008871 * This means the reserved space should be freed here. However,
8872 * if a truncate invalidates the page (by clearing PageDirty)
8873 * and the page is accounted for while allocating extent
8874 * in btrfs_check_data_free_space() we let delayed_ref to
8875 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008876 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008877 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008878 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008879 if (!inode_evicting) {
8880 clear_extent_bit(tree, page_start, page_end,
8881 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008882 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8883 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008884 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008885
8886 __btrfs_releasepage(page, GFP_NOFS);
8887 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008888
Chris Mason4a096752008-07-21 10:29:44 -04008889 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008890 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008891 ClearPagePrivate(page);
8892 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008893 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008894 }
Chris Mason39279cc2007-06-12 06:35:45 -04008895}
8896
Chris Mason9ebefb182007-06-15 13:50:00 -04008897/*
8898 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8899 * called from a page fault handler when a page is first dirtied. Hence we must
8900 * be careful to check for EOF conditions here. We set the page up correctly
8901 * for a written page which means we get ENOSPC checking when writing into
8902 * holes and correct delalloc and unwritten extent mapping on filesystems that
8903 * support these features.
8904 *
8905 * We are not allowed to take the i_mutex here so we have to play games to
8906 * protect against truncate races as the page could now be beyond EOF. Because
8907 * vmtruncate() writes the inode size before removing pages, once we have the
8908 * page lock we can determine safely if the page is beyond EOF. If it is not
8909 * beyond EOF, then the page is guaranteed safe against truncation until we
8910 * unlock the page.
8911 */
Dave Jiang11bac802017-02-24 14:56:41 -08008912int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008913{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008914 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008915 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008916 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008917 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8918 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008919 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008920 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008921 char *kaddr;
8922 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008923 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008924 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008925 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308926 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008927 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008928 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308929 u64 end;
8930
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008931 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008932
Jan Karab2b5ef52012-06-12 16:20:45 +02008933 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008934 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008935 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308936 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008937
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308938 /*
8939 * Reserving delalloc space after obtaining the page lock can lead to
8940 * deadlock. For example, if a dirty page is locked by this function
8941 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8942 * dirty page write out, then the btrfs_writepage() function could
8943 * end up waiting indefinitely to get a lock on the page currently
8944 * being processed by btrfs_page_mkwrite() function.
8945 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08008946 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308947 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008948 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08008949 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008950 reserved = 1;
8951 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008952 if (ret) {
8953 if (ret == -ENOMEM)
8954 ret = VM_FAULT_OOM;
8955 else /* -ENOSPC, -EIO, etc */
8956 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008957 if (reserved)
8958 goto out;
8959 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008960 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008961
Nick Piggin56a76f82009-03-31 15:23:23 -07008962 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008963again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008964 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008965 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008966
Chris Mason9ebefb182007-06-15 13:50:00 -04008967 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008968 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008969 /* page got truncated out from underneath us */
8970 goto out_unlock;
8971 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008972 wait_on_page_writeback(page);
8973
David Sterbaff13db42015-12-03 14:30:40 +01008974 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008975 set_page_extent_mapped(page);
8976
Chris Masoneb84ae02008-07-17 13:53:27 -04008977 /*
8978 * we can't set the delalloc bits if there are pending ordered
8979 * extents. Drop our locks and wait for them to finish
8980 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008981 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8982 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008983 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008984 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008985 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008986 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008987 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008988 btrfs_put_ordered_extent(ordered);
8989 goto again;
8990 }
8991
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008992 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008993 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008994 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008995 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308996 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008997 btrfs_delalloc_release_space(inode, data_reserved,
8998 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308999 }
9000 }
9001
Josef Bacikfbf19082009-10-01 17:10:23 -04009002 /*
Liu Bo54160342016-12-13 12:15:19 -08009003 * page_mkwrite gets called when the page is firstly dirtied after it's
9004 * faulted in, but write(2) could also dirty a page and set delalloc
9005 * bits, thus in this case for space account reason, we still need to
9006 * clear any delalloc bits within this page range since we have to
9007 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009008 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309009 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009010 EXTENT_DIRTY | EXTENT_DELALLOC |
9011 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009012 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009013
Filipe Mananae3b8a482017-11-04 00:16:59 +00009014 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009015 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009016 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009017 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009018 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009019 ret = VM_FAULT_SIGBUS;
9020 goto out_unlock;
9021 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009022 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009023
9024 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009025 if (page_start + PAGE_SIZE > size)
9026 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009027 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009028 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009029
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009030 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009031 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009032 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009033 flush_dcache_page(page);
9034 kunmap(page);
9035 }
Chris Mason247e7432008-07-17 12:53:51 -04009036 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009037 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009038 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009039
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009040 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009041 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009042 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009043
David Sterbae43bbe52017-12-12 21:43:52 +01009044 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009045
9046out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009047 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009048 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009049 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009050 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009051 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009052 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009053 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009054out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009055 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009056 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9057 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009058out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009059 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009060 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009061 return ret;
9062}
9063
Filipe Manana213e8c52018-02-06 20:40:31 +00009064static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04009065{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009066 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009067 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009068 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009069 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009070 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009071 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009072 u64 mask = fs_info->sectorsize - 1;
9073 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009074
Filipe Manana213e8c52018-02-06 20:40:31 +00009075 if (!skip_writeback) {
9076 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9077 (u64)-1);
9078 if (ret)
9079 return ret;
9080 }
Chris Mason39279cc2007-06-12 06:35:45 -04009081
Josef Bacikfcb80c22011-05-03 10:40:22 -04009082 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009083 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009084 * 3 things going on here
9085 *
9086 * 1) We need to reserve space for our orphan item and the space to
9087 * delete our orphan item. Lord knows we don't want to have a dangling
9088 * orphan item because we didn't reserve space to remove it.
9089 *
9090 * 2) We need to reserve space to update our inode.
9091 *
9092 * 3) We need to have something to cache all the space that is going to
9093 * be free'd up by the truncate operation, but also have some slack
9094 * space reserved in case it uses space during the truncate (thank you
9095 * very much snapshotting).
9096 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009097 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009098 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009099 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009100 * doesn't end up using space reserved for updating the inode or
9101 * removing the orphan item. We also need to be able to stop the
9102 * transaction and start a new one, which means we need to be able to
9103 * update the inode several times, and we have no idea of knowing how
9104 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009105 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009106 * Then there is the orphan item, which does indeed need to be held on
9107 * to for the whole operation, and we need nobody to touch this reserved
9108 * space except the orphan code.
9109 *
9110 * So that leaves us with
9111 *
9112 * 1) root->orphan_block_rsv - for the orphan deletion.
9113 * 2) rsv - for the truncate reservation, which we will steal from the
9114 * transaction reservation.
9115 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9116 * updating the inode.
9117 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009118 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009119 if (!rsv)
9120 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009121 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009122 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009123
Josef Bacik907cbce2011-08-08 13:46:15 -04009124 /*
Josef Bacik07127182011-08-19 10:29:59 -04009125 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009126 * 1 for updating the inode.
9127 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009128 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009129 if (IS_ERR(trans)) {
9130 err = PTR_ERR(trans);
9131 goto out;
9132 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009133
Josef Bacik907cbce2011-08-08 13:46:15 -04009134 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009135 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009136 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009137 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009138
Chris Mason5a3f23d2009-03-31 13:27:11 -04009139 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009140 * So if we truncate and then write and fsync we normally would just
9141 * write the extents that changed, which is a problem if we need to
9142 * first truncate that entire inode. So set this flag so we write out
9143 * all of the extents in the inode to the sync log so we're completely
9144 * safe.
9145 */
9146 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009147 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009148
Yan, Zheng80825102009-11-12 09:35:36 +00009149 while (1) {
9150 ret = btrfs_truncate_inode_items(trans, root, inode,
9151 inode->i_size,
9152 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009153 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009154 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009155 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009156 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009157 }
Chris Mason39279cc2007-06-12 06:35:45 -04009158
Yan, Zheng80825102009-11-12 09:35:36 +00009159 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009160 if (ret) {
9161 err = ret;
9162 break;
9163 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009164
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009165 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009166 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009167
9168 trans = btrfs_start_transaction(root, 2);
9169 if (IS_ERR(trans)) {
9170 ret = err = PTR_ERR(trans);
9171 trans = NULL;
9172 break;
9173 }
9174
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009175 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009176 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009177 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009178 BUG_ON(ret); /* shouldn't happen */
9179 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009180 }
9181
Josef Bacikddfae632017-10-19 14:16:02 -04009182 /*
9183 * We can't call btrfs_truncate_block inside a trans handle as we could
9184 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9185 * we've truncated everything except the last little bit, and can do
9186 * btrfs_truncate_block and then update the disk_i_size.
9187 */
9188 if (ret == NEED_TRUNCATE_BLOCK) {
9189 btrfs_end_transaction(trans);
9190 btrfs_btree_balance_dirty(fs_info);
9191
9192 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9193 if (ret)
9194 goto out;
9195 trans = btrfs_start_transaction(root, 1);
9196 if (IS_ERR(trans)) {
9197 ret = PTR_ERR(trans);
9198 goto out;
9199 }
9200 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9201 }
9202
Yan, Zheng80825102009-11-12 09:35:36 +00009203 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009204 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009205 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009206 if (ret)
9207 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009208 }
9209
Chris Mason917c16b2011-11-08 14:49:59 -05009210 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009211 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009212 ret = btrfs_update_inode(trans, root, inode);
9213 if (ret && !err)
9214 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009215
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009216 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009217 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009218 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009219out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009220 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009221
Josef Bacik3893e332011-01-31 16:03:11 -05009222 if (ret && !err)
9223 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009224
Josef Bacik3893e332011-01-31 16:03:11 -05009225 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009226}
9227
Sven Wegener3b963622008-06-09 21:57:42 -04009228/*
Chris Masond352ac62008-09-29 15:18:18 -04009229 * create a new subvolume directory/inode (helper for the ioctl).
9230 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009231int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009232 struct btrfs_root *new_root,
9233 struct btrfs_root *parent_root,
9234 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009235{
Chris Mason39279cc2007-06-12 06:35:45 -04009236 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009237 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009238 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009239
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009240 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9241 new_dirid, new_dirid,
9242 S_IFDIR | (~current_umask() & S_IRWXUGO),
9243 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009244 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009245 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009246 inode->i_op = &btrfs_dir_inode_operations;
9247 inode->i_fop = &btrfs_dir_file_operations;
9248
Miklos Szeredibfe86842011-10-28 14:13:29 +02009249 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009250 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009251 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009252
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009253 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9254 if (err)
9255 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009256 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009257 new_root->root_key.objectid, err);
9258
Yan, Zheng76dda932009-09-21 16:00:26 -04009259 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009260
Yan, Zheng76dda932009-09-21 16:00:26 -04009261 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009262 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009263}
9264
Chris Mason39279cc2007-06-12 06:35:45 -04009265struct inode *btrfs_alloc_inode(struct super_block *sb)
9266{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009267 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009268 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009269 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009270
David Sterba712e36c2017-10-31 17:08:27 +01009271 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009272 if (!ei)
9273 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009274
9275 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009276 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009277 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009278 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009279 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009280 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009281 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009282 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009283 ei->disk_i_size = 0;
9284 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009285 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009286 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009287 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009288 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009289 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009290
Josef Bacik9e0baf62011-07-15 15:16:44 +00009291 spin_lock_init(&ei->lock);
9292 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009293 if (sb->s_magic != BTRFS_TEST_MAGIC)
9294 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9295 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009296 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009297 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009298 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009299
Miao Xie16cdcec2011-04-22 18:12:22 +08009300 ei->delayed_node = NULL;
9301
chandan r9cc97d62012-07-04 12:48:07 +05309302 ei->i_otime.tv_sec = 0;
9303 ei->i_otime.tv_nsec = 0;
9304
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009305 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009306 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009307 extent_io_tree_init(&ei->io_tree, inode);
9308 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009309 ei->io_tree.track_uptodate = 1;
9310 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009311 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009312 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009313 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009314 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009315 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009316 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009317 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009318 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009319
9320 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009321}
9322
Josef Bacikaaedb552013-10-11 14:44:09 -04009323#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9324void btrfs_test_destroy_inode(struct inode *inode)
9325{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009326 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009327 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9328}
9329#endif
9330
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009331static void btrfs_i_callback(struct rcu_head *head)
9332{
9333 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009334 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9335}
9336
Chris Mason39279cc2007-06-12 06:35:45 -04009337void btrfs_destroy_inode(struct inode *inode)
9338{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009339 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009340 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009341 struct btrfs_root *root = BTRFS_I(inode)->root;
9342
Al Virob3d9b7a2012-06-09 13:51:19 -04009343 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009344 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009345 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9346 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009347 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009348 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009349 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009350 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009351 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009352
Chris Mason5a3f23d2009-03-31 13:27:11 -04009353 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009354 * This can happen where we create an inode, but somebody else also
9355 * created the same inode and we need to destroy the one we already
9356 * created.
9357 */
9358 if (!root)
9359 goto free;
9360
Josef Bacik8a35d952012-05-23 14:26:42 -04009361 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9362 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009363 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009364 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009365 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009366 }
Josef Bacik7b128762008-07-24 12:17:14 -04009367
Chris Masond3977122009-01-05 21:25:51 -05009368 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009369 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9370 if (!ordered)
9371 break;
9372 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009373 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009374 "found ordered extent %llu %llu on inode cleanup",
9375 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009376 btrfs_remove_ordered_extent(inode, ordered);
9377 btrfs_put_ordered_extent(ordered);
9378 btrfs_put_ordered_extent(ordered);
9379 }
9380 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009381 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009382 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009383 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009384free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009385 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009386}
9387
Al Viro45321ac2010-06-07 13:43:19 -04009388int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009389{
9390 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009391
Naohiro Aota6379ef92013-06-06 09:56:34 +00009392 if (root == NULL)
9393 return 1;
9394
Liu Bofa6ac872013-02-20 14:10:23 +00009395 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009396 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009397 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009398 else
Al Viro45321ac2010-06-07 13:43:19 -04009399 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009400}
9401
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009402static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009403{
9404 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9405
9406 inode_init_once(&ei->vfs_inode);
9407}
9408
David Sterbae67c7182018-02-19 17:24:18 +01009409void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009410{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009411 /*
9412 * Make sure all delayed rcu free inodes are flushed before we
9413 * destroy cache.
9414 */
9415 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009416 kmem_cache_destroy(btrfs_inode_cachep);
9417 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009418 kmem_cache_destroy(btrfs_path_cachep);
9419 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009420}
9421
Liu Bof5c29bd2017-11-02 17:21:50 -06009422int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009423{
David Sterba837e1972012-09-07 03:00:48 -06009424 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009425 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009426 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9427 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009428 if (!btrfs_inode_cachep)
9429 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009430
David Sterba837e1972012-09-07 03:00:48 -06009431 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009432 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009433 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009434 if (!btrfs_trans_handle_cachep)
9435 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009436
David Sterba837e1972012-09-07 03:00:48 -06009437 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009438 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009439 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009440 if (!btrfs_path_cachep)
9441 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009442
David Sterba837e1972012-09-07 03:00:48 -06009443 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009444 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009445 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009446 if (!btrfs_free_space_cachep)
9447 goto fail;
9448
Chris Mason39279cc2007-06-12 06:35:45 -04009449 return 0;
9450fail:
9451 btrfs_destroy_cachep();
9452 return -ENOMEM;
9453}
9454
David Howellsa528d352017-01-31 16:46:22 +00009455static int btrfs_getattr(const struct path *path, struct kstat *stat,
9456 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009457{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009458 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009459 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009460 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009461 u32 bi_flags = BTRFS_I(inode)->flags;
9462
9463 stat->result_mask |= STATX_BTIME;
9464 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9465 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9466 if (bi_flags & BTRFS_INODE_APPEND)
9467 stat->attributes |= STATX_ATTR_APPEND;
9468 if (bi_flags & BTRFS_INODE_COMPRESS)
9469 stat->attributes |= STATX_ATTR_COMPRESSED;
9470 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9471 stat->attributes |= STATX_ATTR_IMMUTABLE;
9472 if (bi_flags & BTRFS_INODE_NODUMP)
9473 stat->attributes |= STATX_ATTR_NODUMP;
9474
9475 stat->attributes_mask |= (STATX_ATTR_APPEND |
9476 STATX_ATTR_COMPRESSED |
9477 STATX_ATTR_IMMUTABLE |
9478 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009479
Chris Mason39279cc2007-06-12 06:35:45 -04009480 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009481 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009482
9483 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009484 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009485 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009486 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009487 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009488 return 0;
9489}
9490
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009491static int btrfs_rename_exchange(struct inode *old_dir,
9492 struct dentry *old_dentry,
9493 struct inode *new_dir,
9494 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009495{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009496 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009497 struct btrfs_trans_handle *trans;
9498 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009499 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009500 struct inode *new_inode = new_dentry->d_inode;
9501 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009502 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009503 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009504 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9505 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009506 u64 old_idx = 0;
9507 u64 new_idx = 0;
9508 u64 root_objectid;
9509 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009510 bool root_log_pinned = false;
9511 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009512
9513 /* we only allow rename subvolume link between subvolumes */
9514 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9515 return -EXDEV;
9516
9517 /* close the race window with snapshot create/destroy ioctl */
9518 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009519 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009520 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009521 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009522
9523 /*
9524 * We want to reserve the absolute worst case amount of items. So if
9525 * both inodes are subvols and we need to unlink them then that would
9526 * require 4 item modifications, but if they are both normal inodes it
9527 * would require 5 item modifications, so we'll assume their normal
9528 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9529 * should cover the worst case number of items we'll modify.
9530 */
9531 trans = btrfs_start_transaction(root, 12);
9532 if (IS_ERR(trans)) {
9533 ret = PTR_ERR(trans);
9534 goto out_notrans;
9535 }
9536
9537 /*
9538 * We need to find a free sequence number both in the source and
9539 * in the destination directory for the exchange.
9540 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009541 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009542 if (ret)
9543 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009544 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009545 if (ret)
9546 goto out_fail;
9547
9548 BTRFS_I(old_inode)->dir_index = 0ULL;
9549 BTRFS_I(new_inode)->dir_index = 0ULL;
9550
9551 /* Reference for the source. */
9552 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9553 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009554 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009555 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009556 btrfs_pin_log_trans(root);
9557 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009558 ret = btrfs_insert_inode_ref(trans, dest,
9559 new_dentry->d_name.name,
9560 new_dentry->d_name.len,
9561 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009562 btrfs_ino(BTRFS_I(new_dir)),
9563 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009564 if (ret)
9565 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009566 }
9567
9568 /* And now for the dest. */
9569 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9570 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009571 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009572 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009573 btrfs_pin_log_trans(dest);
9574 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009575 ret = btrfs_insert_inode_ref(trans, root,
9576 old_dentry->d_name.name,
9577 old_dentry->d_name.len,
9578 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009579 btrfs_ino(BTRFS_I(old_dir)),
9580 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009581 if (ret)
9582 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009583 }
9584
9585 /* Update inode version and ctime/mtime. */
9586 inode_inc_iversion(old_dir);
9587 inode_inc_iversion(new_dir);
9588 inode_inc_iversion(old_inode);
9589 inode_inc_iversion(new_inode);
9590 old_dir->i_ctime = old_dir->i_mtime = ctime;
9591 new_dir->i_ctime = new_dir->i_mtime = ctime;
9592 old_inode->i_ctime = ctime;
9593 new_inode->i_ctime = ctime;
9594
9595 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009596 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9597 BTRFS_I(old_inode), 1);
9598 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9599 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009600 }
9601
9602 /* src is a subvolume */
9603 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9604 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9605 ret = btrfs_unlink_subvol(trans, root, old_dir,
9606 root_objectid,
9607 old_dentry->d_name.name,
9608 old_dentry->d_name.len);
9609 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009610 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9611 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009612 old_dentry->d_name.name,
9613 old_dentry->d_name.len);
9614 if (!ret)
9615 ret = btrfs_update_inode(trans, root, old_inode);
9616 }
9617 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009618 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009619 goto out_fail;
9620 }
9621
9622 /* dest is a subvolume */
9623 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9624 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9625 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9626 root_objectid,
9627 new_dentry->d_name.name,
9628 new_dentry->d_name.len);
9629 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009630 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9631 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009632 new_dentry->d_name.name,
9633 new_dentry->d_name.len);
9634 if (!ret)
9635 ret = btrfs_update_inode(trans, dest, new_inode);
9636 }
9637 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009638 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009639 goto out_fail;
9640 }
9641
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009642 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009643 new_dentry->d_name.name,
9644 new_dentry->d_name.len, 0, old_idx);
9645 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009646 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009647 goto out_fail;
9648 }
9649
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009650 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009651 old_dentry->d_name.name,
9652 old_dentry->d_name.len, 0, new_idx);
9653 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009654 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009655 goto out_fail;
9656 }
9657
9658 if (old_inode->i_nlink == 1)
9659 BTRFS_I(old_inode)->dir_index = old_idx;
9660 if (new_inode->i_nlink == 1)
9661 BTRFS_I(new_inode)->dir_index = new_idx;
9662
Filipe Manana86e8aa02016-05-05 02:02:27 +01009663 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009665 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9666 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009667 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009668 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009669 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009670 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009671 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009672 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9673 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009674 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009675 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009676 }
9677out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009678 /*
9679 * If we have pinned a log and an error happened, we unpin tasks
9680 * trying to sync the log and force them to fallback to a transaction
9681 * commit if the log currently contains any of the inodes involved in
9682 * this rename operation (to ensure we do not persist a log with an
9683 * inconsistent state for any of these inodes or leading to any
9684 * inconsistencies when replayed). If the transaction was aborted, the
9685 * abortion reason is propagated to userspace when attempting to commit
9686 * the transaction. If the log does not contain any of these inodes, we
9687 * allow the tasks to sync it.
9688 */
9689 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009690 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9691 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9692 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009693 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009694 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009695 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009696
9697 if (root_log_pinned) {
9698 btrfs_end_log_trans(root);
9699 root_log_pinned = false;
9700 }
9701 if (dest_log_pinned) {
9702 btrfs_end_log_trans(dest);
9703 dest_log_pinned = false;
9704 }
9705 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009706 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009707out_notrans:
9708 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009709 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009710 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009711 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009712
9713 return ret;
9714}
9715
9716static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9717 struct btrfs_root *root,
9718 struct inode *dir,
9719 struct dentry *dentry)
9720{
9721 int ret;
9722 struct inode *inode;
9723 u64 objectid;
9724 u64 index;
9725
9726 ret = btrfs_find_free_ino(root, &objectid);
9727 if (ret)
9728 return ret;
9729
9730 inode = btrfs_new_inode(trans, root, dir,
9731 dentry->d_name.name,
9732 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009733 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009734 objectid,
9735 S_IFCHR | WHITEOUT_MODE,
9736 &index);
9737
9738 if (IS_ERR(inode)) {
9739 ret = PTR_ERR(inode);
9740 return ret;
9741 }
9742
9743 inode->i_op = &btrfs_special_inode_operations;
9744 init_special_inode(inode, inode->i_mode,
9745 WHITEOUT_DEV);
9746
9747 ret = btrfs_init_inode_security(trans, inode, dir,
9748 &dentry->d_name);
9749 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009750 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009751
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009752 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9753 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009754 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009755 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009756
9757 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009758out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009759 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009760 if (ret)
9761 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009762 iput(inode);
9763
Filipe Mananac9901612016-05-05 01:41:57 +01009764 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009765}
9766
Chris Mason39279cc2007-06-12 06:35:45 -04009767static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009768 struct inode *new_dir, struct dentry *new_dentry,
9769 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009770{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009771 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009772 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009773 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009774 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9775 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009776 struct inode *new_inode = d_inode(new_dentry);
9777 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009778 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009779 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009780 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009781 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009782 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009783
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009784 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009785 return -EPERM;
9786
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009787 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009788 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009789 return -EXDEV;
9790
Li Zefan33345d012011-04-20 10:31:50 +08009791 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009792 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009793 return -ENOTEMPTY;
9794
Chris Mason39279cc2007-06-12 06:35:45 -04009795 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009796 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009797 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009798
9799
9800 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009801 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009802 new_dentry->d_name.name,
9803 new_dentry->d_name.len);
9804
9805 if (ret) {
9806 if (ret == -EEXIST) {
9807 /* we shouldn't get
9808 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309809 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009810 return ret;
9811 }
9812 } else {
9813 /* maybe -EOVERFLOW */
9814 return ret;
9815 }
9816 }
9817 ret = 0;
9818
Chris Mason5a3f23d2009-03-31 13:27:11 -04009819 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009820 * we're using rename to replace one file with another. Start IO on it
9821 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009822 */
Chris Mason8d875f92014-08-12 10:47:42 -07009823 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009824 filemap_flush(old_inode->i_mapping);
9825
Yan, Zheng76dda932009-09-21 16:00:26 -04009826 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009827 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009828 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009829 /*
9830 * We want to reserve the absolute worst case amount of items. So if
9831 * both inodes are subvols and we need to unlink them then that would
9832 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009833 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009834 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9835 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009836 * If our rename has the whiteout flag, we need more 5 units for the
9837 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9838 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009839 */
Filipe Manana5062af32016-05-05 10:26:26 +01009840 trans_num_items = 11;
9841 if (flags & RENAME_WHITEOUT)
9842 trans_num_items += 5;
9843 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009844 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009845 ret = PTR_ERR(trans);
9846 goto out_notrans;
9847 }
Chris Mason5f39d392007-10-15 16:14:19 -04009848
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009849 if (dest != root)
9850 btrfs_record_root_in_trans(trans, dest);
9851
Nikolay Borisov877574e2017-02-20 13:50:33 +02009852 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009853 if (ret)
9854 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009855
Miao Xie67de1172013-12-26 13:07:06 +08009856 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009857 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009858 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009859 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009860 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009861 btrfs_pin_log_trans(root);
9862 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009863 ret = btrfs_insert_inode_ref(trans, dest,
9864 new_dentry->d_name.name,
9865 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009866 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009867 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009868 if (ret)
9869 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009870 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009871
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009872 inode_inc_iversion(old_dir);
9873 inode_inc_iversion(new_dir);
9874 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009875 old_dir->i_ctime = old_dir->i_mtime =
9876 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009877 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009878
Chris Mason12fcfd22009-03-24 10:24:20 -04009879 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009880 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9881 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009882
Li Zefan33345d012011-04-20 10:31:50 +08009883 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009884 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9885 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9886 old_dentry->d_name.name,
9887 old_dentry->d_name.len);
9888 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009889 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9890 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009891 old_dentry->d_name.name,
9892 old_dentry->d_name.len);
9893 if (!ret)
9894 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009895 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009896 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009897 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009898 goto out_fail;
9899 }
Chris Mason39279cc2007-06-12 06:35:45 -04009900
9901 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009902 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009903 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009904 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009905 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9906 root_objectid = BTRFS_I(new_inode)->location.objectid;
9907 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9908 root_objectid,
9909 new_dentry->d_name.name,
9910 new_dentry->d_name.len);
9911 BUG_ON(new_inode->i_nlink == 0);
9912 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009913 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9914 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009915 new_dentry->d_name.name,
9916 new_dentry->d_name.len);
9917 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009918 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009919 ret = btrfs_orphan_add(trans,
9920 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009921 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009922 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009923 goto out_fail;
9924 }
Chris Mason39279cc2007-06-12 06:35:45 -04009925 }
Josef Bacikaec74772008-07-24 12:12:38 -04009926
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009927 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009928 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009929 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009930 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009931 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009932 goto out_fail;
9933 }
Chris Mason39279cc2007-06-12 06:35:45 -04009934
Miao Xie67de1172013-12-26 13:07:06 +08009935 if (old_inode->i_nlink == 1)
9936 BTRFS_I(old_inode)->dir_index = index;
9937
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009938 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009939 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009940
David Sterbaf85b7372017-01-20 14:54:07 +01009941 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9942 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009943 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009944 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009945 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009946
9947 if (flags & RENAME_WHITEOUT) {
9948 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9949 old_dentry);
9950
9951 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009952 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009953 goto out_fail;
9954 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009955 }
Chris Mason39279cc2007-06-12 06:35:45 -04009956out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009957 /*
9958 * If we have pinned the log and an error happened, we unpin tasks
9959 * trying to sync the log and force them to fallback to a transaction
9960 * commit if the log currently contains any of the inodes involved in
9961 * this rename operation (to ensure we do not persist a log with an
9962 * inconsistent state for any of these inodes or leading to any
9963 * inconsistencies when replayed). If the transaction was aborted, the
9964 * abortion reason is propagated to userspace when attempting to commit
9965 * the transaction. If the log does not contain any of these inodes, we
9966 * allow the tasks to sync it.
9967 */
9968 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009969 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9970 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9971 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009972 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009973 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009974 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009975
9976 btrfs_end_log_trans(root);
9977 log_pinned = false;
9978 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009979 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009980out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009981 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009982 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009983
Chris Mason39279cc2007-06-12 06:35:45 -04009984 return ret;
9985}
9986
Miklos Szeredi80ace852014-07-23 15:15:32 +02009987static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9988 struct inode *new_dir, struct dentry *new_dentry,
9989 unsigned int flags)
9990{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009991 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009992 return -EINVAL;
9993
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009994 if (flags & RENAME_EXCHANGE)
9995 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9996 new_dentry);
9997
9998 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009999}
10000
Miao Xie8ccf6f192012-10-25 09:28:04 +000010001static void btrfs_run_delalloc_work(struct btrfs_work *work)
10002{
10003 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010004 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010005
10006 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10007 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010008 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010009 filemap_flush(inode->i_mapping);
10010 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10011 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010012 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010013
10014 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010015 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010016 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010017 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010018 complete(&delalloc_work->completion);
10019}
10020
10021struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010022 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010023{
10024 struct btrfs_delalloc_work *work;
10025
David Sterba100d5702015-12-08 14:39:32 +010010026 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010027 if (!work)
10028 return NULL;
10029
10030 init_completion(&work->completion);
10031 INIT_LIST_HEAD(&work->list);
10032 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010033 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010034 WARN_ON_ONCE(!inode);
10035 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10036 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010037
10038 return work;
10039}
10040
10041void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10042{
10043 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010044 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010045}
10046
Chris Masond352ac62008-09-29 15:18:18 -040010047/*
10048 * some fairly slow code that needs optimization. This walks the list
10049 * of all the inodes with pending delalloc and forces them to disk.
10050 */
Miao Xie6c255e62014-03-06 13:55:01 +080010051static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10052 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010053{
Chris Masonea8c2812008-08-04 23:17:27 -040010054 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010055 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010056 struct btrfs_delalloc_work *work, *next;
10057 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010058 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010059 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010060
Miao Xie8ccf6f192012-10-25 09:28:04 +000010061 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010062 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010063
Miao Xie573bfb72014-03-06 13:55:03 +080010064 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010065 spin_lock(&root->delalloc_lock);
10066 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010067 while (!list_empty(&splice)) {
10068 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010069 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010070
Miao Xieeb73c1b2013-05-15 07:48:22 +000010071 list_move_tail(&binode->delalloc_inodes,
10072 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010073 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010074 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010075 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010076 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010077 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010078 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010079
David Sterba651d4942015-11-27 19:24:16 +010010080 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010081 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010082 if (delay_iput)
10083 btrfs_add_delayed_iput(inode);
10084 else
10085 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010086 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010087 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010088 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010089 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010090 btrfs_queue_work(root->fs_info->flush_workers,
10091 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010092 ret++;
10093 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010094 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010095 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010096 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010097 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010098 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010099
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010100out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010101 list_for_each_entry_safe(work, next, &works, list) {
10102 list_del_init(&work->list);
10103 btrfs_wait_and_free_delalloc_work(work);
10104 }
10105
Miao Xieeb73c1b2013-05-15 07:48:22 +000010106 if (!list_empty_careful(&splice)) {
10107 spin_lock(&root->delalloc_lock);
10108 list_splice_tail(&splice, &root->delalloc_inodes);
10109 spin_unlock(&root->delalloc_lock);
10110 }
Miao Xie573bfb72014-03-06 13:55:03 +080010111 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010112 return ret;
10113}
10114
10115int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10116{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010117 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010118 int ret;
10119
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010120 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010121 return -EROFS;
10122
Miao Xie6c255e62014-03-06 13:55:01 +080010123 ret = __start_delalloc_inodes(root, delay_iput, -1);
10124 if (ret > 0)
10125 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010126 return ret;
10127}
10128
Miao Xie6c255e62014-03-06 13:55:01 +080010129int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10130 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010131{
10132 struct btrfs_root *root;
10133 struct list_head splice;
10134 int ret;
10135
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010136 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010137 return -EROFS;
10138
10139 INIT_LIST_HEAD(&splice);
10140
Miao Xie573bfb72014-03-06 13:55:03 +080010141 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010142 spin_lock(&fs_info->delalloc_root_lock);
10143 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010144 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010145 root = list_first_entry(&splice, struct btrfs_root,
10146 delalloc_root);
10147 root = btrfs_grab_fs_root(root);
10148 BUG_ON(!root);
10149 list_move_tail(&root->delalloc_root,
10150 &fs_info->delalloc_roots);
10151 spin_unlock(&fs_info->delalloc_root_lock);
10152
Miao Xie6c255e62014-03-06 13:55:01 +080010153 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010154 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010155 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010156 goto out;
10157
Miao Xie6c255e62014-03-06 13:55:01 +080010158 if (nr != -1) {
10159 nr -= ret;
10160 WARN_ON(nr < 0);
10161 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010162 spin_lock(&fs_info->delalloc_root_lock);
10163 }
10164 spin_unlock(&fs_info->delalloc_root_lock);
10165
Miao Xie6c255e62014-03-06 13:55:01 +080010166 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010167out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010168 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010169 spin_lock(&fs_info->delalloc_root_lock);
10170 list_splice_tail(&splice, &fs_info->delalloc_roots);
10171 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010172 }
Miao Xie573bfb72014-03-06 13:55:03 +080010173 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010174 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010175}
10176
Chris Mason39279cc2007-06-12 06:35:45 -040010177static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10178 const char *symname)
10179{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010180 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010181 struct btrfs_trans_handle *trans;
10182 struct btrfs_root *root = BTRFS_I(dir)->root;
10183 struct btrfs_path *path;
10184 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010185 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010186 int err;
10187 int drop_inode = 0;
10188 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010189 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010190 int name_len;
10191 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010192 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010193 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010194 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010195
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010196 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010197 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010198 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010199
Josef Bacik9ed74f22009-09-11 16:12:44 -040010200 /*
10201 * 2 items for inode item and ref
10202 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010203 * 1 item for updating parent inode item
10204 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010205 * 1 item for xattr if selinux is on
10206 */
Filipe Manana9269d122015-12-31 18:16:29 +000010207 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010208 if (IS_ERR(trans))
10209 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010210
Li Zefan581bb052011-04-20 10:06:11 +080010211 err = btrfs_find_free_ino(root, &objectid);
10212 if (err)
10213 goto out_unlock;
10214
Josef Bacikaec74772008-07-24 12:12:38 -040010215 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010216 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10217 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010218 if (IS_ERR(inode)) {
10219 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010220 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010221 }
Chris Mason39279cc2007-06-12 06:35:45 -040010222
Casey Schauflerad19db72011-12-15 10:09:07 -050010223 /*
10224 * If the active LSM wants to access the inode during
10225 * d_instantiate it needs these. Smack checks to see
10226 * if the filesystem supports xattrs by looking at the
10227 * ops vector.
10228 */
10229 inode->i_fop = &btrfs_file_operations;
10230 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010231 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010232 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10233
10234 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10235 if (err)
10236 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010237
Chris Mason39279cc2007-06-12 06:35:45 -040010238 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010239 if (!path) {
10240 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010241 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010242 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010243 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010244 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010245 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010246 datasize = btrfs_file_extent_calc_inline_size(name_len);
10247 err = btrfs_insert_empty_item(trans, root, path, &key,
10248 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010249 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010250 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010251 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010252 }
Chris Mason5f39d392007-10-15 16:14:19 -040010253 leaf = path->nodes[0];
10254 ei = btrfs_item_ptr(leaf, path->slots[0],
10255 struct btrfs_file_extent_item);
10256 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10257 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010258 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010259 btrfs_set_file_extent_encryption(leaf, ei, 0);
10260 btrfs_set_file_extent_compression(leaf, ei, 0);
10261 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10262 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10263
Chris Mason39279cc2007-06-12 06:35:45 -040010264 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010265 write_extent_buffer(leaf, symname, ptr, name_len);
10266 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010267 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010268
Chris Mason39279cc2007-06-12 06:35:45 -040010269 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010270 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010271 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010272 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010273 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010274 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010275 /*
10276 * Last step, add directory indexes for our symlink inode. This is the
10277 * last step to avoid extra cleanup of these indexes if an error happens
10278 * elsewhere above.
10279 */
10280 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010281 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10282 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010283 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010284 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010285 goto out_unlock_inode;
10286 }
10287
10288 unlock_new_inode(inode);
10289 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010290
10291out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010292 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010293 if (drop_inode) {
10294 inode_dec_link_count(inode);
10295 iput(inode);
10296 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010297 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010298 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010299
10300out_unlock_inode:
10301 drop_inode = 1;
10302 unlock_new_inode(inode);
10303 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010304}
Chris Mason16432982008-04-10 10:23:21 -040010305
Josef Bacik0af3d002010-06-21 14:48:16 -040010306static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10307 u64 start, u64 num_bytes, u64 min_size,
10308 loff_t actual_len, u64 *alloc_hint,
10309 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010310{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010311 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010312 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10313 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010314 struct btrfs_root *root = BTRFS_I(inode)->root;
10315 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010316 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010317 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010318 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010319 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010320 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010321 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010322 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010323
Josef Bacik0af3d002010-06-21 14:48:16 -040010324 if (trans)
10325 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010326 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010327 if (own_trans) {
10328 trans = btrfs_start_transaction(root, 3);
10329 if (IS_ERR(trans)) {
10330 ret = PTR_ERR(trans);
10331 break;
10332 }
Yan Zhengd899e052008-10-30 14:25:28 -040010333 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010334
Byongho Leeee221842015-12-15 01:42:10 +090010335 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010336 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010337 /*
10338 * If we are severely fragmented we could end up with really
10339 * small allocations, so if the allocator is returning small
10340 * chunks lets make its job easier by only searching for those
10341 * sized chunks.
10342 */
10343 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010344 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10345 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010346 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010347 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010348 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010349 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010350 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010351 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010352
Josef Bacik0b670dc2015-09-23 17:11:16 -040010353 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010354 ret = insert_reserved_file_extent(trans, inode,
10355 cur_offset, ins.objectid,
10356 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010357 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010358 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010359 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010360 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010361 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010362 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010363 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010364 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010365 break;
10366 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010367
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010368 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010369 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010370
Josef Bacik5dc562c2012-08-17 13:14:17 -040010371 em = alloc_extent_map();
10372 if (!em) {
10373 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10374 &BTRFS_I(inode)->runtime_flags);
10375 goto next;
10376 }
10377
10378 em->start = cur_offset;
10379 em->orig_start = cur_offset;
10380 em->len = ins.offset;
10381 em->block_start = ins.objectid;
10382 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010383 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010384 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010385 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010386 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10387 em->generation = trans->transid;
10388
10389 while (1) {
10390 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010391 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010392 write_unlock(&em_tree->lock);
10393 if (ret != -EEXIST)
10394 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010395 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010396 cur_offset + ins.offset - 1,
10397 0);
10398 }
10399 free_extent_map(em);
10400next:
Yan Zhengd899e052008-10-30 14:25:28 -040010401 num_bytes -= ins.offset;
10402 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010403 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010404
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010405 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010406 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010407 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010408 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010409 (actual_len > inode->i_size) &&
10410 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010411 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010412 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010413 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010414 i_size = cur_offset;
10415 i_size_write(inode, i_size);
10416 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010417 }
10418
Yan Zhengd899e052008-10-30 14:25:28 -040010419 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010420
10421 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010422 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010423 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010424 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010425 break;
10426 }
Yan Zhengd899e052008-10-30 14:25:28 -040010427
Josef Bacik0af3d002010-06-21 14:48:16 -040010428 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010429 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010430 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010431 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010432 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010433 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010434 return ret;
10435}
10436
Josef Bacik0af3d002010-06-21 14:48:16 -040010437int btrfs_prealloc_file_range(struct inode *inode, int mode,
10438 u64 start, u64 num_bytes, u64 min_size,
10439 loff_t actual_len, u64 *alloc_hint)
10440{
10441 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10442 min_size, actual_len, alloc_hint,
10443 NULL);
10444}
10445
10446int btrfs_prealloc_file_range_trans(struct inode *inode,
10447 struct btrfs_trans_handle *trans, int mode,
10448 u64 start, u64 num_bytes, u64 min_size,
10449 loff_t actual_len, u64 *alloc_hint)
10450{
10451 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10452 min_size, actual_len, alloc_hint, trans);
10453}
10454
Chris Masone6dcd2d2008-07-17 12:53:50 -040010455static int btrfs_set_page_dirty(struct page *page)
10456{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010457 return __set_page_dirty_nobuffers(page);
10458}
10459
Al Viro10556cb2011-06-20 19:28:19 -040010460static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010461{
Li Zefanb83cc962010-12-20 16:04:08 +080010462 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010463 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010464
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010465 if (mask & MAY_WRITE &&
10466 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10467 if (btrfs_root_readonly(root))
10468 return -EROFS;
10469 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10470 return -EACCES;
10471 }
Al Viro2830ba72011-06-20 19:16:29 -040010472 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010473}
Chris Mason39279cc2007-06-12 06:35:45 -040010474
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010475static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10476{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010477 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010478 struct btrfs_trans_handle *trans;
10479 struct btrfs_root *root = BTRFS_I(dir)->root;
10480 struct inode *inode = NULL;
10481 u64 objectid;
10482 u64 index;
10483 int ret = 0;
10484
10485 /*
10486 * 5 units required for adding orphan entry
10487 */
10488 trans = btrfs_start_transaction(root, 5);
10489 if (IS_ERR(trans))
10490 return PTR_ERR(trans);
10491
10492 ret = btrfs_find_free_ino(root, &objectid);
10493 if (ret)
10494 goto out;
10495
10496 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010497 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010498 if (IS_ERR(inode)) {
10499 ret = PTR_ERR(inode);
10500 inode = NULL;
10501 goto out;
10502 }
10503
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010504 inode->i_fop = &btrfs_file_operations;
10505 inode->i_op = &btrfs_file_inode_operations;
10506
10507 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010508 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10509
Chris Masonb0d5d102014-09-08 13:08:51 -070010510 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10511 if (ret)
10512 goto out_inode;
10513
10514 ret = btrfs_update_inode(trans, root, inode);
10515 if (ret)
10516 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010517 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010518 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010519 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010520
Filipe Manana5762b5c2014-08-01 00:10:32 +010010521 /*
10522 * We set number of links to 0 in btrfs_new_inode(), and here we set
10523 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10524 * through:
10525 *
10526 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10527 */
10528 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010529 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010530 d_tmpfile(dentry, inode);
10531 mark_inode_dirty(inode);
10532
10533out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010534 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010535 if (ret)
10536 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010537 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010538 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010539
10540out_inode:
10541 unlock_new_inode(inode);
10542 goto out;
10543
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010544}
10545
David Sterba20a7db82017-02-17 16:24:29 +010010546__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010547static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010548{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010549 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010550}
10551
Josef Bacikc6100a42017-05-05 11:57:13 -040010552static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10553{
10554 struct inode *inode = private_data;
10555 return btrfs_sb(inode->i_sb);
10556}
10557
10558static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10559 u64 start, u64 end)
10560{
10561 struct inode *inode = private_data;
10562 u64 isize;
10563
10564 isize = i_size_read(inode);
10565 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10566 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10567 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10568 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10569 }
10570}
10571
10572void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10573{
10574 struct inode *inode = private_data;
10575 unsigned long index = start >> PAGE_SHIFT;
10576 unsigned long end_index = end >> PAGE_SHIFT;
10577 struct page *page;
10578
10579 while (index <= end_index) {
10580 page = find_get_page(inode->i_mapping, index);
10581 ASSERT(page); /* Pages should be in the extent_io_tree */
10582 set_page_writeback(page);
10583 put_page(page);
10584 index++;
10585 }
10586}
10587
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010588static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010589 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010590 .lookup = btrfs_lookup,
10591 .create = btrfs_create,
10592 .unlink = btrfs_unlink,
10593 .link = btrfs_link,
10594 .mkdir = btrfs_mkdir,
10595 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010596 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010597 .symlink = btrfs_symlink,
10598 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010599 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010600 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010601 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010602 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010603 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010604 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010605 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010606};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010607static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010608 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010609 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010610 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010611};
Yan, Zheng76dda932009-09-21 16:00:26 -040010612
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010613static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010614 .llseek = generic_file_llseek,
10615 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010616 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010617 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010618 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010619#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010620 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010621#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010622 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010623 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010624};
10625
David Sterba20e55062015-11-19 11:42:28 +010010626static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010627 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010628 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010629 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010630 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010631 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010632 .tree_fs_info = iotree_fs_info,
10633 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010634
10635 /* optional callbacks */
10636 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010637 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010638 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010639 .set_bit_hook = btrfs_set_bit_hook,
10640 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010641 .merge_extent_hook = btrfs_merge_extent_hook,
10642 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010643 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010644};
10645
Chris Mason35054392009-01-21 13:11:13 -050010646/*
10647 * btrfs doesn't support the bmap operation because swapfiles
10648 * use bmap to make a mapping of extents in the file. They assume
10649 * these extents won't change over the life of the file and they
10650 * use the bmap result to do IO directly to the drive.
10651 *
10652 * the btrfs bmap call would return logical addresses that aren't
10653 * suitable for IO and they also will change frequently as COW
10654 * operations happen. So, swapfile + btrfs == corruption.
10655 *
10656 * For now we're avoiding this by dropping bmap.
10657 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010658static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010659 .readpage = btrfs_readpage,
10660 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010661 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010662 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010663 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010664 .invalidatepage = btrfs_invalidatepage,
10665 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010666 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010667 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010668};
10669
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010670static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010671 .readpage = btrfs_readpage,
10672 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010673 .invalidatepage = btrfs_invalidatepage,
10674 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010675};
10676
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010677static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010678 .getattr = btrfs_getattr,
10679 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010680 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010681 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010682 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010683 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010684 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010685 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010686};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010687static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010688 .getattr = btrfs_getattr,
10689 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010690 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010691 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010692 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010693 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010694 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010695};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010696static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010697 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010698 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010699 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010700 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010701 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010702 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010703};
Yan, Zheng76dda932009-09-21 16:00:26 -040010704
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010705const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010706 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010707 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010708};