blob: bb5de52cbc09babe21d8c8d6aa121a85386a873f [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);
Josef Bacika41ad392011-01-31 15:30:16 -0500104static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400105static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500106static noinline int cow_file_range(struct inode *inode,
107 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800108 u64 start, u64 end, u64 delalloc_end,
109 int *page_started, unsigned long *nr_written,
110 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800111static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
112 u64 orig_start, u64 block_start,
113 u64 block_len, u64 orig_block_len,
114 u64 ram_bytes, int compress_type,
115 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400116
Qu Wenruo524272602017-03-08 10:25:52 +0800117static void __endio_write_update_ordered(struct inode *inode,
118 const u64 offset, const u64 bytes,
119 const bool uptodate);
120
121/*
122 * Cleanup all submitted ordered extents in specified range to handle errors
123 * from the fill_dellaloc() callback.
124 *
125 * NOTE: caller must ensure that when an error happens, it can not call
126 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
127 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
128 * to be released, which we want to happen only when finishing the ordered
129 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
130 * fill_delalloc() callback already does proper cleanup for the first page of
131 * the range, that is, it invokes the callback writepage_end_io_hook() for the
132 * range of the first page.
133 */
134static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
135 const u64 offset,
136 const u64 bytes)
137{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900138 unsigned long index = offset >> PAGE_SHIFT;
139 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
140 struct page *page;
141
142 while (index <= end_index) {
143 page = find_get_page(inode->i_mapping, index);
144 index++;
145 if (!page)
146 continue;
147 ClearPagePrivate2(page);
148 put_page(page);
149 }
Qu Wenruo524272602017-03-08 10:25:52 +0800150 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
151 bytes - PAGE_SIZE, false);
152}
153
Eric Sandeen48a3b632013-04-25 20:41:01 +0000154static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400155
Josef Bacik6a3891c2015-03-16 17:38:52 -0400156#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
157void btrfs_test_inode_set_ops(struct inode *inode)
158{
159 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
160}
161#endif
162
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000163static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500164 struct inode *inode, struct inode *dir,
165 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500166{
167 int err;
168
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000169 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500170 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500171 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500172 return err;
173}
174
Chris Masond352ac62008-09-29 15:18:18 -0400175/*
Chris Masonc8b97812008-10-29 14:49:59 -0400176 * this does all the hard work for inserting an inline extent into
177 * the btree. The caller should have done a btrfs_drop_extents so that
178 * no overlapping inline items exist in the btree
179 */
Chris Mason40f76582014-05-21 13:35:51 -0700180static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000181 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400182 struct btrfs_root *root, struct inode *inode,
183 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000184 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400185 struct page **compressed_pages)
186{
Chris Masonc8b97812008-10-29 14:49:59 -0400187 struct extent_buffer *leaf;
188 struct page *page = NULL;
189 char *kaddr;
190 unsigned long ptr;
191 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400192 int ret;
193 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400194 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400195
Li Zefanfe3f5662011-03-28 08:30:38 +0000196 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400198
Chris Masonc8b97812008-10-29 14:49:59 -0400199 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000200
201 if (!extent_inserted) {
202 struct btrfs_key key;
203 size_t datasize;
204
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200205 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200207 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000208
209 datasize = btrfs_file_extent_calc_inline_size(cur_size);
210 path->leave_spinning = 1;
211 ret = btrfs_insert_empty_item(trans, root, path, &key,
212 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200213 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000214 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400215 }
216 leaf = path->nodes[0];
217 ei = btrfs_item_ptr(leaf, path->slots[0],
218 struct btrfs_file_extent_item);
219 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
220 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
221 btrfs_set_file_extent_encryption(leaf, ei, 0);
222 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
223 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
224 ptr = btrfs_file_extent_inline_start(ei);
225
Li Zefan261507a02010-12-17 14:21:50 +0800226 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400227 struct page *cpage;
228 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500229 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400230 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500231 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300232 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400233
Cong Wang7ac687d2011-11-25 23:14:28 +0800234 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800236 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400237
238 i++;
239 ptr += cur_size;
240 compressed_size -= cur_size;
241 }
242 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800243 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400244 } else {
245 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300246 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800248 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300249 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400250 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800251 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300252 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400253 }
254 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000255 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400256
Yan, Zhengc2167752009-11-12 09:34:21 +0000257 /*
258 * we're an inline extent, so nobody can
259 * extend the file past i_size without locking
260 * a page we already have locked.
261 *
262 * We must do any isize and inode updates
263 * before we unlock the pages. Otherwise we
264 * could end up racing with unlink.
265 */
Chris Masonc8b97812008-10-29 14:49:59 -0400266 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000268
Chris Masonc8b97812008-10-29 14:49:59 -0400269fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200270 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400271}
272
273
274/*
275 * conditionally insert an inline extent into the file. This
276 * does the checks required to make sure the data is small enough
277 * to fit as an inline extent.
278 */
Josef Bacik00361582013-08-14 14:02:47 -0400279static noinline int cow_file_range_inline(struct btrfs_root *root,
280 struct inode *inode, u64 start,
281 u64 end, size_t compressed_size,
282 int compress_type,
283 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400284{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400285 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400286 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400287 u64 isize = i_size_read(inode);
288 u64 actual_end = min(end + 1, isize);
289 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400291 u64 data_len = inline_len;
292 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000293 struct btrfs_path *path;
294 int extent_inserted = 0;
295 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400296
297 if (compressed_size)
298 data_len = compressed_size;
299
300 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400301 actual_end > fs_info->sectorsize ||
302 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400303 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400305 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400306 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400307 return 1;
308 }
309
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 path = btrfs_alloc_path();
311 if (!path)
312 return -ENOMEM;
313
Josef Bacik00361582013-08-14 14:02:47 -0400314 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000315 if (IS_ERR(trans)) {
316 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400317 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000318 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400319 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400320
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000321 if (compressed_size && compressed_pages)
322 extent_item_size = btrfs_file_extent_calc_inline_size(
323 compressed_size);
324 else
325 extent_item_size = btrfs_file_extent_calc_inline_size(
326 inline_len);
327
328 ret = __btrfs_drop_extents(trans, root, inode, path,
329 start, aligned_end, NULL,
330 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400331 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400332 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400333 goto out;
334 }
Chris Masonc8b97812008-10-29 14:49:59 -0400335
336 if (isize > actual_end)
337 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000338 ret = insert_inline_extent(trans, path, extent_inserted,
339 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400340 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000341 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400342 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400343 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400344 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400345 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400346 ret = 1;
347 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400349
Josef Bacikbdc20e62013-02-28 13:23:38 -0500350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200351 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400352out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800353 /*
354 * Don't forget to free the reserved space, as for inlined extent
355 * it won't count as data extent, free them directly here.
356 * And at reserve time, it's always aligned to page size, so
357 * just free one page here.
358 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800359 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000360 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400361 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400362 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400363}
364
Chris Mason771ed682008-11-06 22:02:51 -0500365struct async_extent {
366 u64 start;
367 u64 ram_size;
368 u64 compressed_size;
369 struct page **pages;
370 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800371 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500372 struct list_head list;
373};
374
375struct async_cow {
376 struct inode *inode;
377 struct btrfs_root *root;
378 struct page *locked_page;
379 u64 start;
380 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600381 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500382 struct list_head extents;
383 struct btrfs_work work;
384};
385
386static noinline int add_async_extent(struct async_cow *cow,
387 u64 start, u64 ram_size,
388 u64 compressed_size,
389 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800390 unsigned long nr_pages,
391 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500392{
393 struct async_extent *async_extent;
394
395 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100396 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500397 async_extent->start = start;
398 async_extent->ram_size = ram_size;
399 async_extent->compressed_size = compressed_size;
400 async_extent->pages = pages;
401 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800402 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500403 list_add_tail(&async_extent->list, &cow->extents);
404 return 0;
405}
406
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300407static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400409 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800410
411 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400412 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800413 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200414 /* defrag ioctl */
415 if (BTRFS_I(inode)->defrag_compress)
416 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800417 /* bad compression ratios */
418 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
419 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400420 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800421 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200422 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300423 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800424 return 0;
425}
426
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200427static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800428 u64 start, u64 end, u64 num_bytes, u64 small_write)
429{
430 /* If this is a small write inside eof, kick off a defrag */
431 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200432 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800433 btrfs_add_inode_defrag(NULL, inode);
434}
435
Chris Masonc8b97812008-10-29 14:49:59 -0400436/*
Chris Mason771ed682008-11-06 22:02:51 -0500437 * we create compressed extents in two phases. The first
438 * phase compresses a range of pages that have already been
439 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400440 *
Chris Mason771ed682008-11-06 22:02:51 -0500441 * This is done inside an ordered work queue, and the compression
442 * is spread across many cpus. The actual IO submission is step
443 * two, and the ordered work queue takes care of making sure that
444 * happens in the same order things were put onto the queue by
445 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400446 *
Chris Mason771ed682008-11-06 22:02:51 -0500447 * If this code finds it can't get good compression, it puts an
448 * entry onto the work queue to write the uncompressed bytes. This
449 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300450 * are written in the same order that the flusher thread sent them
451 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400452 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100453static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500454 struct page *locked_page,
455 u64 start, u64 end,
456 struct async_cow *async_cow,
457 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400458{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400459 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400460 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400461 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400462 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500463 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400464 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400465 struct page **pages = NULL;
466 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400467 unsigned long total_compressed = 0;
468 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400469 int i;
470 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400471 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400472 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400473
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200474 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
475 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400476
Chris Mason42dc7ba2008-12-15 11:44:56 -0500477 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400478again:
479 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300480 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100481 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
482 nr_pages = min_t(unsigned long, nr_pages,
483 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400484
Chris Masonf03d9301f2009-02-04 09:31:06 -0500485 /*
486 * we don't want to send crud past the end of i_size through
487 * compression, that's just a waste of CPU time. So, if the
488 * end of the file is before the start of our current
489 * requested range of bytes, we bail out to the uncompressed
490 * cleanup code that can deal with all of this.
491 *
492 * It isn't really the fastest way to fix things, but this is a
493 * very uncommon corner.
494 */
495 if (actual_end <= start)
496 goto cleanup_and_bail_uncompressed;
497
Chris Masonc8b97812008-10-29 14:49:59 -0400498 total_compressed = actual_end - start;
499
Shilong Wang4bcbb332014-10-07 18:44:35 -0400500 /*
501 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400502 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400503 */
504 if (total_compressed <= blocksize &&
505 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
506 goto cleanup_and_bail_uncompressed;
507
David Sterba069eac72017-02-14 19:36:54 +0100508 total_compressed = min_t(unsigned long, total_compressed,
509 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400510 total_in = 0;
511 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400512
Chris Mason771ed682008-11-06 22:02:51 -0500513 /*
514 * we do compression for mount -o compress and when the
515 * inode has not been flagged as nocompress. This flag can
516 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400517 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300518 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400519 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100520 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800521 if (!pages) {
522 /* just bail out to the uncompressed code */
523 goto cont;
524 }
Chris Mason179e29e2007-11-01 11:28:41 -0400525
David Sterbaeec63c62017-07-17 19:41:31 +0200526 if (BTRFS_I(inode)->defrag_compress)
527 compress_type = BTRFS_I(inode)->defrag_compress;
528 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200529 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800530
Chris Mason4adaa612013-03-26 13:07:00 -0400531 /*
532 * we need to call clear_page_dirty_for_io on each
533 * page in the range. Otherwise applications with the file
534 * mmap'd can wander in and change the page contents while
535 * we are compressing them.
536 *
537 * If the compression fails for any reason, we set the pages
538 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300539 *
540 * Note that the remaining part is redirtied, the start pointer
541 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400542 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300543 if (!redirty) {
544 extent_range_clear_dirty_for_io(inode, start, end);
545 redirty = 1;
546 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200547
548 /* Compression level is applied here and only here */
549 ret = btrfs_compress_pages(
550 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800551 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100552 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100553 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800554 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100555 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400556
557 if (!ret) {
558 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300559 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100560 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400561 char *kaddr;
562
563 /* zero the tail end of the last page, we might be
564 * sending it down to disk
565 */
566 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800567 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400568 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300569 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800570 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400571 }
572 will_compress = 1;
573 }
574 }
Li Zefan560f7d72011-09-08 10:22:01 +0800575cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400576 if (start == 0) {
577 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300578 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400579 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500580 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400581 */
Josef Bacik00361582013-08-14 14:02:47 -0400582 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800583 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400584 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500585 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400586 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000587 total_compressed,
588 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400589 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100590 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400591 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400592 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
593 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100594 unsigned long page_error_op;
595
Filipe Mananae6eb4312014-10-10 10:45:12 +0100596 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400597
Chris Mason771ed682008-11-06 22:02:51 -0500598 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100599 * inline extent creation worked or returned error,
600 * we don't need to create any more async work items.
601 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400602 *
603 * We use DO_ACCOUNTING here because we need the
604 * delalloc_release_metadata to be done _after_ we drop
605 * our outstanding extent for clearing delalloc for this
606 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500607 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800608 extent_clear_unlock_delalloc(inode, start, end, end,
609 NULL, clear_flags,
610 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400611 PAGE_CLEAR_DIRTY |
612 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100613 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400614 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400615 goto free_pages_out;
616 }
617 }
618
619 if (will_compress) {
620 /*
621 * we aren't doing an inline extent round the compressed size
622 * up to a block size boundary so the allocator does sane
623 * things
624 */
Qu Wenruofda28322013-02-26 08:10:22 +0000625 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400626
627 /*
628 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300629 * win, compare the page count read with the blocks on disk,
630 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400631 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300632 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300633 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700634 *num_added += 1;
635
636 /*
637 * The async work queues will take care of doing actual
638 * allocation on disk for these compressed pages, and
639 * will submit them to the elevator.
640 */
Timofey Titovets11708622017-10-03 18:06:01 +0300641 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100642 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700643 compress_type);
644
Timofey Titovets11708622017-10-03 18:06:01 +0300645 if (start + total_in < end) {
646 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700647 pages = NULL;
648 cond_resched();
649 goto again;
650 }
651 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400652 }
653 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700654 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400655 /*
656 * the compression code ran but failed to make things smaller,
657 * free any pages it allocated and our page pointer array
658 */
David Sterba4d3a8002017-02-14 19:04:07 +0100659 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400660 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300661 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400662 }
663 kfree(pages);
664 pages = NULL;
665 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100666 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400667
668 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400669 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200670 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500671 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500672 }
Chris Masonc8b97812008-10-29 14:49:59 -0400673 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500674cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700675 /*
676 * No compression, but we still need to write the pages in the file
677 * we've been given so far. redirty the locked page if it corresponds
678 * to our extent and set things up for the async work queue to run
679 * cow_file_range to do the normal delalloc dance.
680 */
681 if (page_offset(locked_page) >= start &&
682 page_offset(locked_page) <= end)
683 __set_page_dirty_nobuffers(locked_page);
684 /* unlocked later on in the async handlers */
685
686 if (redirty)
687 extent_range_redirty_for_io(inode, start, end);
688 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
689 BTRFS_COMPRESS_NONE);
690 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500691
Filipe Mananac44f6492014-10-09 21:15:44 +0100692 return;
Chris Mason771ed682008-11-06 22:02:51 -0500693
694free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100695 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500696 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300697 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500698 }
Chris Masond3977122009-01-05 21:25:51 -0500699 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500700}
Chris Mason771ed682008-11-06 22:02:51 -0500701
Filipe Manana40ae8372014-10-06 22:14:24 +0100702static void free_async_extent_pages(struct async_extent *async_extent)
703{
704 int i;
705
706 if (!async_extent->pages)
707 return;
708
709 for (i = 0; i < async_extent->nr_pages; i++) {
710 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300711 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100712 }
713 kfree(async_extent->pages);
714 async_extent->nr_pages = 0;
715 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500716}
717
718/*
719 * phase two of compressed writeback. This is the ordered portion
720 * of the code, which only gets called in the order the work was
721 * queued. We walk all the async extents created by compress_file_range
722 * and send them down to the disk.
723 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100724static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500725 struct async_cow *async_cow)
726{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400727 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500728 struct async_extent *async_extent;
729 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500730 struct btrfs_key ins;
731 struct extent_map *em;
732 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500733 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500734 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500735
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500736again:
Chris Masond3977122009-01-05 21:25:51 -0500737 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500738 async_extent = list_entry(async_cow->extents.next,
739 struct async_extent, list);
740 list_del(&async_extent->list);
741
742 io_tree = &BTRFS_I(inode)->io_tree;
743
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500744retry:
Chris Mason771ed682008-11-06 22:02:51 -0500745 /* did the compression code fall back to uncompressed IO? */
746 if (!async_extent->pages) {
747 int page_started = 0;
748 unsigned long nr_written = 0;
749
750 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000751 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100752 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500753
754 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500755 ret = cow_file_range(inode, async_cow->locked_page,
756 async_extent->start,
757 async_extent->start +
758 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800759 async_extent->start +
760 async_extent->ram_size - 1,
761 &page_started, &nr_written, 0,
762 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500763
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100764 /* JDM XXX */
765
Chris Mason771ed682008-11-06 22:02:51 -0500766 /*
767 * if page_started, cow_file_range inserted an
768 * inline extent and took care of all the unlocking
769 * and IO for us. Otherwise, we need to submit
770 * all those pages down to the drive.
771 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500772 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200773 extent_write_locked_range(inode,
774 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500775 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500776 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500777 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500778 else if (ret)
779 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500780 kfree(async_extent);
781 cond_resched();
782 continue;
783 }
784
785 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100786 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500787
Wang Xiaoguang18513092016-07-25 15:51:40 +0800788 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500789 async_extent->compressed_size,
790 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800791 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500792 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100793 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500794
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400795 if (ret == -ENOSPC) {
796 unlock_extent(io_tree, async_extent->start,
797 async_extent->start +
798 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800799
800 /*
801 * we need to redirty the pages if we decide to
802 * fallback to uncompressed IO, otherwise we
803 * will not submit these pages down to lower
804 * layers.
805 */
806 extent_range_redirty_for_io(inode,
807 async_extent->start,
808 async_extent->start +
809 async_extent->ram_size - 1);
810
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100811 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400812 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500813 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500814 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000815 /*
816 * here we're doing allocation and writeback of the
817 * compressed pages
818 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800819 em = create_io_em(inode, async_extent->start,
820 async_extent->ram_size, /* len */
821 async_extent->start, /* orig_start */
822 ins.objectid, /* block_start */
823 ins.offset, /* block_len */
824 ins.offset, /* orig_block_len */
825 async_extent->ram_size, /* ram_bytes */
826 async_extent->compress_type,
827 BTRFS_ORDERED_COMPRESSED);
828 if (IS_ERR(em))
829 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500830 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800831 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500832
Li Zefan261507a02010-12-17 14:21:50 +0800833 ret = btrfs_add_ordered_extent_compress(inode,
834 async_extent->start,
835 ins.objectid,
836 async_extent->ram_size,
837 ins.offset,
838 BTRFS_ORDERED_COMPRESSED,
839 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100840 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200841 btrfs_drop_extent_cache(BTRFS_I(inode),
842 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100843 async_extent->start +
844 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500845 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100846 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400847 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500848
Chris Mason771ed682008-11-06 22:02:51 -0500849 /*
850 * clear dirty, set writeback and unlock the pages.
851 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400852 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400853 async_extent->start +
854 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800855 async_extent->start +
856 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400857 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
858 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400859 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200860 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500861 async_extent->start,
862 async_extent->ram_size,
863 ins.objectid,
864 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600865 async_extent->nr_pages,
866 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100867 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
868 struct page *p = async_extent->pages[0];
869 const u64 start = async_extent->start;
870 const u64 end = start + async_extent->ram_size - 1;
871
872 p->mapping = inode->i_mapping;
873 tree->ops->writepage_end_io_hook(p, start, end,
874 NULL, 0);
875 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800876 extent_clear_unlock_delalloc(inode, start, end, end,
877 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100878 PAGE_END_WRITEBACK |
879 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100880 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100881 }
Chris Mason771ed682008-11-06 22:02:51 -0500882 alloc_hint = ins.objectid + ins.offset;
883 kfree(async_extent);
884 cond_resched();
885 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100886 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500887out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400888 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400889 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100890out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400891 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500892 async_extent->start +
893 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800894 async_extent->start +
895 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400896 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100897 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400898 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
899 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100900 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
901 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100902 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100903 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500904 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500905}
906
Josef Bacik4b46fce2010-05-23 11:00:55 -0400907static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
908 u64 num_bytes)
909{
910 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
911 struct extent_map *em;
912 u64 alloc_hint = 0;
913
914 read_lock(&em_tree->lock);
915 em = search_extent_mapping(em_tree, start, num_bytes);
916 if (em) {
917 /*
918 * if block start isn't an actual block number then find the
919 * first block in this inode and use that as a hint. If that
920 * block is also bogus then just don't worry about it.
921 */
922 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
923 free_extent_map(em);
924 em = search_extent_mapping(em_tree, 0, 0);
925 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
926 alloc_hint = em->block_start;
927 if (em)
928 free_extent_map(em);
929 } else {
930 alloc_hint = em->block_start;
931 free_extent_map(em);
932 }
933 }
934 read_unlock(&em_tree->lock);
935
936 return alloc_hint;
937}
938
Chris Mason771ed682008-11-06 22:02:51 -0500939/*
940 * when extent_io.c finds a delayed allocation range in the file,
941 * the call backs end up in this code. The basic idea is to
942 * allocate extents on disk for the range, and create ordered data structs
943 * in ram to track those extents.
944 *
945 * locked_page is the page that writepage had locked already. We use
946 * it to make sure we don't do extra locks or unlocks.
947 *
948 * *page_started is set to one if we unlock locked_page and do everything
949 * required to start IO on it. It may be clean and already done with
950 * IO when we return.
951 */
Josef Bacik00361582013-08-14 14:02:47 -0400952static noinline int cow_file_range(struct inode *inode,
953 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800954 u64 start, u64 end, u64 delalloc_end,
955 int *page_started, unsigned long *nr_written,
956 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500957{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400958 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400959 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500960 u64 alloc_hint = 0;
961 u64 num_bytes;
962 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000963 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400964 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500965 struct btrfs_key ins;
966 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000967 unsigned clear_bits;
968 unsigned long page_ops;
969 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500970 int ret = 0;
971
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200972 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400973 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500974 ret = -EINVAL;
975 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400976 }
Chris Mason771ed682008-11-06 22:02:51 -0500977
Qu Wenruofda28322013-02-26 08:10:22 +0000978 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500979 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800980 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500981
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200982 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400983
Chris Mason771ed682008-11-06 22:02:51 -0500984 if (start == 0) {
985 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800986 ret = cow_file_range_inline(root, inode, start, end, 0,
987 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500988 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400989 /*
990 * We use DO_ACCOUNTING here because we need the
991 * delalloc_release_metadata to be run _after_ we drop
992 * our outstanding extent for clearing delalloc for this
993 * range.
994 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800995 extent_clear_unlock_delalloc(inode, start, end,
996 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400997 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400998 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
999 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001000 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1001 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001002 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001003 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001004 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001005 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001006 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001007 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001008 }
1009 }
Chris Masonc8b97812008-10-29 14:49:59 -04001010
Josef Bacik4b46fce2010-05-23 11:00:55 -04001011 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001012 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1013 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001014
Anand Jain3752d222018-02-15 12:29:38 +08001015 while (num_bytes > 0) {
1016 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001017 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001018 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001019 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001020 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001021 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001022 cur_alloc_size = ins.offset;
1023 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001024
Chris Mason771ed682008-11-06 22:02:51 -05001025 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001026 em = create_io_em(inode, start, ins.offset, /* len */
1027 start, /* orig_start */
1028 ins.objectid, /* block_start */
1029 ins.offset, /* block_len */
1030 ins.offset, /* orig_block_len */
1031 ram_size, /* ram_bytes */
1032 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001033 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001034 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001035 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001036 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001037
Chris Masone6dcd2d2008-07-17 12:53:50 -04001038 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001039 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001040 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001041 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001042
Yan Zheng17d217f2008-12-12 10:03:38 -05001043 if (root->root_key.objectid ==
1044 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1045 ret = btrfs_reloc_clone_csums(inode, start,
1046 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001047 /*
1048 * Only drop cache here, and process as normal.
1049 *
1050 * We must not allow extent_clear_unlock_delalloc()
1051 * at out_unlock label to free meta of this ordered
1052 * extent, as its meta should be freed by
1053 * btrfs_finish_ordered_io().
1054 *
1055 * So we must continue until @start is increased to
1056 * skip current ordered extent.
1057 */
Josef Bacik00361582013-08-14 14:02:47 -04001058 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001059 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1060 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001061 }
1062
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001063 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001064
Chris Masonc8b97812008-10-29 14:49:59 -04001065 /* we're not doing compressed IO, don't unlock the first
1066 * page (which the caller expects to stay locked), don't
1067 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001068 *
1069 * Do set the Private2 bit so we know this page was properly
1070 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001071 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001072 page_ops = unlock ? PAGE_UNLOCK : 0;
1073 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001074
Josef Bacikc2790a22013-07-29 11:20:47 -04001075 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001076 start + ram_size - 1,
1077 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001078 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001079 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001080 if (num_bytes < cur_alloc_size)
1081 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001082 else
Anand Jain3752d222018-02-15 12:29:38 +08001083 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001084 alloc_hint = ins.objectid + ins.offset;
1085 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001086 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001087
1088 /*
1089 * btrfs_reloc_clone_csums() error, since start is increased
1090 * extent_clear_unlock_delalloc() at out_unlock label won't
1091 * free metadata of current ordered extent, we're OK to exit.
1092 */
1093 if (ret)
1094 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001095 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001096out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001097 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001098
Filipe Mananad9f85962014-08-25 10:43:00 +01001099out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001100 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001101out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001102 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001103 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001104out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001105 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1106 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001107 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1108 PAGE_END_WRITEBACK;
1109 /*
1110 * If we reserved an extent for our delalloc range (or a subrange) and
1111 * failed to create the respective ordered extent, then it means that
1112 * when we reserved the extent we decremented the extent's size from
1113 * the data space_info's bytes_may_use counter and incremented the
1114 * space_info's bytes_reserved counter by the same amount. We must make
1115 * sure extent_clear_unlock_delalloc() does not try to decrement again
1116 * the data space_info's bytes_may_use counter, therefore we do not pass
1117 * it the flag EXTENT_CLEAR_DATA_RESV.
1118 */
1119 if (extent_reserved) {
1120 extent_clear_unlock_delalloc(inode, start,
1121 start + cur_alloc_size,
1122 start + cur_alloc_size,
1123 locked_page,
1124 clear_bits,
1125 page_ops);
1126 start += cur_alloc_size;
1127 if (start >= end)
1128 goto out;
1129 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001130 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1131 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001132 clear_bits | EXTENT_CLEAR_DATA_RESV,
1133 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001134 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001135}
Chris Masonc8b97812008-10-29 14:49:59 -04001136
Chris Mason771ed682008-11-06 22:02:51 -05001137/*
1138 * work queue call back to started compression on a file and pages
1139 */
1140static noinline void async_cow_start(struct btrfs_work *work)
1141{
1142 struct async_cow *async_cow;
1143 int num_added = 0;
1144 async_cow = container_of(work, struct async_cow, work);
1145
1146 compress_file_range(async_cow->inode, async_cow->locked_page,
1147 async_cow->start, async_cow->end, async_cow,
1148 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001149 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001150 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001151 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001152 }
Chris Mason771ed682008-11-06 22:02:51 -05001153}
1154
1155/*
1156 * work queue call back to submit previously compressed pages
1157 */
1158static noinline void async_cow_submit(struct btrfs_work *work)
1159{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001160 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001161 struct async_cow *async_cow;
1162 struct btrfs_root *root;
1163 unsigned long nr_pages;
1164
1165 async_cow = container_of(work, struct async_cow, work);
1166
1167 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001168 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001169 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1170 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001171
David Sterbaee863952015-02-16 19:41:40 +01001172 /*
1173 * atomic_sub_return implies a barrier for waitqueue_active
1174 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001175 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001176 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001177 waitqueue_active(&fs_info->async_submit_wait))
1178 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001179
Chris Masond3977122009-01-05 21:25:51 -05001180 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001181 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001182}
Chris Masonc8b97812008-10-29 14:49:59 -04001183
Chris Mason771ed682008-11-06 22:02:51 -05001184static noinline void async_cow_free(struct btrfs_work *work)
1185{
1186 struct async_cow *async_cow;
1187 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001188 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001189 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001190 kfree(async_cow);
1191}
1192
1193static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1194 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001195 unsigned long *nr_written,
1196 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001197{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001198 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001199 struct async_cow *async_cow;
1200 struct btrfs_root *root = BTRFS_I(inode)->root;
1201 unsigned long nr_pages;
1202 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001203
Chris Masona3429ab2009-10-08 12:30:20 -04001204 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001205 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001206 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001207 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001208 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001209 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001210 async_cow->root = root;
1211 async_cow->locked_page = locked_page;
1212 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001213 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001214
Wang Shilongf79707b2014-07-17 11:44:09 +08001215 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001216 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001217 cur_end = end;
1218 else
Byongho Leeee221842015-12-15 01:42:10 +09001219 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001220
1221 async_cow->end = cur_end;
1222 INIT_LIST_HEAD(&async_cow->extents);
1223
Liu Bo9e0af232014-08-15 23:36:53 +08001224 btrfs_init_work(&async_cow->work,
1225 btrfs_delalloc_helper,
1226 async_cow_start, async_cow_submit,
1227 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001228
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001229 nr_pages = (cur_end - start + PAGE_SIZE) >>
1230 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001231 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001232
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001233 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001234
Chris Mason771ed682008-11-06 22:02:51 -05001235 *nr_written += nr_pages;
1236 start = cur_end + 1;
1237 }
1238 *page_started = 1;
1239 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001240}
1241
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001242static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001243 u64 bytenr, u64 num_bytes)
1244{
1245 int ret;
1246 struct btrfs_ordered_sum *sums;
1247 LIST_HEAD(list);
1248
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001249 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001250 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001251 if (ret == 0 && list_empty(&list))
1252 return 0;
1253
1254 while (!list_empty(&list)) {
1255 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1256 list_del(&sums->list);
1257 kfree(sums);
1258 }
Liu Bo58113752018-01-31 17:09:13 -07001259 if (ret < 0)
1260 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001261 return 1;
1262}
1263
Chris Masond352ac62008-09-29 15:18:18 -04001264/*
1265 * when nowcow writeback call back. This checks for snapshots or COW copies
1266 * of the extents that exist in the file, and COWs the file as required.
1267 *
1268 * If no cow copies or snapshots exist, we write directly to the existing
1269 * blocks on disk
1270 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001271static noinline int run_delalloc_nocow(struct inode *inode,
1272 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001273 u64 start, u64 end, int *page_started, int force,
1274 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001275{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001276 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001277 struct btrfs_root *root = BTRFS_I(inode)->root;
1278 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001279 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001281 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001282 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 u64 cow_start;
1284 u64 cur_offset;
1285 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001286 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 u64 disk_bytenr;
1288 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001289 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001290 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001292 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001293 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 int nocow;
1295 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001296 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001297 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001298
1299 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001300 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001301 extent_clear_unlock_delalloc(inode, start, end, end,
1302 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001303 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001304 EXTENT_DO_ACCOUNTING |
1305 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001306 PAGE_CLEAR_DIRTY |
1307 PAGE_SET_WRITEBACK |
1308 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001309 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001310 }
Li Zefan82d59022011-04-20 10:33:24 +08001311
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001312 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001313
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 cow_start = (u64)-1;
1315 cur_offset = start;
1316 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001317 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001319 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001320 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1322 leaf = path->nodes[0];
1323 btrfs_item_key_to_cpu(leaf, &found_key,
1324 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001325 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001326 found_key.type == BTRFS_EXTENT_DATA_KEY)
1327 path->slots[0]--;
1328 }
1329 check_prev = 0;
1330next_slot:
1331 leaf = path->nodes[0];
1332 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1333 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001334 if (ret < 0) {
1335 if (cow_start != (u64)-1)
1336 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001337 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001338 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 if (ret > 0)
1340 break;
1341 leaf = path->nodes[0];
1342 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001343
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 nocow = 0;
1345 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001346 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001348
Filipe Manana1d512cb2015-11-09 00:33:58 +00001349 if (found_key.objectid > ino)
1350 break;
1351 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1352 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1353 path->slots[0]++;
1354 goto next_slot;
1355 }
1356 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 found_key.offset > end)
1358 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001359
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 if (found_key.offset > cur_offset) {
1361 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001362 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 goto out_check;
1364 }
Chris Masonc31f8832008-01-08 15:46:31 -05001365
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 fi = btrfs_item_ptr(leaf, path->slots[0],
1367 struct btrfs_file_extent_item);
1368 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001369
Josef Bacikcc95bef2013-04-04 14:31:27 -04001370 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001371 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1372 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001374 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001375 extent_end = found_key.offset +
1376 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001377 disk_num_bytes =
1378 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001379 if (extent_end <= start) {
1380 path->slots[0]++;
1381 goto next_slot;
1382 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001383 if (disk_bytenr == 0)
1384 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001385 if (btrfs_file_extent_compression(leaf, fi) ||
1386 btrfs_file_extent_encryption(leaf, fi) ||
1387 btrfs_file_extent_other_encoding(leaf, fi))
1388 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001389 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1390 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001391 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001392 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001393 ret = btrfs_cross_ref_exist(root, ino,
1394 found_key.offset -
1395 extent_offset, disk_bytenr);
1396 if (ret) {
1397 /*
1398 * ret could be -EIO if the above fails to read
1399 * metadata.
1400 */
1401 if (ret < 0) {
1402 if (cow_start != (u64)-1)
1403 cur_offset = cow_start;
1404 goto error;
1405 }
1406
1407 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001408 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001409 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001410 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001411 disk_bytenr += cur_offset - found_key.offset;
1412 num_bytes = min(end + 1, extent_end) - cur_offset;
1413 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001414 * if there are pending snapshots for this root,
1415 * we fall into common COW way.
1416 */
1417 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001418 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001419 if (!err)
1420 goto out_check;
1421 }
1422 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001423 * force cow if csum exists in the range.
1424 * this ensure that csum for a given extent are
1425 * either valid or do not exist.
1426 */
Liu Bo58113752018-01-31 17:09:13 -07001427 ret = csum_exist_in_range(fs_info, disk_bytenr,
1428 num_bytes);
1429 if (ret) {
Robbie Ko91e1f562016-10-07 10:01:29 +08001430 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001431 btrfs_end_write_no_snapshotting(root);
Liu Bo58113752018-01-31 17:09:13 -07001432
1433 /*
1434 * ret could be -EIO if the above fails to read
1435 * metadata.
1436 */
1437 if (ret < 0) {
1438 if (cow_start != (u64)-1)
1439 cur_offset = cow_start;
1440 goto error;
1441 }
1442 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001443 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001444 }
1445 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1446 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001447 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001448 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001449 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001450 nocow = 1;
1451 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1452 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001453 btrfs_file_extent_inline_len(leaf,
1454 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001455 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001456 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001457 } else {
1458 BUG_ON(1);
1459 }
1460out_check:
1461 if (extent_end <= start) {
1462 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001463 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001464 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001465 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001466 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001467 goto next_slot;
1468 }
1469 if (!nocow) {
1470 if (cow_start == (u64)-1)
1471 cow_start = cur_offset;
1472 cur_offset = extent_end;
1473 if (cur_offset > end)
1474 break;
1475 path->slots[0]++;
1476 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001477 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001478
David Sterbab3b4aa72011-04-21 01:20:15 +02001479 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001480 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001481 ret = cow_file_range(inode, locked_page,
1482 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001483 end, page_started, nr_written, 1,
1484 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001485 if (ret) {
1486 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001487 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001488 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001489 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001490 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001491 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001492 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001493 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001494 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001495
Yan Zhengd899e052008-10-30 14:25:28 -04001496 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001497 u64 orig_start = found_key.offset - extent_offset;
1498
1499 em = create_io_em(inode, cur_offset, num_bytes,
1500 orig_start,
1501 disk_bytenr, /* block_start */
1502 num_bytes, /* block_len */
1503 disk_num_bytes, /* orig_block_len */
1504 ram_bytes, BTRFS_COMPRESS_NONE,
1505 BTRFS_ORDERED_PREALLOC);
1506 if (IS_ERR(em)) {
1507 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001508 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001509 if (nocow)
1510 btrfs_dec_nocow_writers(fs_info,
1511 disk_bytenr);
1512 ret = PTR_ERR(em);
1513 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001514 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001515 free_extent_map(em);
1516 }
1517
1518 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001519 type = BTRFS_ORDERED_PREALLOC;
1520 } else {
1521 type = BTRFS_ORDERED_NOCOW;
1522 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001523
1524 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001525 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001526 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001527 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001528 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001529
Yan, Zhengefa56462010-05-16 10:49:59 -04001530 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001531 BTRFS_DATA_RELOC_TREE_OBJECTID)
1532 /*
1533 * Error handled later, as we must prevent
1534 * extent_clear_unlock_delalloc() in error handler
1535 * from freeing metadata of created ordered extent.
1536 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001537 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1538 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001539
Josef Bacikc2790a22013-07-29 11:20:47 -04001540 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001541 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001542 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001543 EXTENT_DELALLOC |
1544 EXTENT_CLEAR_DATA_RESV,
1545 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1546
Wang Shilonge9894fd2014-03-27 11:12:25 +08001547 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001548 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001549 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001550
1551 /*
1552 * btrfs_reloc_clone_csums() error, now we're OK to call error
1553 * handler, as metadata for created ordered extent will only
1554 * be freed by btrfs_finish_ordered_io().
1555 */
1556 if (ret)
1557 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001558 if (cur_offset > end)
1559 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001560 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001561 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001562
Josef Bacik17ca04a2012-05-31 15:58:55 -04001563 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001564 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001565 cur_offset = end;
1566 }
1567
Yan Zheng80ff3852008-10-30 14:20:02 -04001568 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001569 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1570 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001571 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001572 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001573 }
1574
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001575error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001576 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001577 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001578 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001579 EXTENT_DELALLOC | EXTENT_DEFRAG |
1580 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1581 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001582 PAGE_SET_WRITEBACK |
1583 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001584 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001585 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001586}
1587
Wang Shilong47059d92014-07-03 18:22:07 +08001588static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1589{
1590
1591 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1592 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1593 return 0;
1594
1595 /*
1596 * @defrag_bytes is a hint value, no spinlock held here,
1597 * if is not zero, it means the file is defragging.
1598 * Force cow if given extent needs to be defragged.
1599 */
1600 if (BTRFS_I(inode)->defrag_bytes &&
1601 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1602 EXTENT_DEFRAG, 0, NULL))
1603 return 1;
1604
1605 return 0;
1606}
1607
Chris Masond352ac62008-09-29 15:18:18 -04001608/*
1609 * extent_io.c call back to do delayed allocation processing
1610 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001611static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001612 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001613 unsigned long *nr_written,
1614 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001615{
Josef Bacikc6100a42017-05-05 11:57:13 -04001616 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001617 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001618 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001619 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001620
Wang Shilong47059d92014-07-03 18:22:07 +08001621 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001622 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001623 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001624 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001625 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001626 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001627 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001628 ret = cow_file_range(inode, locked_page, start, end, end,
1629 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001630 } else {
1631 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1632 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001633 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001634 page_started, nr_written,
1635 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001636 }
Qu Wenruo524272602017-03-08 10:25:52 +08001637 if (ret)
1638 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001639 return ret;
1640}
1641
Josef Bacikc6100a42017-05-05 11:57:13 -04001642static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001643 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001644{
Josef Bacikc6100a42017-05-05 11:57:13 -04001645 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001646 u64 size;
1647
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001648 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001649 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001650 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001651
Josef Bacikdcab6a32015-02-11 15:08:59 -05001652 size = orig->end - orig->start + 1;
1653 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001654 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001655 u64 new_size;
1656
1657 /*
Josef Bacikba117212015-03-13 15:01:24 -04001658 * See the explanation in btrfs_merge_extent_hook, the same
1659 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001660 */
1661 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001662 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001663 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001664 num_extents += count_max_extents(new_size);
1665 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001666 return;
1667 }
1668
Josef Bacik9e0baf62011-07-15 15:16:44 +00001669 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001670 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001671 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001672}
1673
1674/*
1675 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1676 * extents so we can keep track of new extents that are just merged onto old
1677 * extents, such as when we are doing sequential writes, so we can properly
1678 * account for the metadata space we'll need.
1679 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001680static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001681 struct extent_state *new,
1682 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001683{
Josef Bacikc6100a42017-05-05 11:57:13 -04001684 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001685 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001686 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001687
Josef Bacik9ed74f22009-09-11 16:12:44 -04001688 /* not delalloc, ignore it */
1689 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001690 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001691
Josef Bacik8461a3d2015-03-13 15:12:08 -04001692 if (new->start > other->start)
1693 new_size = new->end - other->start + 1;
1694 else
1695 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001696
1697 /* we're not bigger than the max, unreserve the space and go */
1698 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1699 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001700 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001701 spin_unlock(&BTRFS_I(inode)->lock);
1702 return;
1703 }
1704
1705 /*
Josef Bacikba117212015-03-13 15:01:24 -04001706 * We have to add up either side to figure out how many extents were
1707 * accounted for before we merged into one big extent. If the number of
1708 * extents we accounted for is <= the amount we need for the new range
1709 * then we can return, otherwise drop. Think of it like this
1710 *
1711 * [ 4k][MAX_SIZE]
1712 *
1713 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1714 * need 2 outstanding extents, on one side we have 1 and the other side
1715 * we have 1 so they are == and we can return. But in this case
1716 *
1717 * [MAX_SIZE+4k][MAX_SIZE+4k]
1718 *
1719 * Each range on their own accounts for 2 extents, but merged together
1720 * they are only 3 extents worth of accounting, so we need to drop in
1721 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001722 */
Josef Bacikba117212015-03-13 15:01:24 -04001723 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001724 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001725 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001726 num_extents += count_max_extents(old_size);
1727 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001728 return;
1729
Josef Bacik9e0baf62011-07-15 15:16:44 +00001730 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001731 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001732 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001733}
1734
Miao Xieeb73c1b2013-05-15 07:48:22 +00001735static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1736 struct inode *inode)
1737{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001738 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1739
Miao Xieeb73c1b2013-05-15 07:48:22 +00001740 spin_lock(&root->delalloc_lock);
1741 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1742 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1743 &root->delalloc_inodes);
1744 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1745 &BTRFS_I(inode)->runtime_flags);
1746 root->nr_delalloc_inodes++;
1747 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001748 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001749 BUG_ON(!list_empty(&root->delalloc_root));
1750 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001751 &fs_info->delalloc_roots);
1752 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001753 }
1754 }
1755 spin_unlock(&root->delalloc_lock);
1756}
1757
1758static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001759 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001760{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001761 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001762
Miao Xieeb73c1b2013-05-15 07:48:22 +00001763 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001764 if (!list_empty(&inode->delalloc_inodes)) {
1765 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001766 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001767 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001768 root->nr_delalloc_inodes--;
1769 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001770 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001771 BUG_ON(list_empty(&root->delalloc_root));
1772 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001773 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001774 }
1775 }
1776 spin_unlock(&root->delalloc_lock);
1777}
1778
Chris Masond352ac62008-09-29 15:18:18 -04001779/*
1780 * extent_io.c set_bit_hook, used to track delayed allocation
1781 * bytes in this file, and to maintain the list of inodes that
1782 * have pending delalloc work to be done.
1783 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001784static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001785 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001786{
Josef Bacikc6100a42017-05-05 11:57:13 -04001787 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001788
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001789 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1790
Wang Shilong47059d92014-07-03 18:22:07 +08001791 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1792 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001793 /*
1794 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001795 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001796 * bit, which is only set or cleared with irqs on
1797 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001798 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001799 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001800 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001801 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001802 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001803
Josef Bacik8b62f872017-10-19 14:15:55 -04001804 spin_lock(&BTRFS_I(inode)->lock);
1805 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1806 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001807
Josef Bacik6a3891c2015-03-16 17:38:52 -04001808 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001809 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001810 return;
1811
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001812 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1813 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001814 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001815 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001816 if (*bits & EXTENT_DEFRAG)
1817 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001818 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001819 &BTRFS_I(inode)->runtime_flags))
1820 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001821 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001822 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001823
1824 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1825 (*bits & EXTENT_DELALLOC_NEW)) {
1826 spin_lock(&BTRFS_I(inode)->lock);
1827 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1828 state->start;
1829 spin_unlock(&BTRFS_I(inode)->lock);
1830 }
Chris Mason291d6732008-01-29 15:55:23 -05001831}
1832
Chris Masond352ac62008-09-29 15:18:18 -04001833/*
1834 * extent_io.c clear_bit_hook, see set_bit_hook for why
1835 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001836static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001837 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001838 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001839{
Josef Bacikc6100a42017-05-05 11:57:13 -04001840 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001841 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001842 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001843 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001844
Filipe Manana4a4b9642017-07-27 19:52:55 +01001845 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1846 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001847 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001848 spin_unlock(&inode->lock);
1849 }
Wang Shilong47059d92014-07-03 18:22:07 +08001850
Chris Mason75eff682008-12-15 15:54:40 -05001851 /*
1852 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001853 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001854 * bit, which is only set or cleared with irqs on
1855 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001856 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001857 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001858 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001859
Josef Bacik8b62f872017-10-19 14:15:55 -04001860 spin_lock(&inode->lock);
1861 btrfs_mod_outstanding_extents(inode, -num_extents);
1862 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001863
Josef Bacikb6d08f02013-09-27 14:57:43 -04001864 /*
1865 * We don't reserve metadata space for space cache inodes so we
1866 * don't need to call dellalloc_release_metadata if there is an
1867 * error.
1868 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001869 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001870 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001871 btrfs_delalloc_release_metadata(inode, len);
1872
Josef Bacik6a3891c2015-03-16 17:38:52 -04001873 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001874 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001875 return;
1876
Filipe Mananaa315e682017-03-06 23:04:20 +00001877 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1878 do_list && !(state->state & EXTENT_NORESERVE) &&
1879 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001880 btrfs_free_reserved_data_space_noquota(
1881 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001882 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001883
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001884 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1885 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001886 spin_lock(&inode->lock);
1887 inode->delalloc_bytes -= len;
1888 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001889 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001890 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001891 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001892 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001893 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001894
1895 if ((state->state & EXTENT_DELALLOC_NEW) &&
1896 (*bits & EXTENT_DELALLOC_NEW)) {
1897 spin_lock(&inode->lock);
1898 ASSERT(inode->new_delalloc_bytes >= len);
1899 inode->new_delalloc_bytes -= len;
1900 spin_unlock(&inode->lock);
1901 }
Chris Mason291d6732008-01-29 15:55:23 -05001902}
1903
Chris Masond352ac62008-09-29 15:18:18 -04001904/*
1905 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1906 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001907 *
1908 * return 1 if page cannot be merged to bio
1909 * return 0 if page can be merged to bio
1910 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001911 */
Mike Christie81a75f62016-06-05 14:31:54 -05001912int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001913 size_t size, struct bio *bio,
1914 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001915{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001916 struct inode *inode = page->mapping->host;
1917 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001918 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001919 u64 length = 0;
1920 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001921 int ret;
1922
Chris Mason771ed682008-11-06 22:02:51 -05001923 if (bio_flags & EXTENT_BIO_COMPRESSED)
1924 return 0;
1925
Kent Overstreet4f024f32013-10-11 15:44:27 -07001926 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001927 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001928 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1929 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001930 if (ret < 0)
1931 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001932 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001933 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001934 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001935}
1936
Chris Masond352ac62008-09-29 15:18:18 -04001937/*
1938 * in order to insert checksums into the metadata in large chunks,
1939 * we wait until bio submission time. All the pages in the bio are
1940 * checksummed and sums are attached onto the ordered extent record.
1941 *
1942 * At IO completion time the cums attached on the ordered extent record
1943 * are inserted into the btree
1944 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001945static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f62016-06-05 14:31:54 -05001946 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001947 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001948{
Josef Bacikc6100a42017-05-05 11:57:13 -04001949 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001950 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001951
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001952 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001953 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001954 return 0;
1955}
Chris Masone0156402008-04-16 11:15:20 -04001956
Chris Mason4a69a412008-11-06 22:03:00 -05001957/*
1958 * in order to insert checksums into the metadata in large chunks,
1959 * we wait until bio submission time. All the pages in the bio are
1960 * checksummed and sums are attached onto the ordered extent record.
1961 *
1962 * At IO completion time the cums attached on the ordered extent record
1963 * are inserted into the btree
1964 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001965static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001966 int mirror_num, unsigned long bio_flags,
1967 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001968{
Josef Bacikc6100a42017-05-05 11:57:13 -04001969 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001970 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001971 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001972
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001973 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001974 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001975 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001976 bio_endio(bio);
1977 }
Stefan Behrens61891922012-11-05 18:51:52 +01001978 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001979}
1980
Chris Masond352ac62008-09-29 15:18:18 -04001981/*
Chris Masoncad321a2008-12-17 14:51:42 -05001982 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001983 * on write, or reading the csums from the tree before a read.
1984 *
1985 * Rules about async/sync submit,
1986 * a) read: sync submit
1987 *
1988 * b) write without checksum: sync submit
1989 *
1990 * c) write with checksum:
1991 * c-1) if bio is issued by fsync: sync submit
1992 * (sync_writers != 0)
1993 *
1994 * c-2) if root is reloc root: sync submit
1995 * (only in case of buffered IO)
1996 *
1997 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001998 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001999static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04002000 int mirror_num, unsigned long bio_flags,
2001 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04002002{
Josef Bacikc6100a42017-05-05 11:57:13 -04002003 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002004 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002005 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302006 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002007 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002008 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002009 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002010
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002011 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002012
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002013 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302014 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002015
Mike Christie37226b22016-06-05 14:31:52 -05002016 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002017 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002018 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002019 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002020
Chris Masond20f7042008-12-08 16:58:54 -05002021 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002022 ret = btrfs_submit_compressed_read(inode, bio,
2023 mirror_num,
2024 bio_flags);
2025 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002026 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002027 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002028 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002029 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002030 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002031 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002032 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002033 /* csum items have already been cloned */
2034 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2035 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002036 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002037 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2038 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002039 __btrfs_submit_bio_start,
2040 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002041 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002042 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002043 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002044 if (ret)
2045 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002046 }
2047
Chris Mason0b86a832008-03-24 15:01:56 -04002048mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002049 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002050
2051out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002052 if (ret) {
2053 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002054 bio_endio(bio);
2055 }
Stefan Behrens61891922012-11-05 18:51:52 +01002056 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002057}
Chris Mason6885f302008-02-20 16:11:05 -05002058
Chris Masond352ac62008-09-29 15:18:18 -04002059/*
2060 * given a list of ordered sums record them in the inode. This happens
2061 * at IO completion time based on sums calculated at bio submission time.
2062 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002063static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002064 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002065{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002066 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002067 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002068
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002069 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002070 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002071 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002072 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002073 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002074 if (ret)
2075 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002076 }
2077 return 0;
2078}
2079
Josef Bacik2ac55d42010-02-03 19:33:23 +00002080int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002081 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002082 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002083{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002084 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002085 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002086 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002087}
2088
Chris Masond352ac62008-09-29 15:18:18 -04002089/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002090struct btrfs_writepage_fixup {
2091 struct page *page;
2092 struct btrfs_work work;
2093};
2094
Christoph Hellwigb2950862008-12-02 09:54:17 -05002095static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002096{
2097 struct btrfs_writepage_fixup *fixup;
2098 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002099 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002100 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002101 struct page *page;
2102 struct inode *inode;
2103 u64 page_start;
2104 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002105 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002106
2107 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2108 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002109again:
Chris Mason247e7432008-07-17 12:53:51 -04002110 lock_page(page);
2111 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2112 ClearPageChecked(page);
2113 goto out_page;
2114 }
2115
2116 inode = page->mapping->host;
2117 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002118 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002119
David Sterbaff13db42015-12-03 14:30:40 +01002120 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002121 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002122
2123 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002124 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002125 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002126
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002127 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002128 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002129 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002130 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002131 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002132 unlock_page(page);
2133 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002134 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002135 goto again;
2136 }
Chris Mason247e7432008-07-17 12:53:51 -04002137
Qu Wenruo364ecf32017-02-27 15:10:38 +08002138 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002139 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002140 if (ret) {
2141 mapping_set_error(page->mapping, ret);
2142 end_extent_writepage(page, ret, page_start, page_end);
2143 ClearPageChecked(page);
2144 goto out;
2145 }
2146
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002147 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2148 &cached_state, 0);
2149 if (ret) {
2150 mapping_set_error(page->mapping, ret);
2151 end_extent_writepage(page, ret, page_start, page_end);
2152 ClearPageChecked(page);
2153 goto out;
2154 }
2155
Chris Mason247e7432008-07-17 12:53:51 -04002156 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002157 set_page_dirty(page);
Josef Bacik8b62f872017-10-19 14:15:55 -04002158 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002159out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002160 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002161 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002162out_page:
2163 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002164 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002165 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002166 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002167}
2168
2169/*
2170 * There are a few paths in the higher layers of the kernel that directly
2171 * set the page dirty bit without asking the filesystem if it is a
2172 * good idea. This causes problems because we want to make sure COW
2173 * properly happens and the data=ordered rules are followed.
2174 *
Chris Masonc8b97812008-10-29 14:49:59 -04002175 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002176 * hasn't been properly setup for IO. We kick off an async process
2177 * to fix it up. The async helper will wait for ordered extents, set
2178 * the delalloc bit and make it safe to write the page.
2179 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002180static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002181{
2182 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002183 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002184 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002185
Chris Mason8b62b722009-09-02 16:53:46 -04002186 /* this page is properly in the ordered list */
2187 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002188 return 0;
2189
2190 if (PageChecked(page))
2191 return -EAGAIN;
2192
2193 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2194 if (!fixup)
2195 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002196
Chris Mason247e7432008-07-17 12:53:51 -04002197 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002198 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002199 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2200 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002201 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002202 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002203 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002204}
2205
Yan Zhengd899e052008-10-30 14:25:28 -04002206static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2207 struct inode *inode, u64 file_pos,
2208 u64 disk_bytenr, u64 disk_num_bytes,
2209 u64 num_bytes, u64 ram_bytes,
2210 u8 compression, u8 encryption,
2211 u16 other_encoding, int extent_type)
2212{
2213 struct btrfs_root *root = BTRFS_I(inode)->root;
2214 struct btrfs_file_extent_item *fi;
2215 struct btrfs_path *path;
2216 struct extent_buffer *leaf;
2217 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002218 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002219 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002220 int ret;
2221
2222 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002223 if (!path)
2224 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002225
Chris Masona1ed8352009-09-11 12:27:37 -04002226 /*
2227 * we may be replacing one extent in the tree with another.
2228 * The new extent is pinned in the extent map, and we don't want
2229 * to drop it from the cache until it is completely in the btree.
2230 *
2231 * So, tell btrfs_drop_extents to leave this extent in the cache.
2232 * the caller is expected to unpin it and allow it to be merged
2233 * with the others.
2234 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002235 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2236 file_pos + num_bytes, NULL, 0,
2237 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002238 if (ret)
2239 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002240
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002241 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002242 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002243 ins.offset = file_pos;
2244 ins.type = BTRFS_EXTENT_DATA_KEY;
2245
2246 path->leave_spinning = 1;
2247 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2248 sizeof(*fi));
2249 if (ret)
2250 goto out;
2251 }
Yan Zhengd899e052008-10-30 14:25:28 -04002252 leaf = path->nodes[0];
2253 fi = btrfs_item_ptr(leaf, path->slots[0],
2254 struct btrfs_file_extent_item);
2255 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2256 btrfs_set_file_extent_type(leaf, fi, extent_type);
2257 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2258 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2259 btrfs_set_file_extent_offset(leaf, fi, 0);
2260 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2261 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2262 btrfs_set_file_extent_compression(leaf, fi, compression);
2263 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2264 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002265
Yan Zhengd899e052008-10-30 14:25:28 -04002266 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002267 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002268
2269 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002270
2271 ins.objectid = disk_bytenr;
2272 ins.offset = disk_num_bytes;
2273 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002274
Qu Wenruo297d7502015-09-08 17:08:37 +08002275 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002276 * Release the reserved range from inode dirty range map, as it is
2277 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002278 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002279 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2280 if (ret < 0)
2281 goto out;
2282 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002283 ret = btrfs_alloc_reserved_file_extent(trans, root,
2284 btrfs_ino(BTRFS_I(inode)),
2285 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002286out:
Yan Zhengd899e052008-10-30 14:25:28 -04002287 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002288
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002289 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002290}
2291
Liu Bo38c227d2013-01-29 03:18:40 +00002292/* snapshot-aware defrag */
2293struct sa_defrag_extent_backref {
2294 struct rb_node node;
2295 struct old_sa_defrag_extent *old;
2296 u64 root_id;
2297 u64 inum;
2298 u64 file_pos;
2299 u64 extent_offset;
2300 u64 num_bytes;
2301 u64 generation;
2302};
2303
2304struct old_sa_defrag_extent {
2305 struct list_head list;
2306 struct new_sa_defrag_extent *new;
2307
2308 u64 extent_offset;
2309 u64 bytenr;
2310 u64 offset;
2311 u64 len;
2312 int count;
2313};
2314
2315struct new_sa_defrag_extent {
2316 struct rb_root root;
2317 struct list_head head;
2318 struct btrfs_path *path;
2319 struct inode *inode;
2320 u64 file_pos;
2321 u64 len;
2322 u64 bytenr;
2323 u64 disk_len;
2324 u8 compress_type;
2325};
2326
2327static int backref_comp(struct sa_defrag_extent_backref *b1,
2328 struct sa_defrag_extent_backref *b2)
2329{
2330 if (b1->root_id < b2->root_id)
2331 return -1;
2332 else if (b1->root_id > b2->root_id)
2333 return 1;
2334
2335 if (b1->inum < b2->inum)
2336 return -1;
2337 else if (b1->inum > b2->inum)
2338 return 1;
2339
2340 if (b1->file_pos < b2->file_pos)
2341 return -1;
2342 else if (b1->file_pos > b2->file_pos)
2343 return 1;
2344
2345 /*
2346 * [------------------------------] ===> (a range of space)
2347 * |<--->| |<---->| =============> (fs/file tree A)
2348 * |<---------------------------->| ===> (fs/file tree B)
2349 *
2350 * A range of space can refer to two file extents in one tree while
2351 * refer to only one file extent in another tree.
2352 *
2353 * So we may process a disk offset more than one time(two extents in A)
2354 * and locate at the same extent(one extent in B), then insert two same
2355 * backrefs(both refer to the extent in B).
2356 */
2357 return 0;
2358}
2359
2360static void backref_insert(struct rb_root *root,
2361 struct sa_defrag_extent_backref *backref)
2362{
2363 struct rb_node **p = &root->rb_node;
2364 struct rb_node *parent = NULL;
2365 struct sa_defrag_extent_backref *entry;
2366 int ret;
2367
2368 while (*p) {
2369 parent = *p;
2370 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2371
2372 ret = backref_comp(backref, entry);
2373 if (ret < 0)
2374 p = &(*p)->rb_left;
2375 else
2376 p = &(*p)->rb_right;
2377 }
2378
2379 rb_link_node(&backref->node, parent, p);
2380 rb_insert_color(&backref->node, root);
2381}
2382
2383/*
2384 * Note the backref might has changed, and in this case we just return 0.
2385 */
2386static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2387 void *ctx)
2388{
2389 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002390 struct old_sa_defrag_extent *old = ctx;
2391 struct new_sa_defrag_extent *new = old->new;
2392 struct btrfs_path *path = new->path;
2393 struct btrfs_key key;
2394 struct btrfs_root *root;
2395 struct sa_defrag_extent_backref *backref;
2396 struct extent_buffer *leaf;
2397 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002398 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002399 int slot;
2400 int ret;
2401 u64 extent_offset;
2402 u64 num_bytes;
2403
2404 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002405 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002406 return 0;
2407
2408 key.objectid = root_id;
2409 key.type = BTRFS_ROOT_ITEM_KEY;
2410 key.offset = (u64)-1;
2411
Liu Bo38c227d2013-01-29 03:18:40 +00002412 root = btrfs_read_fs_root_no_name(fs_info, &key);
2413 if (IS_ERR(root)) {
2414 if (PTR_ERR(root) == -ENOENT)
2415 return 0;
2416 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002417 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002418 inum, offset, root_id);
2419 return PTR_ERR(root);
2420 }
2421
2422 key.objectid = inum;
2423 key.type = BTRFS_EXTENT_DATA_KEY;
2424 if (offset > (u64)-1 << 32)
2425 key.offset = 0;
2426 else
2427 key.offset = offset;
2428
2429 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302430 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002431 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002432 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002433
2434 while (1) {
2435 cond_resched();
2436
2437 leaf = path->nodes[0];
2438 slot = path->slots[0];
2439
2440 if (slot >= btrfs_header_nritems(leaf)) {
2441 ret = btrfs_next_leaf(root, path);
2442 if (ret < 0) {
2443 goto out;
2444 } else if (ret > 0) {
2445 ret = 0;
2446 goto out;
2447 }
2448 continue;
2449 }
2450
2451 path->slots[0]++;
2452
2453 btrfs_item_key_to_cpu(leaf, &key, slot);
2454
2455 if (key.objectid > inum)
2456 goto out;
2457
2458 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2459 continue;
2460
2461 extent = btrfs_item_ptr(leaf, slot,
2462 struct btrfs_file_extent_item);
2463
2464 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2465 continue;
2466
Liu Boe68afa42013-07-01 22:13:26 +08002467 /*
2468 * 'offset' refers to the exact key.offset,
2469 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2470 * (key.offset - extent_offset).
2471 */
2472 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002473 continue;
2474
Liu Boe68afa42013-07-01 22:13:26 +08002475 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002476 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002477
Liu Bo38c227d2013-01-29 03:18:40 +00002478 if (extent_offset >= old->extent_offset + old->offset +
2479 old->len || extent_offset + num_bytes <=
2480 old->extent_offset + old->offset)
2481 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002482 break;
2483 }
2484
2485 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2486 if (!backref) {
2487 ret = -ENOENT;
2488 goto out;
2489 }
2490
2491 backref->root_id = root_id;
2492 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002493 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002494 backref->num_bytes = num_bytes;
2495 backref->extent_offset = extent_offset;
2496 backref->generation = btrfs_file_extent_generation(leaf, extent);
2497 backref->old = old;
2498 backref_insert(&new->root, backref);
2499 old->count++;
2500out:
2501 btrfs_release_path(path);
2502 WARN_ON(ret);
2503 return ret;
2504}
2505
2506static noinline bool record_extent_backrefs(struct btrfs_path *path,
2507 struct new_sa_defrag_extent *new)
2508{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002509 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002510 struct old_sa_defrag_extent *old, *tmp;
2511 int ret;
2512
2513 new->path = path;
2514
2515 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002516 ret = iterate_inodes_from_logical(old->bytenr +
2517 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002518 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002519 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002520 if (ret < 0 && ret != -ENOENT)
2521 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002522
2523 /* no backref to be processed for this extent */
2524 if (!old->count) {
2525 list_del(&old->list);
2526 kfree(old);
2527 }
2528 }
2529
2530 if (list_empty(&new->head))
2531 return false;
2532
2533 return true;
2534}
2535
2536static int relink_is_mergable(struct extent_buffer *leaf,
2537 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002538 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002539{
Liu Bo116e0022013-08-02 16:30:40 +08002540 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002541 return 0;
2542
2543 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2544 return 0;
2545
Liu Bo116e0022013-08-02 16:30:40 +08002546 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2547 return 0;
2548
2549 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002550 btrfs_file_extent_other_encoding(leaf, fi))
2551 return 0;
2552
2553 return 1;
2554}
2555
2556/*
2557 * Note the backref might has changed, and in this case we just return 0.
2558 */
2559static noinline int relink_extent_backref(struct btrfs_path *path,
2560 struct sa_defrag_extent_backref *prev,
2561 struct sa_defrag_extent_backref *backref)
2562{
2563 struct btrfs_file_extent_item *extent;
2564 struct btrfs_file_extent_item *item;
2565 struct btrfs_ordered_extent *ordered;
2566 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002567 struct btrfs_root *root;
2568 struct btrfs_key key;
2569 struct extent_buffer *leaf;
2570 struct old_sa_defrag_extent *old = backref->old;
2571 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002572 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002573 struct inode *inode;
2574 struct extent_state *cached = NULL;
2575 int ret = 0;
2576 u64 start;
2577 u64 len;
2578 u64 lock_start;
2579 u64 lock_end;
2580 bool merge = false;
2581 int index;
2582
2583 if (prev && prev->root_id == backref->root_id &&
2584 prev->inum == backref->inum &&
2585 prev->file_pos + prev->num_bytes == backref->file_pos)
2586 merge = true;
2587
2588 /* step 1: get root */
2589 key.objectid = backref->root_id;
2590 key.type = BTRFS_ROOT_ITEM_KEY;
2591 key.offset = (u64)-1;
2592
Liu Bo38c227d2013-01-29 03:18:40 +00002593 index = srcu_read_lock(&fs_info->subvol_srcu);
2594
2595 root = btrfs_read_fs_root_no_name(fs_info, &key);
2596 if (IS_ERR(root)) {
2597 srcu_read_unlock(&fs_info->subvol_srcu, index);
2598 if (PTR_ERR(root) == -ENOENT)
2599 return 0;
2600 return PTR_ERR(root);
2601 }
Liu Bo38c227d2013-01-29 03:18:40 +00002602
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002603 if (btrfs_root_readonly(root)) {
2604 srcu_read_unlock(&fs_info->subvol_srcu, index);
2605 return 0;
2606 }
2607
Liu Bo38c227d2013-01-29 03:18:40 +00002608 /* step 2: get inode */
2609 key.objectid = backref->inum;
2610 key.type = BTRFS_INODE_ITEM_KEY;
2611 key.offset = 0;
2612
2613 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2614 if (IS_ERR(inode)) {
2615 srcu_read_unlock(&fs_info->subvol_srcu, index);
2616 return 0;
2617 }
2618
2619 srcu_read_unlock(&fs_info->subvol_srcu, index);
2620
2621 /* step 3: relink backref */
2622 lock_start = backref->file_pos;
2623 lock_end = backref->file_pos + backref->num_bytes - 1;
2624 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002625 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002626
2627 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2628 if (ordered) {
2629 btrfs_put_ordered_extent(ordered);
2630 goto out_unlock;
2631 }
2632
2633 trans = btrfs_join_transaction(root);
2634 if (IS_ERR(trans)) {
2635 ret = PTR_ERR(trans);
2636 goto out_unlock;
2637 }
2638
2639 key.objectid = backref->inum;
2640 key.type = BTRFS_EXTENT_DATA_KEY;
2641 key.offset = backref->file_pos;
2642
2643 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2644 if (ret < 0) {
2645 goto out_free_path;
2646 } else if (ret > 0) {
2647 ret = 0;
2648 goto out_free_path;
2649 }
2650
2651 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2652 struct btrfs_file_extent_item);
2653
2654 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2655 backref->generation)
2656 goto out_free_path;
2657
2658 btrfs_release_path(path);
2659
2660 start = backref->file_pos;
2661 if (backref->extent_offset < old->extent_offset + old->offset)
2662 start += old->extent_offset + old->offset -
2663 backref->extent_offset;
2664
2665 len = min(backref->extent_offset + backref->num_bytes,
2666 old->extent_offset + old->offset + old->len);
2667 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2668
2669 ret = btrfs_drop_extents(trans, root, inode, start,
2670 start + len, 1);
2671 if (ret)
2672 goto out_free_path;
2673again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002674 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002675 key.type = BTRFS_EXTENT_DATA_KEY;
2676 key.offset = start;
2677
Liu Boa09a0a72013-03-11 09:20:58 +00002678 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002679 if (merge) {
2680 struct btrfs_file_extent_item *fi;
2681 u64 extent_len;
2682 struct btrfs_key found_key;
2683
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002684 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002685 if (ret < 0)
2686 goto out_free_path;
2687
2688 path->slots[0]--;
2689 leaf = path->nodes[0];
2690 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2691
2692 fi = btrfs_item_ptr(leaf, path->slots[0],
2693 struct btrfs_file_extent_item);
2694 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2695
Liu Bo116e0022013-08-02 16:30:40 +08002696 if (extent_len + found_key.offset == start &&
2697 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002698 btrfs_set_file_extent_num_bytes(leaf, fi,
2699 extent_len + len);
2700 btrfs_mark_buffer_dirty(leaf);
2701 inode_add_bytes(inode, len);
2702
2703 ret = 1;
2704 goto out_free_path;
2705 } else {
2706 merge = false;
2707 btrfs_release_path(path);
2708 goto again;
2709 }
2710 }
2711
2712 ret = btrfs_insert_empty_item(trans, root, path, &key,
2713 sizeof(*extent));
2714 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002715 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002716 goto out_free_path;
2717 }
2718
2719 leaf = path->nodes[0];
2720 item = btrfs_item_ptr(leaf, path->slots[0],
2721 struct btrfs_file_extent_item);
2722 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2723 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2724 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2725 btrfs_set_file_extent_num_bytes(leaf, item, len);
2726 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2727 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2728 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2729 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2730 btrfs_set_file_extent_encryption(leaf, item, 0);
2731 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2732
2733 btrfs_mark_buffer_dirty(leaf);
2734 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002735 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002736
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002737 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002738 new->disk_len, 0,
2739 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002740 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002741 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002742 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002743 goto out_free_path;
2744 }
2745
2746 ret = 1;
2747out_free_path:
2748 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002749 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002750 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002751out_unlock:
2752 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002753 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002754 iput(inode);
2755 return ret;
2756}
2757
Liu Bo6f519562013-10-29 10:45:05 +08002758static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2759{
2760 struct old_sa_defrag_extent *old, *tmp;
2761
2762 if (!new)
2763 return;
2764
2765 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002766 kfree(old);
2767 }
2768 kfree(new);
2769}
2770
Liu Bo38c227d2013-01-29 03:18:40 +00002771static void relink_file_extents(struct new_sa_defrag_extent *new)
2772{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002773 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002774 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002775 struct sa_defrag_extent_backref *backref;
2776 struct sa_defrag_extent_backref *prev = NULL;
2777 struct inode *inode;
2778 struct btrfs_root *root;
2779 struct rb_node *node;
2780 int ret;
2781
2782 inode = new->inode;
2783 root = BTRFS_I(inode)->root;
2784
2785 path = btrfs_alloc_path();
2786 if (!path)
2787 return;
2788
2789 if (!record_extent_backrefs(path, new)) {
2790 btrfs_free_path(path);
2791 goto out;
2792 }
2793 btrfs_release_path(path);
2794
2795 while (1) {
2796 node = rb_first(&new->root);
2797 if (!node)
2798 break;
2799 rb_erase(node, &new->root);
2800
2801 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2802
2803 ret = relink_extent_backref(path, prev, backref);
2804 WARN_ON(ret < 0);
2805
2806 kfree(prev);
2807
2808 if (ret == 1)
2809 prev = backref;
2810 else
2811 prev = NULL;
2812 cond_resched();
2813 }
2814 kfree(prev);
2815
2816 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002817out:
Liu Bo6f519562013-10-29 10:45:05 +08002818 free_sa_defrag_extent(new);
2819
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002820 atomic_dec(&fs_info->defrag_running);
2821 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002822}
2823
2824static struct new_sa_defrag_extent *
2825record_old_file_extents(struct inode *inode,
2826 struct btrfs_ordered_extent *ordered)
2827{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002828 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002829 struct btrfs_root *root = BTRFS_I(inode)->root;
2830 struct btrfs_path *path;
2831 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002832 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002833 struct new_sa_defrag_extent *new;
2834 int ret;
2835
2836 new = kmalloc(sizeof(*new), GFP_NOFS);
2837 if (!new)
2838 return NULL;
2839
2840 new->inode = inode;
2841 new->file_pos = ordered->file_offset;
2842 new->len = ordered->len;
2843 new->bytenr = ordered->start;
2844 new->disk_len = ordered->disk_len;
2845 new->compress_type = ordered->compress_type;
2846 new->root = RB_ROOT;
2847 INIT_LIST_HEAD(&new->head);
2848
2849 path = btrfs_alloc_path();
2850 if (!path)
2851 goto out_kfree;
2852
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002853 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002854 key.type = BTRFS_EXTENT_DATA_KEY;
2855 key.offset = new->file_pos;
2856
2857 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2858 if (ret < 0)
2859 goto out_free_path;
2860 if (ret > 0 && path->slots[0] > 0)
2861 path->slots[0]--;
2862
2863 /* find out all the old extents for the file range */
2864 while (1) {
2865 struct btrfs_file_extent_item *extent;
2866 struct extent_buffer *l;
2867 int slot;
2868 u64 num_bytes;
2869 u64 offset;
2870 u64 end;
2871 u64 disk_bytenr;
2872 u64 extent_offset;
2873
2874 l = path->nodes[0];
2875 slot = path->slots[0];
2876
2877 if (slot >= btrfs_header_nritems(l)) {
2878 ret = btrfs_next_leaf(root, path);
2879 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002880 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002881 else if (ret > 0)
2882 break;
2883 continue;
2884 }
2885
2886 btrfs_item_key_to_cpu(l, &key, slot);
2887
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002888 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002889 break;
2890 if (key.type != BTRFS_EXTENT_DATA_KEY)
2891 break;
2892 if (key.offset >= new->file_pos + new->len)
2893 break;
2894
2895 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2896
2897 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2898 if (key.offset + num_bytes < new->file_pos)
2899 goto next;
2900
2901 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2902 if (!disk_bytenr)
2903 goto next;
2904
2905 extent_offset = btrfs_file_extent_offset(l, extent);
2906
2907 old = kmalloc(sizeof(*old), GFP_NOFS);
2908 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002909 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002910
2911 offset = max(new->file_pos, key.offset);
2912 end = min(new->file_pos + new->len, key.offset + num_bytes);
2913
2914 old->bytenr = disk_bytenr;
2915 old->extent_offset = extent_offset;
2916 old->offset = offset - key.offset;
2917 old->len = end - offset;
2918 old->new = new;
2919 old->count = 0;
2920 list_add_tail(&old->list, &new->head);
2921next:
2922 path->slots[0]++;
2923 cond_resched();
2924 }
2925
2926 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002927 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002928
2929 return new;
2930
Liu Bo38c227d2013-01-29 03:18:40 +00002931out_free_path:
2932 btrfs_free_path(path);
2933out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002934 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002935 return NULL;
2936}
2937
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002938static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002939 u64 start, u64 len)
2940{
2941 struct btrfs_block_group_cache *cache;
2942
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002943 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002944 ASSERT(cache);
2945
2946 spin_lock(&cache->lock);
2947 cache->delalloc_bytes -= len;
2948 spin_unlock(&cache->lock);
2949
2950 btrfs_put_block_group(cache);
2951}
2952
Chris Masond352ac62008-09-29 15:18:18 -04002953/* as ordered data IO finishes, this gets called so we can finish
2954 * an ordered extent if the range of bytes in the file it covers are
2955 * fully written.
2956 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002957static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002958{
Josef Bacik5fd02042012-05-02 14:00:54 -04002959 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002960 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002961 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002962 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002963 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002964 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002965 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002966 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002967 int ret = 0;
2968 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002969 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002970 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002971 bool range_locked = false;
2972 bool clear_new_delalloc_bytes = false;
2973
2974 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2975 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2976 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2977 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002978
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002979 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002980
Josef Bacik5fd02042012-05-02 14:00:54 -04002981 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2982 ret = -EIO;
2983 goto out;
2984 }
2985
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002986 btrfs_free_io_failure_record(BTRFS_I(inode),
2987 ordered_extent->file_offset,
2988 ordered_extent->file_offset +
2989 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002990
Josef Bacik77cef2e2013-08-29 13:57:21 -04002991 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2992 truncated = true;
2993 logical_len = ordered_extent->truncated_len;
2994 /* Truncated the entire extent, don't bother adding */
2995 if (!logical_len)
2996 goto out;
2997 }
2998
Yan, Zhengc2167752009-11-12 09:34:21 +00002999 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003000 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08003001
3002 /*
3003 * For mwrite(mmap + memset to write) case, we still reserve
3004 * space for NOCOW range.
3005 * As NOCOW won't cause a new delayed ref, just free the space
3006 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08003007 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08003008 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05003009 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3010 if (nolock)
3011 trans = btrfs_join_transaction_nolock(root);
3012 else
3013 trans = btrfs_join_transaction(root);
3014 if (IS_ERR(trans)) {
3015 ret = PTR_ERR(trans);
3016 trans = NULL;
3017 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003018 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003019 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003020 ret = btrfs_update_inode_fallback(trans, root, inode);
3021 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003022 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003023 goto out;
3024 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003025
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003026 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003027 lock_extent_bits(io_tree, ordered_extent->file_offset,
3028 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003029 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003030
Liu Bo38c227d2013-01-29 03:18:40 +00003031 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3032 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003033 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003034 if (ret) {
3035 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003036 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003037 /* the inode is shared */
3038 new = record_old_file_extents(inode, ordered_extent);
3039
3040 clear_extent_bit(io_tree, ordered_extent->file_offset,
3041 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003042 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003043 }
3044
Josef Bacik0cb59c92010-07-02 12:14:14 -04003045 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003046 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003047 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003048 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003049 if (IS_ERR(trans)) {
3050 ret = PTR_ERR(trans);
3051 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003052 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003053 }
Chris Masona79b7d42014-05-22 16:18:52 -07003054
Josef Bacik69fe2d72017-10-19 14:15:57 -04003055 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003056
Chris Masonc8b97812008-10-29 14:49:59 -04003057 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003058 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003059 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003060 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003061 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3062 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003063 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003064 ordered_extent->file_offset,
3065 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003066 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003067 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003068 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003069 ret = insert_reserved_file_extent(trans, inode,
3070 ordered_extent->file_offset,
3071 ordered_extent->start,
3072 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003073 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003074 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003075 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003076 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003077 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003078 ordered_extent->start,
3079 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003080 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003081 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3082 ordered_extent->file_offset, ordered_extent->len,
3083 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003084 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003085 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003086 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003087 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003088
Nikolay Borisovac01f262018-01-08 10:59:43 +02003089 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3090 if (ret) {
3091 btrfs_abort_transaction(trans, ret);
3092 goto out;
3093 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003094
Josef Bacik6c760c02012-11-09 10:53:21 -05003095 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3096 ret = btrfs_update_inode_fallback(trans, root, inode);
3097 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003098 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003099 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003100 }
3101 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003102out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003103 if (range_locked || clear_new_delalloc_bytes) {
3104 unsigned int clear_bits = 0;
3105
3106 if (range_locked)
3107 clear_bits |= EXTENT_LOCKED;
3108 if (clear_new_delalloc_bytes)
3109 clear_bits |= EXTENT_DELALLOC_NEW;
3110 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3111 ordered_extent->file_offset,
3112 ordered_extent->file_offset +
3113 ordered_extent->len - 1,
3114 clear_bits,
3115 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003116 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003117 }
3118
Miao Xiea698d0752012-09-20 01:51:59 -06003119 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003120 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003121
Josef Bacik77cef2e2013-08-29 13:57:21 -04003122 if (ret || truncated) {
3123 u64 start, end;
3124
3125 if (truncated)
3126 start = ordered_extent->file_offset + logical_len;
3127 else
3128 start = ordered_extent->file_offset;
3129 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003130 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003131
3132 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003133 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003134
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003135 /*
3136 * If the ordered extent had an IOERR or something else went
3137 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003138 * back to the allocator. We only free the extent in the
3139 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003140 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003141 if ((ret || !logical_len) &&
3142 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003143 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003144 btrfs_free_reserved_extent(fs_info,
3145 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003146 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003147 }
3148
3149
Josef Bacik5fd02042012-05-02 14:00:54 -04003150 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003151 * This needs to be done to make sure anybody waiting knows we are done
3152 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003153 */
3154 btrfs_remove_ordered_extent(inode, ordered_extent);
3155
Liu Bo38c227d2013-01-29 03:18:40 +00003156 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003157 if (new) {
3158 if (ret) {
3159 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003160 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003161 } else {
3162 relink_file_extents(new);
3163 }
3164 }
Liu Bo38c227d2013-01-29 03:18:40 +00003165
Chris Masone6dcd2d2008-07-17 12:53:50 -04003166 /* once for us */
3167 btrfs_put_ordered_extent(ordered_extent);
3168 /* once for the tree */
3169 btrfs_put_ordered_extent(ordered_extent);
3170
Josef Bacik5fd02042012-05-02 14:00:54 -04003171 return ret;
3172}
3173
3174static void finish_ordered_fn(struct btrfs_work *work)
3175{
3176 struct btrfs_ordered_extent *ordered_extent;
3177 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3178 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003179}
3180
David Sterbac3988d62017-02-17 15:18:32 +01003181static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003182 struct extent_state *state, int uptodate)
3183{
Josef Bacik5fd02042012-05-02 14:00:54 -04003184 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003185 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003186 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003187 struct btrfs_workqueue *wq;
3188 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003189
liubo1abe9b82011-03-24 11:18:59 +00003190 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3191
Chris Mason8b62b722009-09-02 16:53:46 -04003192 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003193 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3194 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003195 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003196
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003197 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003198 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003199 func = btrfs_freespace_write_helper;
3200 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003201 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003202 func = btrfs_endio_write_helper;
3203 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003204
Liu Bo9e0af232014-08-15 23:36:53 +08003205 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3206 NULL);
3207 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003208}
3209
Miao Xiedc380ae2014-09-12 18:43:55 +08003210static int __readpage_endio_check(struct inode *inode,
3211 struct btrfs_io_bio *io_bio,
3212 int icsum, struct page *page,
3213 int pgoff, u64 start, size_t len)
3214{
3215 char *kaddr;
3216 u32 csum_expected;
3217 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003218
3219 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3220
3221 kaddr = kmap_atomic(page);
3222 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003223 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003224 if (csum != csum_expected)
3225 goto zeroit;
3226
3227 kunmap_atomic(kaddr);
3228 return 0;
3229zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003230 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003231 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003232 memset(kaddr + pgoff, 1, len);
3233 flush_dcache_page(page);
3234 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003235 return -EIO;
3236}
3237
Chris Masond352ac62008-09-29 15:18:18 -04003238/*
Chris Masond352ac62008-09-29 15:18:18 -04003239 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003240 * if there's a match, we allow the bio to finish. If not, the code in
3241 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003242 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003243static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3244 u64 phy_offset, struct page *page,
3245 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003246{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003247 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003248 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003249 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003250 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003251
Chris Masond20f7042008-12-08 16:58:54 -05003252 if (PageChecked(page)) {
3253 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003254 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003255 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003256
3257 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003258 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003259
Yan Zheng17d217f2008-12-12 10:03:38 -05003260 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003261 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003262 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003263 return 0;
3264 }
3265
Miao Xiefacc8a222013-07-25 19:22:34 +08003266 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003267 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3268 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003269}
Chris Masonb888db22007-08-27 16:49:44 -04003270
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003271/*
3272 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3273 *
3274 * @inode: The inode we want to perform iput on
3275 *
3276 * This function uses the generic vfs_inode::i_count to track whether we should
3277 * just decrement it (in case it's > 1) or if this is the last iput then link
3278 * the inode to the delayed iput machinery. Delayed iputs are processed at
3279 * transaction commit time/superblock commit/cleaner kthread.
3280 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003281void btrfs_add_delayed_iput(struct inode *inode)
3282{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003283 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003284 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003285
3286 if (atomic_add_unless(&inode->i_count, -1, 1))
3287 return;
3288
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003289 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003290 ASSERT(list_empty(&binode->delayed_iput));
3291 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003292 spin_unlock(&fs_info->delayed_iput_lock);
3293}
3294
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003295void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003296{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003297
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003298 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003299 while (!list_empty(&fs_info->delayed_iputs)) {
3300 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003301
David Sterba8089fe62015-11-19 14:15:51 +01003302 inode = list_first_entry(&fs_info->delayed_iputs,
3303 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003304 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003305 spin_unlock(&fs_info->delayed_iput_lock);
3306 iput(&inode->vfs_inode);
3307 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003308 }
David Sterba8089fe62015-11-19 14:15:51 +01003309 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003310}
3311
Yan, Zhengd68fc572010-05-16 10:49:58 -04003312/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003313 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003314 * files in the subvolume, it removes orphan item and frees block_rsv
3315 * structure.
3316 */
3317void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3318 struct btrfs_root *root)
3319{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003320 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003321 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003322 int ret;
3323
Josef Bacik8a35d952012-05-23 14:26:42 -04003324 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003325 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3326 return;
3327
Josef Bacik90290e12011-12-02 15:44:12 -05003328 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003329 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003330 spin_unlock(&root->orphan_lock);
3331 return;
3332 }
3333
3334 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3335 spin_unlock(&root->orphan_lock);
3336 return;
3337 }
3338
3339 block_rsv = root->orphan_block_rsv;
3340 root->orphan_block_rsv = NULL;
3341 spin_unlock(&root->orphan_lock);
3342
Miao Xie27cdeb72014-04-02 19:51:05 +08003343 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003344 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003345 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003346 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003347 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003348 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003349 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003350 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3351 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003352 }
3353
Josef Bacik90290e12011-12-02 15:44:12 -05003354 if (block_rsv) {
3355 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003356 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003357 }
3358}
3359
3360/*
Josef Bacik7b128762008-07-24 12:17:14 -04003361 * This creates an orphan entry for the given inode in case something goes
3362 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003363 *
3364 * NOTE: caller of this function should reserve 5 units of metadata for
3365 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003366 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003367int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3368 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003369{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003370 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3371 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003372 struct btrfs_block_rsv *block_rsv = NULL;
3373 int reserve = 0;
3374 int insert = 0;
3375 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003376
Yan, Zhengd68fc572010-05-16 10:49:58 -04003377 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003378 block_rsv = btrfs_alloc_block_rsv(fs_info,
3379 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003380 if (!block_rsv)
3381 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003382 }
3383
Josef Bacik8a35d952012-05-23 14:26:42 -04003384 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003385 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003386#if 0
3387 /*
3388 * For proper ENOSPC handling, we should do orphan
3389 * cleanup when mounting. But this introduces backward
3390 * compatibility issue.
3391 */
3392 if (!xchg(&root->orphan_item_inserted, 1))
3393 insert = 2;
3394 else
3395 insert = 1;
3396#endif
3397 insert = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003398 }
Josef Bacik7b128762008-07-24 12:17:14 -04003399
Josef Bacik72ac3c02012-05-23 14:13:11 -04003400 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003401 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003402 reserve = 1;
Liu Bo3d5adda2018-01-25 11:02:55 -07003403
3404 spin_lock(&root->orphan_lock);
3405 /* If someone has created ->orphan_block_rsv, be happy to use it. */
3406 if (!root->orphan_block_rsv) {
3407 root->orphan_block_rsv = block_rsv;
3408 } else if (block_rsv) {
3409 btrfs_free_block_rsv(fs_info, block_rsv);
3410 block_rsv = NULL;
3411 }
3412
3413 if (insert)
3414 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003415 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003416
Yan, Zhengd68fc572010-05-16 10:49:58 -04003417 /* grab metadata reservation from transaction handle */
3418 if (reserve) {
3419 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003420 ASSERT(!ret);
3421 if (ret) {
Liu Bo1a932ef2018-01-25 11:02:54 -07003422 /*
3423 * dec doesn't need spin_lock as ->orphan_block_rsv
3424 * would be released only if ->orphan_inodes is
3425 * zero.
3426 */
Josef Bacik3b6571c2016-05-27 13:03:04 -04003427 atomic_dec(&root->orphan_inodes);
3428 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003429 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003430 if (insert)
3431 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003432 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003433 return ret;
3434 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003435 }
3436
3437 /* insert an orphan item to track this unlinked/truncated file */
3438 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003439 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003440 if (ret) {
Josef Bacik4ef31a42013-08-13 14:10:08 -04003441 if (reserve) {
3442 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003443 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003444 btrfs_orphan_release_metadata(inode);
3445 }
Liu Bo1a932ef2018-01-25 11:02:54 -07003446 /*
3447 * btrfs_orphan_commit_root may race with us and set
3448 * ->orphan_block_rsv to zero, in order to avoid that,
3449 * decrease ->orphan_inodes after everything is done.
3450 */
3451 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003452 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003453 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003454 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003455 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003456 return ret;
3457 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003458 }
3459 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003460 }
3461
3462 /* insert an orphan item to track subvolume contains orphan files */
3463 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003464 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003465 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003466 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003467 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003468 return ret;
3469 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003470 }
3471 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003472}
3473
3474/*
3475 * We have done the truncate/delete so we can go ahead and remove the orphan
3476 * item for this particular inode.
3477 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003478static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003479 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003480{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003481 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003482 int delete_item = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003483 int ret = 0;
3484
Josef Bacik8a35d952012-05-23 14:26:42 -04003485 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003486 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003487 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003488
Liu Bo1a932ef2018-01-25 11:02:54 -07003489 if (delete_item && trans)
3490 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
3491
Josef Bacik72ac3c02012-05-23 14:13:11 -04003492 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003493 &inode->runtime_flags))
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003494 btrfs_orphan_release_metadata(inode);
3495
Liu Bo1a932ef2018-01-25 11:02:54 -07003496 /*
3497 * btrfs_orphan_commit_root may race with us and set ->orphan_block_rsv
3498 * to zero, in order to avoid that, decrease ->orphan_inodes after
3499 * everything is done.
3500 */
3501 if (delete_item)
3502 atomic_dec(&root->orphan_inodes);
3503
Josef Bacik4ef31a42013-08-13 14:10:08 -04003504 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003505}
3506
3507/*
3508 * this cleans up any orphans that may be left on the list from the last use
3509 * of this root.
3510 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003511int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003512{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003513 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003514 struct btrfs_path *path;
3515 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003516 struct btrfs_key key, found_key;
3517 struct btrfs_trans_handle *trans;
3518 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003519 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003520 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3521
Yan, Zhengd68fc572010-05-16 10:49:58 -04003522 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003523 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003524
3525 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003526 if (!path) {
3527 ret = -ENOMEM;
3528 goto out;
3529 }
David Sterbae4058b52015-11-27 16:31:35 +01003530 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003531
3532 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003533 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003534 key.offset = (u64)-1;
3535
Josef Bacik7b128762008-07-24 12:17:14 -04003536 while (1) {
3537 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003538 if (ret < 0)
3539 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003540
3541 /*
3542 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003543 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003544 * find the key and see if we have stuff that matches
3545 */
3546 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003547 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003548 if (path->slots[0] == 0)
3549 break;
3550 path->slots[0]--;
3551 }
3552
3553 /* pull out the item */
3554 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003555 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3556
3557 /* make sure the item matches what we want */
3558 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3559 break;
David Sterba962a2982014-06-04 18:41:45 +02003560 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003561 break;
3562
3563 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003564 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003565
3566 /*
3567 * this is where we are basically btrfs_lookup, without the
3568 * crossing root thing. we store the inode number in the
3569 * offset of the orphan item.
3570 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003571
3572 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003573 btrfs_err(fs_info,
3574 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003575 ret = -EINVAL;
3576 goto out;
3577 }
3578
3579 last_objectid = found_key.offset;
3580
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003581 found_key.objectid = found_key.offset;
3582 found_key.type = BTRFS_INODE_ITEM_KEY;
3583 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003584 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303585 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003586 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003587 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003588
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003589 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003590 struct btrfs_root *dead_root;
3591 struct btrfs_fs_info *fs_info = root->fs_info;
3592 int is_dead_root = 0;
3593
3594 /*
3595 * this is an orphan in the tree root. Currently these
3596 * could come from 2 sources:
3597 * a) a snapshot deletion in progress
3598 * b) a free space cache inode
3599 * We need to distinguish those two, as the snapshot
3600 * orphan must not get deleted.
3601 * find_dead_roots already ran before us, so if this
3602 * is a snapshot deletion, we should find the root
3603 * in the dead_roots list
3604 */
3605 spin_lock(&fs_info->trans_lock);
3606 list_for_each_entry(dead_root, &fs_info->dead_roots,
3607 root_list) {
3608 if (dead_root->root_key.objectid ==
3609 found_key.objectid) {
3610 is_dead_root = 1;
3611 break;
3612 }
3613 }
3614 spin_unlock(&fs_info->trans_lock);
3615 if (is_dead_root) {
3616 /* prevent this orphan from being found again */
3617 key.offset = found_key.objectid - 1;
3618 continue;
3619 }
3620 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003621 /*
3622 * Inode is already gone but the orphan item is still there,
3623 * kill the orphan item.
3624 */
Filipe Manana67710892016-06-06 11:51:25 +01003625 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003626 trans = btrfs_start_transaction(root, 1);
3627 if (IS_ERR(trans)) {
3628 ret = PTR_ERR(trans);
3629 goto out;
3630 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003631 btrfs_debug(fs_info, "auto deleting %Lu",
3632 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003633 ret = btrfs_del_orphan_item(trans, root,
3634 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003635 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003636 if (ret)
3637 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003638 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003639 }
Josef Bacik7b128762008-07-24 12:17:14 -04003640
Josef Bacik7b128762008-07-24 12:17:14 -04003641 /*
3642 * add this inode to the orphan list so btrfs_orphan_del does
3643 * the proper thing when we hit it
3644 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003645 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3646 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003647 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003648
Josef Bacik7b128762008-07-24 12:17:14 -04003649 /* if we have links, this was a truncate, lets do that */
3650 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303651 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003652 iput(inode);
3653 continue;
3654 }
Josef Bacik7b128762008-07-24 12:17:14 -04003655 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003656
3657 /* 1 for the orphan item deletion. */
3658 trans = btrfs_start_transaction(root, 1);
3659 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003660 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003661 ret = PTR_ERR(trans);
3662 goto out;
3663 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003664 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003665 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003666 if (ret) {
3667 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003668 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003669 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003670
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003671 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003672 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003673 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003674 } else {
3675 nr_unlink++;
3676 }
3677
3678 /* this will do delete_inode and everything for us */
3679 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003680 if (ret)
3681 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003682 }
Miao Xie3254c872011-11-10 20:45:05 -05003683 /* release the path since we're done with it */
3684 btrfs_release_path(path);
3685
Yan, Zhengd68fc572010-05-16 10:49:58 -04003686 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3687
3688 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003689 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003690 (u64)-1);
3691
Miao Xie27cdeb72014-04-02 19:51:05 +08003692 if (root->orphan_block_rsv ||
3693 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003694 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003695 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003696 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003697 }
Josef Bacik7b128762008-07-24 12:17:14 -04003698
3699 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003700 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003701 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003702 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003703
3704out:
3705 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003706 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003707 btrfs_free_path(path);
3708 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003709}
3710
Chris Masond352ac62008-09-29 15:18:18 -04003711/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003712 * very simple check to peek ahead in the leaf looking for xattrs. If we
3713 * don't find any xattrs, we know there can't be any acls.
3714 *
3715 * slot is the slot the inode is in, objectid is the objectid of the inode
3716 */
3717static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003718 int slot, u64 objectid,
3719 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003720{
3721 u32 nritems = btrfs_header_nritems(leaf);
3722 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003723 static u64 xattr_access = 0;
3724 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003725 int scanned = 0;
3726
Josef Bacikf23b5a52013-06-19 10:16:26 -04003727 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003728 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3729 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3730 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3731 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003732 }
3733
Chris Mason46a53cc2009-04-27 11:47:50 -04003734 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003735 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003736 while (slot < nritems) {
3737 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3738
3739 /* we found a different objectid, there must not be acls */
3740 if (found_key.objectid != objectid)
3741 return 0;
3742
3743 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003744 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003745 if (*first_xattr_slot == -1)
3746 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003747 if (found_key.offset == xattr_access ||
3748 found_key.offset == xattr_default)
3749 return 1;
3750 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003751
3752 /*
3753 * we found a key greater than an xattr key, there can't
3754 * be any acls later on
3755 */
3756 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3757 return 0;
3758
3759 slot++;
3760 scanned++;
3761
3762 /*
3763 * it goes inode, inode backrefs, xattrs, extents,
3764 * so if there are a ton of hard links to an inode there can
3765 * be a lot of backrefs. Don't waste time searching too hard,
3766 * this is just an optimization
3767 */
3768 if (scanned >= 8)
3769 break;
3770 }
3771 /* we hit the end of the leaf before we found an xattr or
3772 * something larger than an xattr. We have to assume the inode
3773 * has acls
3774 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003775 if (*first_xattr_slot == -1)
3776 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003777 return 1;
3778}
3779
3780/*
Chris Masond352ac62008-09-29 15:18:18 -04003781 * read an inode from the btree into the in-memory inode
3782 */
Filipe Manana67710892016-06-06 11:51:25 +01003783static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003784{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003785 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003786 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003787 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003788 struct btrfs_inode_item *inode_item;
3789 struct btrfs_root *root = BTRFS_I(inode)->root;
3790 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003791 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003792 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003793 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003794 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003795 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003796 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003797
3798 ret = btrfs_fill_inode(inode, &rdev);
3799 if (!ret)
3800 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003801
3802 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003803 if (!path) {
3804 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003805 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003806 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003807
Chris Mason39279cc2007-06-12 06:35:45 -04003808 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003809
Chris Mason39279cc2007-06-12 06:35:45 -04003810 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003811 if (ret) {
3812 if (ret > 0)
3813 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003814 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003815 }
Chris Mason39279cc2007-06-12 06:35:45 -04003816
Chris Mason5f39d392007-10-15 16:14:19 -04003817 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003818
3819 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003820 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003821
Chris Mason5f39d392007-10-15 16:14:19 -04003822 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3823 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003824 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003825 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003826 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3827 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003828 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003829
David Sterbaa937b972014-12-12 17:39:12 +01003830 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3831 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003832
David Sterbaa937b972014-12-12 17:39:12 +01003833 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3834 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003835
David Sterbaa937b972014-12-12 17:39:12 +01003836 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3837 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003838
chandan r9cc97d62012-07-04 12:48:07 +05303839 BTRFS_I(inode)->i_otime.tv_sec =
3840 btrfs_timespec_sec(leaf, &inode_item->otime);
3841 BTRFS_I(inode)->i_otime.tv_nsec =
3842 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003843
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003844 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003845 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003846 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3847
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003848 inode_set_iversion_queried(inode,
3849 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003850 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003851 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003852 rdev = btrfs_inode_rdev(leaf, inode_item);
3853
Josef Bacikaec74772008-07-24 12:12:38 -04003854 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003855 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003856
3857cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003858 /*
3859 * If we were modified in the current generation and evicted from memory
3860 * and then re-read we need to do a full sync since we don't have any
3861 * idea about which extents were modified before we were evicted from
3862 * cache.
3863 *
3864 * This is required for both inode re-read from disk and delayed inode
3865 * in delayed_nodes_tree.
3866 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003867 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003868 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3869 &BTRFS_I(inode)->runtime_flags);
3870
Filipe Mananabde6c242015-07-24 00:00:19 +01003871 /*
3872 * We don't persist the id of the transaction where an unlink operation
3873 * against the inode was last made. So here we assume the inode might
3874 * have been evicted, and therefore the exact value of last_unlink_trans
3875 * lost, and set it to last_trans to avoid metadata inconsistencies
3876 * between the inode and its parent if the inode is fsync'ed and the log
3877 * replayed. For example, in the scenario:
3878 *
3879 * touch mydir/foo
3880 * ln mydir/foo mydir/bar
3881 * sync
3882 * unlink mydir/bar
3883 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3884 * xfs_io -c fsync mydir/foo
3885 * <power failure>
3886 * mount fs, triggers fsync log replay
3887 *
3888 * We must make sure that when we fsync our inode foo we also log its
3889 * parent inode, otherwise after log replay the parent still has the
3890 * dentry with the "bar" name but our inode foo has a link count of 1
3891 * and doesn't have an inode ref with the name "bar" anymore.
3892 *
3893 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003894 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003895 * transaction commits on fsync if our inode is a directory, or if our
3896 * inode is not a directory, logging its parent unnecessarily.
3897 */
3898 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3899
Miao Xie67de1172013-12-26 13:07:06 +08003900 path->slots[0]++;
3901 if (inode->i_nlink != 1 ||
3902 path->slots[0] >= btrfs_header_nritems(leaf))
3903 goto cache_acl;
3904
3905 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003906 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003907 goto cache_acl;
3908
3909 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3910 if (location.type == BTRFS_INODE_REF_KEY) {
3911 struct btrfs_inode_ref *ref;
3912
3913 ref = (struct btrfs_inode_ref *)ptr;
3914 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3915 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3916 struct btrfs_inode_extref *extref;
3917
3918 extref = (struct btrfs_inode_extref *)ptr;
3919 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3920 extref);
3921 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003922cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003923 /*
3924 * try to precache a NULL acl entry for files that don't have
3925 * any xattrs or acls
3926 */
Li Zefan33345d012011-04-20 10:31:50 +08003927 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003928 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003929 if (first_xattr_slot != -1) {
3930 path->slots[0] = first_xattr_slot;
3931 ret = btrfs_load_inode_props(inode, path);
3932 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003933 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003934 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003935 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003936 root->root_key.objectid, ret);
3937 }
3938 btrfs_free_path(path);
3939
Al Viro72c04902009-06-24 16:58:48 -04003940 if (!maybe_acls)
3941 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003942
Chris Mason39279cc2007-06-12 06:35:45 -04003943 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003944 case S_IFREG:
3945 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003946 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003947 inode->i_fop = &btrfs_file_operations;
3948 inode->i_op = &btrfs_file_inode_operations;
3949 break;
3950 case S_IFDIR:
3951 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003952 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003953 break;
3954 case S_IFLNK:
3955 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003956 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003957 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3958 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003959 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003960 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003961 init_special_inode(inode, inode->i_mode, rdev);
3962 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003963 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003964
3965 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003966 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003967
3968make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003969 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003970 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003971 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003972}
3973
Chris Masond352ac62008-09-29 15:18:18 -04003974/*
3975 * given a leaf and an inode, copy the inode fields into the leaf
3976 */
Chris Masone02119d2008-09-05 16:13:11 -04003977static void fill_inode_item(struct btrfs_trans_handle *trans,
3978 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003979 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003980 struct inode *inode)
3981{
Liu Bo51fab692012-12-27 09:01:21 +00003982 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003983
Liu Bo51fab692012-12-27 09:01:21 +00003984 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003985
Liu Bo51fab692012-12-27 09:01:21 +00003986 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3987 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3988 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3989 &token);
3990 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3991 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003992
David Sterbaa937b972014-12-12 17:39:12 +01003993 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003994 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003995 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003996 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003997
David Sterbaa937b972014-12-12 17:39:12 +01003998 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003999 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01004000 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00004001 inode->i_mtime.tv_nsec, &token);
4002
David Sterbaa937b972014-12-12 17:39:12 +01004003 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00004004 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01004005 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00004006 inode->i_ctime.tv_nsec, &token);
4007
chandan r9cc97d62012-07-04 12:48:07 +05304008 btrfs_set_token_timespec_sec(leaf, &item->otime,
4009 BTRFS_I(inode)->i_otime.tv_sec, &token);
4010 btrfs_set_token_timespec_nsec(leaf, &item->otime,
4011 BTRFS_I(inode)->i_otime.tv_nsec, &token);
4012
Liu Bo51fab692012-12-27 09:01:21 +00004013 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
4014 &token);
4015 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
4016 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05004017 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
4018 &token);
Liu Bo51fab692012-12-27 09:01:21 +00004019 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
4020 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
4021 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
4022 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04004023}
4024
Chris Masond352ac62008-09-29 15:18:18 -04004025/*
4026 * copy everything in the in-memory inode into the btree.
4027 */
Chris Mason21151332011-11-10 20:39:08 -05004028static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05004029 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004030{
4031 struct btrfs_inode_item *inode_item;
4032 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004033 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004034 int ret;
4035
4036 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004037 if (!path)
4038 return -ENOMEM;
4039
Chris Masonb9473432009-03-13 11:00:37 -04004040 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08004041 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
4042 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004043 if (ret) {
4044 if (ret > 0)
4045 ret = -ENOENT;
4046 goto failed;
4047 }
4048
Chris Mason5f39d392007-10-15 16:14:19 -04004049 leaf = path->nodes[0];
4050 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004051 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004052
Chris Masone02119d2008-09-05 16:13:11 -04004053 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004054 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004055 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004056 ret = 0;
4057failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004058 btrfs_free_path(path);
4059 return ret;
4060}
4061
Chris Masond352ac62008-09-29 15:18:18 -04004062/*
Chris Mason21151332011-11-10 20:39:08 -05004063 * copy everything in the in-memory inode into the btree.
4064 */
4065noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4066 struct btrfs_root *root, struct inode *inode)
4067{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004068 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004069 int ret;
4070
4071 /*
4072 * If the inode is a free space inode, we can deadlock during commit
4073 * if we put it into the delayed code.
4074 *
4075 * The data relocation inode should also be directly updated
4076 * without delay
4077 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004078 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004079 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004080 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004081 btrfs_update_root_times(trans, root);
4082
Chris Mason21151332011-11-10 20:39:08 -05004083 ret = btrfs_delayed_update_inode(trans, root, inode);
4084 if (!ret)
4085 btrfs_set_inode_last_trans(trans, inode);
4086 return ret;
4087 }
4088
4089 return btrfs_update_inode_item(trans, root, inode);
4090}
4091
Josef Bacikbe6aef62012-10-22 15:43:12 -04004092noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4093 struct btrfs_root *root,
4094 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004095{
4096 int ret;
4097
4098 ret = btrfs_update_inode(trans, root, inode);
4099 if (ret == -ENOSPC)
4100 return btrfs_update_inode_item(trans, root, inode);
4101 return ret;
4102}
4103
4104/*
Chris Masond352ac62008-09-29 15:18:18 -04004105 * unlink helper that gets used here in inode.c and in the tree logging
4106 * recovery code. It remove a link in a directory with a given name, and
4107 * also drops the back refs in the inode to the directory
4108 */
Al Viro92986792011-03-04 17:14:37 +00004109static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4110 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004111 struct btrfs_inode *dir,
4112 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004113 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004114{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004115 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004116 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004117 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004118 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004119 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004120 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004121 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004122 u64 ino = btrfs_ino(inode);
4123 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004124
4125 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004126 if (!path) {
4127 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004128 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004129 }
4130
Chris Masonb9473432009-03-13 11:00:37 -04004131 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004132 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004133 name, name_len, -1);
4134 if (IS_ERR(di)) {
4135 ret = PTR_ERR(di);
4136 goto err;
4137 }
4138 if (!di) {
4139 ret = -ENOENT;
4140 goto err;
4141 }
Chris Mason5f39d392007-10-15 16:14:19 -04004142 leaf = path->nodes[0];
4143 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004144 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004145 if (ret)
4146 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004147 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004148
Miao Xie67de1172013-12-26 13:07:06 +08004149 /*
4150 * If we don't have dir index, we have to get it by looking up
4151 * the inode ref, since we get the inode ref, remove it directly,
4152 * it is unnecessary to do delayed deletion.
4153 *
4154 * But if we have dir index, needn't search inode ref to get it.
4155 * Since the inode ref is close to the inode item, it is better
4156 * that we delay to delete it, and just do this deletion when
4157 * we update the inode item.
4158 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004159 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004160 ret = btrfs_delayed_delete_inode_ref(inode);
4161 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004162 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004163 goto skip_backref;
4164 }
4165 }
4166
Li Zefan33345d012011-04-20 10:31:50 +08004167 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4168 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004169 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004170 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004171 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004172 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004173 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004174 goto err;
4175 }
Miao Xie67de1172013-12-26 13:07:06 +08004176skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004177 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004178 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004179 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004180 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004181 }
Chris Mason39279cc2007-06-12 06:35:45 -04004182
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004183 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4184 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004185 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004186 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004187 goto err;
4188 }
Chris Masone02119d2008-09-05 16:13:11 -04004189
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004190 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4191 index);
Chris Mason6418c962010-10-30 07:34:24 -04004192 if (ret == -ENOENT)
4193 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004194 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004195 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004196err:
4197 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004198 if (ret)
4199 goto out;
4200
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004201 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004202 inode_inc_iversion(&inode->vfs_inode);
4203 inode_inc_iversion(&dir->vfs_inode);
4204 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4205 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4206 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004207out:
Chris Mason39279cc2007-06-12 06:35:45 -04004208 return ret;
4209}
4210
Al Viro92986792011-03-04 17:14:37 +00004211int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4212 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004213 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004214 const char *name, int name_len)
4215{
4216 int ret;
4217 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4218 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004219 drop_nlink(&inode->vfs_inode);
4220 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004221 }
4222 return ret;
4223}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004224
4225/*
4226 * helper to start transaction for unlink and rmdir.
4227 *
Josef Bacikd52be812013-05-29 14:54:47 -04004228 * unlink and rmdir are special in btrfs, they do not always free space, so
4229 * if we cannot make our reservations the normal way try and see if there is
4230 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4231 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004232 */
Josef Bacikd52be812013-05-29 14:54:47 -04004233static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004234{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004235 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004236
Josef Bacike70bea52011-10-11 14:18:24 -04004237 /*
4238 * 1 for the possible orphan item
4239 * 1 for the dir item
4240 * 1 for the dir index
4241 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004242 * 1 for the inode
4243 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004244 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004245}
4246
Chris Mason39279cc2007-06-12 06:35:45 -04004247static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4248{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004249 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004250 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004251 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004252 int ret;
4253
Josef Bacikd52be812013-05-29 14:54:47 -04004254 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004255 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004256 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004257
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004258 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4259 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004260
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004261 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4262 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4263 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004264 if (ret)
4265 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004266
Yan, Zhenga22285a2010-05-16 10:48:46 -04004267 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004268 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004269 if (ret)
4270 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004271 }
Josef Bacik7b128762008-07-24 12:17:14 -04004272
Tsutomu Itohb5324022011-07-19 07:27:20 +00004273out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004274 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004275 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004276 return ret;
4277}
4278
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004279int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4280 struct btrfs_root *root,
4281 struct inode *dir, u64 objectid,
4282 const char *name, int name_len)
4283{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004284 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004285 struct btrfs_path *path;
4286 struct extent_buffer *leaf;
4287 struct btrfs_dir_item *di;
4288 struct btrfs_key key;
4289 u64 index;
4290 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004291 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004292
4293 path = btrfs_alloc_path();
4294 if (!path)
4295 return -ENOMEM;
4296
Li Zefan33345d012011-04-20 10:31:50 +08004297 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004298 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004299 if (IS_ERR_OR_NULL(di)) {
4300 if (!di)
4301 ret = -ENOENT;
4302 else
4303 ret = PTR_ERR(di);
4304 goto out;
4305 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004306
4307 leaf = path->nodes[0];
4308 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4309 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4310 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004311 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004312 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004313 goto out;
4314 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004315 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004316
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004317 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4318 root->root_key.objectid, dir_ino,
4319 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004320 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004321 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004322 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004323 goto out;
4324 }
Li Zefan33345d012011-04-20 10:31:50 +08004325 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004326 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004327 if (IS_ERR_OR_NULL(di)) {
4328 if (!di)
4329 ret = -ENOENT;
4330 else
4331 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004332 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004333 goto out;
4334 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004335
4336 leaf = path->nodes[0];
4337 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004338 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004339 index = key.offset;
4340 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004341 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004342
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004343 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004344 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004345 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004346 goto out;
4347 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004348
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004349 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004350 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004351 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004352 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004353 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004354 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004355out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004356 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004357 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004358}
4359
Chris Mason39279cc2007-06-12 06:35:45 -04004360static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4361{
David Howells2b0143b2015-03-17 22:25:59 +00004362 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004363 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004364 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004365 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004366 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004367
David Sterbab3ae2442012-09-13 16:04:34 -06004368 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004369 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004370 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004371 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004372
Josef Bacikd52be812013-05-29 14:54:47 -04004373 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004374 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004375 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004376
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004377 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004378 err = btrfs_unlink_subvol(trans, root, dir,
4379 BTRFS_I(inode)->location.objectid,
4380 dentry->d_name.name,
4381 dentry->d_name.len);
4382 goto out;
4383 }
4384
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004385 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004386 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004387 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004388
Filipe Manana44f714d2016-06-06 16:11:13 +01004389 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4390
Chris Mason39279cc2007-06-12 06:35:45 -04004391 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004392 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4393 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4394 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004395 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004396 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004397 /*
4398 * Propagate the last_unlink_trans value of the deleted dir to
4399 * its parent directory. This is to prevent an unrecoverable
4400 * log tree in the case we do something like this:
4401 * 1) create dir foo
4402 * 2) create snapshot under dir foo
4403 * 3) delete the snapshot
4404 * 4) rmdir foo
4405 * 5) mkdir foo
4406 * 6) fsync foo or some file inside foo
4407 */
4408 if (last_unlink_trans >= trans->transid)
4409 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4410 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004411out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004412 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004413 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004414
Chris Mason39279cc2007-06-12 06:35:45 -04004415 return err;
4416}
4417
Chris Mason28f75a02015-02-04 06:59:29 -08004418static int truncate_space_check(struct btrfs_trans_handle *trans,
4419 struct btrfs_root *root,
4420 u64 bytes_deleted)
4421{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004422 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004423 int ret;
4424
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004425 /*
4426 * This is only used to apply pressure to the enospc system, we don't
4427 * intend to use this reservation at all.
4428 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004429 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004430 bytes_deleted *= fs_info->nodesize;
4431 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004432 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004433 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004434 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004435 trans->transid,
4436 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004437 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004438 }
Chris Mason28f75a02015-02-04 06:59:29 -08004439 return ret;
4440
4441}
4442
Josef Bacikddfae632017-10-19 14:16:02 -04004443/*
4444 * Return this if we need to call truncate_block for the last bit of the
4445 * truncate.
4446 */
4447#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004448
Chris Mason323ac952008-10-01 19:05:46 -04004449/*
Chris Mason39279cc2007-06-12 06:35:45 -04004450 * this can truncate away extent items, csum items and directory items.
4451 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004452 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004453 *
4454 * csum items that cross the new i_size are truncated to the new size
4455 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004456 *
4457 * min_type is the minimum key type to truncate down to. If set to 0, this
4458 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004459 */
Yan, Zheng80825102009-11-12 09:35:36 +00004460int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4461 struct btrfs_root *root,
4462 struct inode *inode,
4463 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004464{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004465 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004466 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004467 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004468 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004469 struct btrfs_key key;
4470 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004471 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004472 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004473 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004474 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004475 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004476 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004477 int found_extent;
4478 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004479 int pending_del_nr = 0;
4480 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004481 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004482 int ret;
4483 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004484 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004485 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004486 bool be_nice = false;
4487 bool should_throttle = false;
4488 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004489
4490 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004491
Chris Mason28ed1342014-12-17 09:41:04 -08004492 /*
4493 * for non-free space inodes and ref cows, we want to back off from
4494 * time to time
4495 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004496 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004497 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004498 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004499
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004500 path = btrfs_alloc_path();
4501 if (!path)
4502 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004503 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004504
Josef Bacik5dc562c2012-08-17 13:14:17 -04004505 /*
4506 * We want to drop from the next block forward in case this new size is
4507 * not block aligned since we will be keeping the last block of the
4508 * extent just the way it is.
4509 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004510 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004511 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004512 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004513 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004514 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004515
Miao Xie16cdcec2011-04-22 18:12:22 +08004516 /*
4517 * This function is also used to drop the items in the log tree before
4518 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4519 * it is used to drop the loged items. So we shouldn't kill the delayed
4520 * items.
4521 */
4522 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004523 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004524
Li Zefan33345d012011-04-20 10:31:50 +08004525 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004526 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004527 key.type = (u8)-1;
4528
Chris Mason85e21ba2008-01-29 15:11:36 -05004529search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004530 /*
4531 * with a 16K leaf size and 128MB extents, you can actually queue
4532 * up a huge file in a single leaf. Most of the time that
4533 * bytes_deleted is > 0, it will be huge by the time we get here
4534 */
Byongho Leeee221842015-12-15 01:42:10 +09004535 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004536 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004537 err = -EAGAIN;
4538 goto error;
4539 }
4540 }
4541
4542
Chris Masonb9473432009-03-13 11:00:37 -04004543 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004544 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004545 if (ret < 0) {
4546 err = ret;
4547 goto out;
4548 }
Chris Masond3977122009-01-05 21:25:51 -05004549
Chris Mason85e21ba2008-01-29 15:11:36 -05004550 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004551 /* there are no items in the tree for us to truncate, we're
4552 * done
4553 */
Yan, Zheng80825102009-11-12 09:35:36 +00004554 if (path->slots[0] == 0)
4555 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004556 path->slots[0]--;
4557 }
4558
Chris Masond3977122009-01-05 21:25:51 -05004559 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004560 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004561 leaf = path->nodes[0];
4562 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004563 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004564
Li Zefan33345d012011-04-20 10:31:50 +08004565 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004566 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004567
Chris Mason85e21ba2008-01-29 15:11:36 -05004568 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004569 break;
4570
Chris Mason5f39d392007-10-15 16:14:19 -04004571 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004572 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004573 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004574 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004575 extent_type = btrfs_file_extent_type(leaf, fi);
4576 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004577 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004578 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004579
4580 trace_btrfs_truncate_show_fi_regular(
4581 BTRFS_I(inode), leaf, fi,
4582 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004583 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004584 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004585 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004586
4587 trace_btrfs_truncate_show_fi_inline(
4588 BTRFS_I(inode), leaf, fi, path->slots[0],
4589 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004590 }
Yan008630c2007-11-07 13:31:09 -05004591 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004592 }
Yan, Zheng80825102009-11-12 09:35:36 +00004593 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004594 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004595 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004596 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004597 break;
4598 if (found_key.offset >= new_size)
4599 del_item = 1;
4600 else
4601 del_item = 0;
4602 }
Chris Mason39279cc2007-06-12 06:35:45 -04004603 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004604 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004605 if (found_type != BTRFS_EXTENT_DATA_KEY)
4606 goto delete;
4607
4608 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004609 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004610 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004611 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004612 u64 orig_num_bytes =
4613 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004614 extent_num_bytes = ALIGN(new_size -
4615 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004616 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004617 btrfs_set_file_extent_num_bytes(leaf, fi,
4618 extent_num_bytes);
4619 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004620 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004621 if (test_bit(BTRFS_ROOT_REF_COWS,
4622 &root->state) &&
4623 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004624 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004625 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004626 } else {
Chris Masondb945352007-10-15 16:15:53 -04004627 extent_num_bytes =
4628 btrfs_file_extent_disk_num_bytes(leaf,
4629 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004630 extent_offset = found_key.offset -
4631 btrfs_file_extent_offset(leaf, fi);
4632
Chris Mason39279cc2007-06-12 06:35:45 -04004633 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004634 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004635 if (extent_start != 0) {
4636 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004637 if (test_bit(BTRFS_ROOT_REF_COWS,
4638 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004639 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004640 }
4641 }
Chris Mason90692182008-02-08 13:49:28 -05004642 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004643 /*
4644 * we can't truncate inline items that have had
4645 * special encodings
4646 */
4647 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004648 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004649 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4650 btrfs_file_extent_compression(leaf, fi) == 0) {
4651 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004652
Josef Bacikddfae632017-10-19 14:16:02 -04004653 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4654 size = btrfs_file_extent_calc_inline_size(size);
4655 btrfs_truncate_item(root->fs_info, path, size, 1);
4656 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004657 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004658 * We have to bail so the last_size is set to
4659 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004660 */
Josef Bacikddfae632017-10-19 14:16:02 -04004661 err = NEED_TRUNCATE_BLOCK;
4662 break;
Chris Mason90692182008-02-08 13:49:28 -05004663 }
Josef Bacikddfae632017-10-19 14:16:02 -04004664
4665 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4666 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004667 }
Chris Mason179e29e2007-11-01 11:28:41 -04004668delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004669 if (del_item)
4670 last_size = found_key.offset;
4671 else
4672 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004673 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004674 if (!pending_del_nr) {
4675 /* no pending yet, add ourselves */
4676 pending_del_slot = path->slots[0];
4677 pending_del_nr = 1;
4678 } else if (pending_del_nr &&
4679 path->slots[0] + 1 == pending_del_slot) {
4680 /* hop on the pending chunk */
4681 pending_del_nr++;
4682 pending_del_slot = path->slots[0];
4683 } else {
Chris Masond3977122009-01-05 21:25:51 -05004684 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004685 }
Chris Mason39279cc2007-06-12 06:35:45 -04004686 } else {
4687 break;
4688 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004689 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004690
Miao Xie27cdeb72014-04-02 19:51:05 +08004691 if (found_extent &&
4692 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004693 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004694 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004695 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004696 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004697 extent_num_bytes, 0,
4698 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004699 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004700 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004701 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4702 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004703 trans->delayed_ref_updates * 2,
4704 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004705 if (be_nice) {
4706 if (truncate_space_check(trans, root,
4707 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004708 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004709 }
4710 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004711 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004712 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004713 }
Chris Mason39279cc2007-06-12 06:35:45 -04004714 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004715
Yan, Zheng80825102009-11-12 09:35:36 +00004716 if (found_type == BTRFS_INODE_ITEM_KEY)
4717 break;
4718
4719 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004720 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004721 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004722 if (pending_del_nr) {
4723 ret = btrfs_del_items(trans, root, path,
4724 pending_del_slot,
4725 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004726 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004727 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004728 goto error;
4729 }
Yan, Zheng80825102009-11-12 09:35:36 +00004730 pending_del_nr = 0;
4731 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004732 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004733 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004734 unsigned long updates = trans->delayed_ref_updates;
4735 if (updates) {
4736 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004737 ret = btrfs_run_delayed_refs(trans,
4738 fs_info,
4739 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004740 if (ret && !err)
4741 err = ret;
4742 }
4743 }
Chris Mason28f75a02015-02-04 06:59:29 -08004744 /*
4745 * if we failed to refill our space rsv, bail out
4746 * and let the transaction restart
4747 */
4748 if (should_end) {
4749 err = -EAGAIN;
4750 goto error;
4751 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004752 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004753 } else {
4754 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004755 }
Chris Mason39279cc2007-06-12 06:35:45 -04004756 }
Yan, Zheng80825102009-11-12 09:35:36 +00004757out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004758 if (pending_del_nr) {
4759 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4760 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004761 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004762 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004763 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004764error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004765 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4766 ASSERT(last_size >= new_size);
4767 if (!err && last_size > new_size)
4768 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004769 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004770 }
Chris Mason28ed1342014-12-17 09:41:04 -08004771
Chris Mason39279cc2007-06-12 06:35:45 -04004772 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004773
Byongho Leeee221842015-12-15 01:42:10 +09004774 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004775 unsigned long updates = trans->delayed_ref_updates;
4776 if (updates) {
4777 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004778 ret = btrfs_run_delayed_refs(trans, fs_info,
4779 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004780 if (ret && !err)
4781 err = ret;
4782 }
4783 }
Yan, Zheng80825102009-11-12 09:35:36 +00004784 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004785}
4786
Chris Masona52d9a82007-08-27 16:49:44 -04004787/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304788 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004789 * @inode - inode that we're zeroing
4790 * @from - the offset to start zeroing
4791 * @len - the length to zero, 0 to zero the entire range respective to the
4792 * offset
4793 * @front - zero up to the offset instead of from the offset on
4794 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304795 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004796 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004797 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304798int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004799 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004800{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004801 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004802 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004803 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4804 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004805 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004806 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004807 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004808 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004809 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304810 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004811 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004812 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004813 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304814 u64 block_start;
4815 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004816
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004817 if (IS_ALIGNED(offset, blocksize) &&
4818 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004819 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304820
Josef Bacik8b62f872017-10-19 14:15:55 -04004821 block_start = round_down(from, blocksize);
4822 block_end = block_start + blocksize - 1;
4823
Qu Wenruo364ecf32017-02-27 15:10:38 +08004824 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004825 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004826 if (ret)
4827 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004828
Chris Mason211c17f2008-05-15 09:13:45 -04004829again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004830 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004831 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004832 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004833 block_start, blocksize);
4834 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004835 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004836 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004837 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004838
Chris Masona52d9a82007-08-27 16:49:44 -04004839 if (!PageUptodate(page)) {
4840 ret = btrfs_readpage(NULL, page);
4841 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004842 if (page->mapping != mapping) {
4843 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004844 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004845 goto again;
4846 }
Chris Masona52d9a82007-08-27 16:49:44 -04004847 if (!PageUptodate(page)) {
4848 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004849 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004850 }
4851 }
Chris Mason211c17f2008-05-15 09:13:45 -04004852 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004853
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304854 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004855 set_page_extent_mapped(page);
4856
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304857 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004858 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304859 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004860 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004861 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004862 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004863 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004864 btrfs_put_ordered_extent(ordered);
4865 goto again;
4866 }
4867
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304868 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004869 EXTENT_DIRTY | EXTENT_DELALLOC |
4870 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004871 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004872
Filipe Mananae3b8a482017-11-04 00:16:59 +00004873 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004874 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004875 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304876 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004877 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004878 goto out_unlock;
4879 }
4880
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304881 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004882 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304883 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004884 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004885 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304886 memset(kaddr + (block_start - page_offset(page)),
4887 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004888 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304889 memset(kaddr + (block_start - page_offset(page)) + offset,
4890 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004891 flush_dcache_page(page);
4892 kunmap(page);
4893 }
Chris Mason247e7432008-07-17 12:53:51 -04004894 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004895 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004896 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004897
Chris Mason89642222008-07-24 09:41:53 -04004898out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004899 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004900 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304901 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004902 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004903 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004904 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004905out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004906 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004907 return ret;
4908}
4909
Josef Bacik16e75492013-10-22 12:18:51 -04004910static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4911 u64 offset, u64 len)
4912{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004913 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004914 struct btrfs_trans_handle *trans;
4915 int ret;
4916
4917 /*
4918 * Still need to make sure the inode looks like it's been updated so
4919 * that any holes get logged if we fsync.
4920 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004921 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4922 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004923 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4924 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4925 return 0;
4926 }
4927
4928 /*
4929 * 1 - for the one we're dropping
4930 * 1 - for the one we're adding
4931 * 1 - for updating the inode.
4932 */
4933 trans = btrfs_start_transaction(root, 3);
4934 if (IS_ERR(trans))
4935 return PTR_ERR(trans);
4936
4937 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4938 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004939 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004940 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004941 return ret;
4942 }
4943
David Sterbaf85b7372017-01-20 14:54:07 +01004944 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4945 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004946 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004947 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004948 else
4949 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004950 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004951 return ret;
4952}
4953
Josef Bacik695a0d02011-03-04 15:46:53 -05004954/*
4955 * This function puts in dummy file extents for the area we're creating a hole
4956 * for. So if we are truncating this file to a larger size we need to insert
4957 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4958 * the range between oldsize and size
4959 */
Josef Bacika41ad392011-01-31 15:30:16 -05004960int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004961{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004962 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004963 struct btrfs_root *root = BTRFS_I(inode)->root;
4964 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004965 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004966 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004967 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004968 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4969 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004970 u64 last_byte;
4971 u64 cur_offset;
4972 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004973 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004974
Josef Bacika71754f2013-06-17 17:14:39 -04004975 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304976 * If our size started in the middle of a block we need to zero out the
4977 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004978 * expose stale data.
4979 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304980 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004981 if (err)
4982 return err;
4983
Yan Zheng9036c102008-10-30 14:19:41 -04004984 if (size <= hole_start)
4985 return 0;
4986
Yan Zheng9036c102008-10-30 14:19:41 -04004987 while (1) {
4988 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004989
David Sterbaff13db42015-12-03 14:30:40 +01004990 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004991 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004992 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004993 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004994 if (!ordered)
4995 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004996 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004997 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004998 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004999 btrfs_put_ordered_extent(ordered);
5000 }
5001
Yan Zheng9036c102008-10-30 14:19:41 -04005002 cur_offset = hole_start;
5003 while (1) {
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02005004 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005005 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005006 if (IS_ERR(em)) {
5007 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005008 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005009 break;
5010 }
Yan Zheng9036c102008-10-30 14:19:41 -04005011 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005012 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005013 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005014 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005015 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005016
Josef Bacik16e75492013-10-22 12:18:51 -04005017 err = maybe_insert_hole(root, inode, cur_offset,
5018 hole_size);
5019 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005020 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005021 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005022 cur_offset + hole_size - 1, 0);
5023 hole_em = alloc_extent_map();
5024 if (!hole_em) {
5025 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5026 &BTRFS_I(inode)->runtime_flags);
5027 goto next;
5028 }
5029 hole_em->start = cur_offset;
5030 hole_em->len = hole_size;
5031 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005032
Josef Bacik5dc562c2012-08-17 13:14:17 -04005033 hole_em->block_start = EXTENT_MAP_HOLE;
5034 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005035 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005036 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005037 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005038 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005039 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005040
5041 while (1) {
5042 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005043 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005044 write_unlock(&em_tree->lock);
5045 if (err != -EEXIST)
5046 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005047 btrfs_drop_extent_cache(BTRFS_I(inode),
5048 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005049 cur_offset +
5050 hole_size - 1, 0);
5051 }
5052 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005053 }
Josef Bacik16e75492013-10-22 12:18:51 -04005054next:
Yan Zheng9036c102008-10-30 14:19:41 -04005055 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005056 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005057 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005058 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005059 break;
5060 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005061 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005062 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005063 return err;
5064}
5065
Eric Sandeen3972f262013-01-12 02:57:22 +00005066static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005067{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005068 struct btrfs_root *root = BTRFS_I(inode)->root;
5069 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005070 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005071 loff_t newsize = attr->ia_size;
5072 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005073 int ret;
5074
Eric Sandeen3972f262013-01-12 02:57:22 +00005075 /*
5076 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5077 * special case where we need to update the times despite not having
5078 * these flags set. For all other operations the VFS set these flags
5079 * explicitly if it wants a timestamp update.
5080 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005081 if (newsize != oldsize) {
5082 inode_inc_iversion(inode);
5083 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5084 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005085 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005086 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005087
Josef Bacika41ad392011-01-31 15:30:16 -05005088 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005089 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005090 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005091 * This is to ensure the snapshot captures a fully consistent
5092 * state of this file - if the snapshot captures this expanding
5093 * truncation, it must capture all writes that happened before
5094 * this truncation.
5095 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005096 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005097 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005098 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005099 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005100 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005101 }
Yan, Zheng80825102009-11-12 09:35:36 +00005102
Miao Xief4a2f4c2011-12-14 20:12:01 -05005103 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005104 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005105 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005106 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005107 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005108
5109 i_size_write(inode, newsize);
5110 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305111 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005112 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005113 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005114 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005115 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005116
Josef Bacika41ad392011-01-31 15:30:16 -05005117 /*
5118 * We're truncating a file that used to have good data down to
5119 * zero. Make sure it gets into the ordered flush list so that
5120 * any new writes get down to disk quickly.
5121 */
5122 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005123 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5124 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005125
Josef Bacikf3fe8202013-01-07 17:03:21 -05005126 /*
5127 * 1 for the orphan item we're going to add
5128 * 1 for the orphan item deletion.
5129 */
5130 trans = btrfs_start_transaction(root, 2);
5131 if (IS_ERR(trans))
5132 return PTR_ERR(trans);
5133
5134 /*
5135 * We need to do this in case we fail at _any_ point during the
5136 * actual truncate. Once we do the truncate_setsize we could
5137 * invalidate pages which forces any outstanding ordered io to
5138 * be instantly completed which will give us extents that need
5139 * to be truncated. If we fail to get an orphan inode down we
5140 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005141 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005142 * will be consistent.
5143 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005144 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005145 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005146 if (ret)
5147 return ret;
5148
Josef Bacika41ad392011-01-31 15:30:16 -05005149 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5150 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005151
5152 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005153 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005154 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005155 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005156
Josef Bacika41ad392011-01-31 15:30:16 -05005157 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005158 if (ret && inode->i_nlink) {
5159 int err;
5160
Liu Bo19fd2df2016-12-01 13:01:02 -08005161 /* To get a stable disk_i_size */
5162 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5163 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005164 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005165 return err;
5166 }
5167
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005168 /*
5169 * failed to truncate, disk_i_size is only adjusted down
5170 * as we remove extents, so it should represent the true
5171 * size of the inode, so reset the in memory size and
5172 * delete our orphan entry.
5173 */
5174 trans = btrfs_join_transaction(root);
5175 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005176 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005177 return ret;
5178 }
5179 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005180 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005181 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005182 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005183 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005184 }
Yan, Zheng80825102009-11-12 09:35:36 +00005185 }
5186
Josef Bacika41ad392011-01-31 15:30:16 -05005187 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005188}
5189
Chris Mason39279cc2007-06-12 06:35:45 -04005190static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5191{
David Howells2b0143b2015-03-17 22:25:59 +00005192 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005193 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005194 int err;
5195
Li Zefanb83cc962010-12-20 16:04:08 +08005196 if (btrfs_root_readonly(root))
5197 return -EROFS;
5198
Jan Kara31051c82016-05-26 16:55:18 +02005199 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005200 if (err)
5201 return err;
5202
Chris Mason5a3f23d2009-03-31 13:27:11 -04005203 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005204 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005205 if (err)
5206 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005207 }
Yan Zheng9036c102008-10-30 14:19:41 -04005208
Christoph Hellwig10257742010-06-04 11:30:02 +02005209 if (attr->ia_valid) {
5210 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005211 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005212 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005213
Josef Bacik22c44fe2011-11-30 10:45:38 -05005214 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005215 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005216 }
5217
Chris Mason39279cc2007-06-12 06:35:45 -04005218 return err;
5219}
Chris Mason61295eb2008-01-14 16:24:38 -05005220
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005221/*
5222 * While truncating the inode pages during eviction, we get the VFS calling
5223 * btrfs_invalidatepage() against each page of the inode. This is slow because
5224 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5225 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5226 * extent_state structures over and over, wasting lots of time.
5227 *
5228 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5229 * those expensive operations on a per page basis and do only the ordered io
5230 * finishing, while we release here the extent_map and extent_state structures,
5231 * without the excessive merging and splitting.
5232 */
5233static void evict_inode_truncate_pages(struct inode *inode)
5234{
5235 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5236 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5237 struct rb_node *node;
5238
5239 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005240 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005241
5242 write_lock(&map_tree->lock);
5243 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5244 struct extent_map *em;
5245
5246 node = rb_first(&map_tree->map);
5247 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005248 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5249 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005250 remove_extent_mapping(map_tree, em);
5251 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005252 if (need_resched()) {
5253 write_unlock(&map_tree->lock);
5254 cond_resched();
5255 write_lock(&map_tree->lock);
5256 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005257 }
5258 write_unlock(&map_tree->lock);
5259
Filipe Manana6ca07092015-05-26 00:55:42 +01005260 /*
5261 * Keep looping until we have no more ranges in the io tree.
5262 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005263 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5264 * still in progress (unlocked the pages in the bio but did not yet
5265 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005266 * ranges can still be locked and eviction started because before
5267 * submitting those bios, which are executed by a separate task (work
5268 * queue kthread), inode references (inode->i_count) were not taken
5269 * (which would be dropped in the end io callback of each bio).
5270 * Therefore here we effectively end up waiting for those bios and
5271 * anyone else holding locked ranges without having bumped the inode's
5272 * reference count - if we don't do it, when they access the inode's
5273 * io_tree to unlock a range it may be too late, leading to an
5274 * use-after-free issue.
5275 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005276 spin_lock(&io_tree->lock);
5277 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5278 struct extent_state *state;
5279 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005280 u64 start;
5281 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005282
5283 node = rb_first(&io_tree->state);
5284 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005285 start = state->start;
5286 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005287 spin_unlock(&io_tree->lock);
5288
David Sterbaff13db42015-12-03 14:30:40 +01005289 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005290
5291 /*
5292 * If still has DELALLOC flag, the extent didn't reach disk,
5293 * and its reserved space won't be freed by delayed_ref.
5294 * So we need to free its reserved space here.
5295 * (Refer to comment in btrfs_invalidatepage, case 2)
5296 *
5297 * Note, end is the bytenr of last byte, so we need + 1 here.
5298 */
5299 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005300 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005301
Filipe Manana6ca07092015-05-26 00:55:42 +01005302 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005303 EXTENT_LOCKED | EXTENT_DIRTY |
5304 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005305 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005306
Filipe Manana7064dd52014-08-08 02:47:05 +01005307 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005308 spin_lock(&io_tree->lock);
5309 }
5310 spin_unlock(&io_tree->lock);
5311}
5312
Al Virobd555972010-06-07 11:35:40 -04005313void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005314{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005315 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005316 struct btrfs_trans_handle *trans;
5317 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005318 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005319 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005320 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005321 int ret;
5322
liubo1abe9b82011-03-24 11:18:59 +00005323 trace_btrfs_inode_evict(inode);
5324
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005325 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005326 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005327 return;
5328 }
5329
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005330 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005331
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005332 evict_inode_truncate_pages(inode);
5333
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005334 if (inode->i_nlink &&
5335 ((btrfs_root_refs(&root->root_item) != 0 &&
5336 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005337 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005338 goto no_delete;
5339
Chris Mason39279cc2007-06-12 06:35:45 -04005340 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005341 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005342 goto no_delete;
5343 }
Al Virobd555972010-06-07 11:35:40 -04005344 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005345 if (!special_file(inode->i_mode))
5346 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005347
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005348 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005349
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005350 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005351 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005352 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005353 goto no_delete;
5354 }
5355
Yan, Zheng76dda932009-09-21 16:00:26 -04005356 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005357 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5358 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005359 goto no_delete;
5360 }
5361
Nikolay Borisovaa790212017-01-10 20:35:40 +02005362 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005363 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005364 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005365 goto no_delete;
5366 }
5367
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005368 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005369 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005370 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005371 goto no_delete;
5372 }
Josef Bacik4a338542011-08-29 11:01:31 -04005373 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005374 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005375 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005376
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005377 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005378
Josef Bacik4289a662011-08-05 13:22:24 -04005379 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005380 * This is a bit simpler than btrfs_truncate since we've already
5381 * reserved our space for our orphan item in the unlink, so we just
5382 * need to reserve some slack space in case we add bytes and update
5383 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005384 */
Yan, Zheng80825102009-11-12 09:35:36 +00005385 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005386 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5387 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005388
Josef Bacik726c35f2011-09-26 15:46:06 -04005389 /*
5390 * Try and steal from the global reserve since we will
5391 * likely not use this space anyway, we want to try as
5392 * hard as possible to get this to work.
5393 */
5394 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005395 steal_from_global++;
5396 else
5397 steal_from_global = 0;
5398 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005399
Josef Bacik3bce8762015-02-24 12:35:51 -08005400 /*
5401 * steal_from_global == 0: we reserved stuff, hooray!
5402 * steal_from_global == 1: we didn't reserve stuff, boo!
5403 * steal_from_global == 2: we've committed, still not a lot of
5404 * room but maybe we'll have room in the global reserve this
5405 * time.
5406 * steal_from_global == 3: abandon all hope!
5407 */
5408 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005409 btrfs_warn(fs_info,
5410 "Could not get space for a delete, will truncate on mount %d",
5411 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005412 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005413 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005414 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005415 }
5416
Miao Xie0e8c36a2012-12-19 06:59:51 +00005417 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005418 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005419 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005420 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005421 goto no_delete;
5422 }
5423
Josef Bacik3bce8762015-02-24 12:35:51 -08005424 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005425 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005426 * sure there is room to do it, if not we need to commit and try
5427 * again.
5428 */
5429 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005430 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005431 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005432 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005433 else
5434 ret = -ENOSPC;
5435 }
5436
5437 /*
5438 * Couldn't steal from the global reserve, we have too much
5439 * pending stuff built up, commit the transaction and try it
5440 * again.
5441 */
5442 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005443 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005444 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005445 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005446 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005447 goto no_delete;
5448 }
5449 continue;
5450 } else {
5451 steal_from_global = 0;
5452 }
5453
Josef Bacik4289a662011-08-05 13:22:24 -04005454 trans->block_rsv = rsv;
5455
Yan, Zhengd68fc572010-05-16 10:49:58 -04005456 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005457 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005458 break;
5459
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005460 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005461 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005462 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005463 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005464 }
5465
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005466 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005467
Josef Bacik4ef31a42013-08-13 14:10:08 -04005468 /*
5469 * Errors here aren't a big deal, it just means we leave orphan items
5470 * in the tree. They will be cleaned up on the next mount.
5471 */
Yan, Zheng80825102009-11-12 09:35:36 +00005472 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005473 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005474 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005475 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005476 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005477 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005478
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005479 trans->block_rsv = &fs_info->trans_block_rsv;
5480 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005481 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005482 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005483
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005484 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005485 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005486no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005487 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005488 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005489}
5490
5491/*
5492 * this returns the key found in the dir entry in the location pointer.
5493 * If no dir entries were found, location->objectid is 0.
5494 */
5495static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5496 struct btrfs_key *location)
5497{
5498 const char *name = dentry->d_name.name;
5499 int namelen = dentry->d_name.len;
5500 struct btrfs_dir_item *di;
5501 struct btrfs_path *path;
5502 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005503 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005504
5505 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005506 if (!path)
5507 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005508
David Sterbaf85b7372017-01-20 14:54:07 +01005509 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5510 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005511 if (IS_ERR(di))
5512 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005513
David Sterbac7040052011-04-19 18:00:01 +02005514 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005515 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005516
Chris Mason5f39d392007-10-15 16:14:19 -04005517 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005518 if (location->type != BTRFS_INODE_ITEM_KEY &&
5519 location->type != BTRFS_ROOT_ITEM_KEY) {
5520 btrfs_warn(root->fs_info,
5521"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5522 __func__, name, btrfs_ino(BTRFS_I(dir)),
5523 location->objectid, location->type, location->offset);
5524 goto out_err;
5525 }
Chris Mason39279cc2007-06-12 06:35:45 -04005526out:
Chris Mason39279cc2007-06-12 06:35:45 -04005527 btrfs_free_path(path);
5528 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005529out_err:
5530 location->objectid = 0;
5531 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005532}
5533
5534/*
5535 * when we hit a tree root in a directory, the btrfs part of the inode
5536 * needs to be changed to reflect the root directory of the tree root. This
5537 * is kind of like crossing a mount point.
5538 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005539static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005540 struct inode *dir,
5541 struct dentry *dentry,
5542 struct btrfs_key *location,
5543 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005544{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005545 struct btrfs_path *path;
5546 struct btrfs_root *new_root;
5547 struct btrfs_root_ref *ref;
5548 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005549 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005550 int ret;
5551 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005552
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005553 path = btrfs_alloc_path();
5554 if (!path) {
5555 err = -ENOMEM;
5556 goto out;
5557 }
Chris Mason39279cc2007-06-12 06:35:45 -04005558
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005559 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005560 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5561 key.type = BTRFS_ROOT_REF_KEY;
5562 key.offset = location->objectid;
5563
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005564 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005565 if (ret) {
5566 if (ret < 0)
5567 err = ret;
5568 goto out;
5569 }
Chris Mason39279cc2007-06-12 06:35:45 -04005570
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005571 leaf = path->nodes[0];
5572 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005573 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005574 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5575 goto out;
5576
5577 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5578 (unsigned long)(ref + 1),
5579 dentry->d_name.len);
5580 if (ret)
5581 goto out;
5582
David Sterbab3b4aa72011-04-21 01:20:15 +02005583 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005584
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005585 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005586 if (IS_ERR(new_root)) {
5587 err = PTR_ERR(new_root);
5588 goto out;
5589 }
5590
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005591 *sub_root = new_root;
5592 location->objectid = btrfs_root_dirid(&new_root->root_item);
5593 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005594 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005595 err = 0;
5596out:
5597 btrfs_free_path(path);
5598 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005599}
5600
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601static void inode_tree_add(struct inode *inode)
5602{
5603 struct btrfs_root *root = BTRFS_I(inode)->root;
5604 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005605 struct rb_node **p;
5606 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005607 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005608 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005609
Al Viro1d3382cb2010-10-23 15:19:20 -04005610 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005611 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005612 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005613 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005614 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005615 while (*p) {
5616 parent = *p;
5617 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5618
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005619 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005620 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005621 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005622 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005623 else {
5624 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005625 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005626 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005627 RB_CLEAR_NODE(parent);
5628 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005629 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005630 }
5631 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005632 rb_link_node(new, parent, p);
5633 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005634 spin_unlock(&root->inode_lock);
5635}
5636
5637static void inode_tree_del(struct inode *inode)
5638{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005639 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005640 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005641 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005642
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005643 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005644 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005645 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005646 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005647 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005648 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005649 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005650
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005651 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005652 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005653 spin_lock(&root->inode_lock);
5654 empty = RB_EMPTY_ROOT(&root->inode_tree);
5655 spin_unlock(&root->inode_lock);
5656 if (empty)
5657 btrfs_add_dead_root(root);
5658 }
5659}
5660
Jeff Mahoney143bede2012-03-01 14:56:26 +01005661void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005662{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005663 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005664 struct rb_node *node;
5665 struct rb_node *prev;
5666 struct btrfs_inode *entry;
5667 struct inode *inode;
5668 u64 objectid = 0;
5669
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005670 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005671 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005672
5673 spin_lock(&root->inode_lock);
5674again:
5675 node = root->inode_tree.rb_node;
5676 prev = NULL;
5677 while (node) {
5678 prev = node;
5679 entry = rb_entry(node, struct btrfs_inode, rb_node);
5680
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005681 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005682 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005683 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005684 node = node->rb_right;
5685 else
5686 break;
5687 }
5688 if (!node) {
5689 while (prev) {
5690 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005691 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005692 node = prev;
5693 break;
5694 }
5695 prev = rb_next(prev);
5696 }
5697 }
5698 while (node) {
5699 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005700 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005701 inode = igrab(&entry->vfs_inode);
5702 if (inode) {
5703 spin_unlock(&root->inode_lock);
5704 if (atomic_read(&inode->i_count) > 1)
5705 d_prune_aliases(inode);
5706 /*
Al Viro45321ac2010-06-07 13:43:19 -04005707 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005708 * the inode cache when its usage count
5709 * hits zero.
5710 */
5711 iput(inode);
5712 cond_resched();
5713 spin_lock(&root->inode_lock);
5714 goto again;
5715 }
5716
5717 if (cond_resched_lock(&root->inode_lock))
5718 goto again;
5719
5720 node = rb_next(node);
5721 }
5722 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005723}
5724
Chris Masone02119d2008-09-05 16:13:11 -04005725static int btrfs_init_locked_inode(struct inode *inode, void *p)
5726{
5727 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005728 inode->i_ino = args->location->objectid;
5729 memcpy(&BTRFS_I(inode)->location, args->location,
5730 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005731 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005732 return 0;
5733}
5734
5735static int btrfs_find_actor(struct inode *inode, void *opaque)
5736{
5737 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005738 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005739 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005740}
5741
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005742static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005743 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005744 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005745{
5746 struct inode *inode;
5747 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005748 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005749
Chris Mason90d3e592014-01-09 17:28:00 -08005750 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005751 args.root = root;
5752
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005753 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005754 btrfs_init_locked_inode,
5755 (void *)&args);
5756 return inode;
5757}
5758
Balaji Rao1a54ef82008-07-21 02:01:04 +05305759/* Get an inode object given its location and corresponding root.
5760 * Returns in *is_new if the inode was read from disk
5761 */
5762struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005763 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305764{
5765 struct inode *inode;
5766
Chris Mason90d3e592014-01-09 17:28:00 -08005767 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305768 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005769 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305770
5771 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005772 int ret;
5773
5774 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005775 if (!is_bad_inode(inode)) {
5776 inode_tree_add(inode);
5777 unlock_new_inode(inode);
5778 if (new)
5779 *new = 1;
5780 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005781 unlock_new_inode(inode);
5782 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005783 ASSERT(ret < 0);
5784 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005785 }
5786 }
5787
Balaji Rao1a54ef82008-07-21 02:01:04 +05305788 return inode;
5789}
5790
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005791static struct inode *new_simple_dir(struct super_block *s,
5792 struct btrfs_key *key,
5793 struct btrfs_root *root)
5794{
5795 struct inode *inode = new_inode(s);
5796
5797 if (!inode)
5798 return ERR_PTR(-ENOMEM);
5799
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005800 BTRFS_I(inode)->root = root;
5801 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005802 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005803
5804 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005805 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005806 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005807 inode->i_fop = &simple_dir_operations;
5808 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005809 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305810 inode->i_atime = inode->i_mtime;
5811 inode->i_ctime = inode->i_mtime;
5812 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005813
5814 return inode;
5815}
5816
Chris Mason3de45862008-11-17 21:02:50 -05005817struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005818{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005819 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005820 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005821 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005822 struct btrfs_root *sub_root = root;
5823 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005824 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005825 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005826
5827 if (dentry->d_name.len > BTRFS_NAME_LEN)
5828 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005829
Jeff Layton39e3c952012-11-28 11:30:53 -05005830 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005831 if (ret < 0)
5832 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005833
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005834 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005835 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005836
5837 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005838 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005839 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005840 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005841
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005842 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005843 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005844 &location, &sub_root);
5845 if (ret < 0) {
5846 if (ret != -ENOENT)
5847 inode = ERR_PTR(ret);
5848 else
5849 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5850 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005851 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005852 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005853 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005854
Julia Lawall34d19ba2011-01-24 19:55:19 +00005855 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005856 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005857 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005858 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005859 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005860 if (ret) {
5861 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005862 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005863 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005864 }
5865
Chris Mason3de45862008-11-17 21:02:50 -05005866 return inode;
5867}
5868
Nick Pigginfe15ce42011-01-07 17:49:23 +11005869static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005870{
5871 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005872 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005873
Li Zefan848cce02012-02-21 17:04:28 +08005874 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005875 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005876
Li Zefan848cce02012-02-21 17:04:28 +08005877 if (inode) {
5878 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005879 if (btrfs_root_refs(&root->root_item) == 0)
5880 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005881
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005882 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005883 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005884 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005885 return 0;
5886}
5887
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005888static void btrfs_dentry_release(struct dentry *dentry)
5889{
Daeseok Youn944a4512014-04-14 15:37:02 +09005890 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005891}
5892
Chris Mason3de45862008-11-17 21:02:50 -05005893static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005894 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005895{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005896 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005897
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005898 inode = btrfs_lookup_dentry(dir, dentry);
5899 if (IS_ERR(inode)) {
5900 if (PTR_ERR(inode) == -ENOENT)
5901 inode = NULL;
5902 else
5903 return ERR_CAST(inode);
5904 }
5905
Al Viro41d28bc2014-10-12 22:24:21 -04005906 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005907}
5908
Miao Xie16cdcec2011-04-22 18:12:22 +08005909unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005910 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5911};
5912
Josef Bacik23b5ec72017-07-24 15:14:25 -04005913/*
5914 * All this infrastructure exists because dir_emit can fault, and we are holding
5915 * the tree lock when doing readdir. For now just allocate a buffer and copy
5916 * our information into that, and then dir_emit from the buffer. This is
5917 * similar to what NFS does, only we don't keep the buffer around in pagecache
5918 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5919 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5920 * tree lock.
5921 */
5922static int btrfs_opendir(struct inode *inode, struct file *file)
5923{
5924 struct btrfs_file_private *private;
5925
5926 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5927 if (!private)
5928 return -ENOMEM;
5929 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5930 if (!private->filldir_buf) {
5931 kfree(private);
5932 return -ENOMEM;
5933 }
5934 file->private_data = private;
5935 return 0;
5936}
5937
5938struct dir_entry {
5939 u64 ino;
5940 u64 offset;
5941 unsigned type;
5942 int name_len;
5943};
5944
5945static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5946{
5947 while (entries--) {
5948 struct dir_entry *entry = addr;
5949 char *name = (char *)(entry + 1);
5950
5951 ctx->pos = entry->offset;
5952 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5953 entry->type))
5954 return 1;
5955 addr += sizeof(struct dir_entry) + entry->name_len;
5956 ctx->pos++;
5957 }
5958 return 0;
5959}
5960
Al Viro9cdda8d2013-05-22 16:48:09 -04005961static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005962{
Al Viro9cdda8d2013-05-22 16:48:09 -04005963 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005964 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005965 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005966 struct btrfs_dir_item *di;
5967 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005968 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005969 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005970 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005971 struct list_head ins_list;
5972 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005973 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005974 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005975 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005976 char *name_ptr;
5977 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005978 int entries = 0;
5979 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005980 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005981 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005982
Al Viro9cdda8d2013-05-22 16:48:09 -04005983 if (!dir_emit_dots(file, ctx))
5984 return 0;
5985
David Woodhouse49593bf2008-08-17 17:08:36 +01005986 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005987 if (!path)
5988 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005989
Josef Bacik23b5ec72017-07-24 15:14:25 -04005990 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005991 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005992
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005993 INIT_LIST_HEAD(&ins_list);
5994 INIT_LIST_HEAD(&del_list);
5995 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005996
Josef Bacik23b5ec72017-07-24 15:14:25 -04005997again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005998 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005999 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006000 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006001
Chris Mason39279cc2007-06-12 06:35:45 -04006002 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6003 if (ret < 0)
6004 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006005
6006 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006007 struct dir_entry *entry;
6008
Chris Mason5f39d392007-10-15 16:14:19 -04006009 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006010 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006011 if (slot >= btrfs_header_nritems(leaf)) {
6012 ret = btrfs_next_leaf(root, path);
6013 if (ret < 0)
6014 goto err;
6015 else if (ret > 0)
6016 break;
6017 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006018 }
Chris Mason3de45862008-11-17 21:02:50 -05006019
Chris Mason5f39d392007-10-15 16:14:19 -04006020 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6021
6022 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006023 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006024 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006025 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006026 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006027 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006028 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006029 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006030 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006031 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006032 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6033 PAGE_SIZE) {
6034 btrfs_release_path(path);
6035 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6036 if (ret)
6037 goto nopos;
6038 addr = private->filldir_buf;
6039 entries = 0;
6040 total_len = 0;
6041 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006042 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006043
6044 entry = addr;
6045 entry->name_len = name_len;
6046 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006047 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6048 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006049 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006050 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006051 entry->ino = location.objectid;
6052 entry->offset = found_key.offset;
6053 entries++;
6054 addr += sizeof(struct dir_entry) + name_len;
6055 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006056next:
6057 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006058 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006059 btrfs_release_path(path);
6060
6061 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6062 if (ret)
6063 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006064
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006065 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006066 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006067 goto nopos;
6068
Zach Browndb62efb2013-07-11 16:19:42 -07006069 /*
6070 * Stop new entries from being returned after we return the last
6071 * entry.
6072 *
6073 * New directory entries are assigned a strictly increasing
6074 * offset. This means that new entries created during readdir
6075 * are *guaranteed* to be seen in the future by that readdir.
6076 * This has broken buggy programs which operate on names as
6077 * they're returned by readdir. Until we re-use freed offsets
6078 * we have this hack to stop new entries from being returned
6079 * under the assumption that they'll never reach this huge
6080 * offset.
6081 *
6082 * This is being careful not to overflow 32bit loff_t unless the
6083 * last entry requires it because doing so has broken 32bit apps
6084 * in the past.
6085 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006086 if (ctx->pos >= INT_MAX)
6087 ctx->pos = LLONG_MAX;
6088 else
6089 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006090nopos:
6091 ret = 0;
6092err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006093 if (put)
6094 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006095 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006096 return ret;
6097}
6098
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006099int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006100{
6101 struct btrfs_root *root = BTRFS_I(inode)->root;
6102 struct btrfs_trans_handle *trans;
6103 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006104 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006105
Josef Bacik72ac3c02012-05-23 14:13:11 -04006106 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006107 return 0;
6108
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006109 if (btrfs_fs_closing(root->fs_info) &&
6110 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006111 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006112
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006113 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006114 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006115 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006116 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006117 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006118 if (IS_ERR(trans))
6119 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006120 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006121 }
6122 return ret;
6123}
6124
6125/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006126 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006127 * inode changes. But, it is most likely to find the inode in cache.
6128 * FIXME, needs more benchmarking...there are no reasons other than performance
6129 * to keep or drop this code.
6130 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006131static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006132{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006133 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006134 struct btrfs_root *root = BTRFS_I(inode)->root;
6135 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006136 int ret;
6137
Josef Bacik72ac3c02012-05-23 14:13:11 -04006138 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006139 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006140
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006141 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006142 if (IS_ERR(trans))
6143 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006144
6145 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006146 if (ret && ret == -ENOSPC) {
6147 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006148 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006149 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006150 if (IS_ERR(trans))
6151 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006152
Chris Mason94b60442010-05-26 11:02:00 -04006153 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006154 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006155 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006156 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006157 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006158
6159 return ret;
6160}
6161
6162/*
6163 * This is a copy of file_update_time. We need this so we can return error on
6164 * ENOSPC for updating the inode in the case of file write and mmap writes.
6165 */
Josef Bacike41f9412012-03-26 09:46:47 -04006166static int btrfs_update_time(struct inode *inode, struct timespec *now,
6167 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006168{
Alexander Block2bc5565282012-06-15 09:49:33 +02006169 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006170 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006171
6172 if (btrfs_root_readonly(root))
6173 return -EROFS;
6174
Josef Bacike41f9412012-03-26 09:46:47 -04006175 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006176 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006177 if (flags & S_CTIME)
6178 inode->i_ctime = *now;
6179 if (flags & S_MTIME)
6180 inode->i_mtime = *now;
6181 if (flags & S_ATIME)
6182 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006183 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006184}
6185
Chris Masond352ac62008-09-29 15:18:18 -04006186/*
6187 * find the highest existing sequence number in a directory
6188 * and then set the in-memory index_cnt variable to reflect
6189 * free sequence numbers
6190 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006191static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006192{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006193 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006194 struct btrfs_key key, found_key;
6195 struct btrfs_path *path;
6196 struct extent_buffer *leaf;
6197 int ret;
6198
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006199 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006200 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006201 key.offset = (u64)-1;
6202
6203 path = btrfs_alloc_path();
6204 if (!path)
6205 return -ENOMEM;
6206
6207 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6208 if (ret < 0)
6209 goto out;
6210 /* FIXME: we should be able to handle this */
6211 if (ret == 0)
6212 goto out;
6213 ret = 0;
6214
6215 /*
6216 * MAGIC NUMBER EXPLANATION:
6217 * since we search a directory based on f_pos we have to start at 2
6218 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6219 * else has to start at 2
6220 */
6221 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006222 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006223 goto out;
6224 }
6225
6226 path->slots[0]--;
6227
6228 leaf = path->nodes[0];
6229 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6230
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006231 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006232 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006233 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006234 goto out;
6235 }
6236
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006237 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006238out:
6239 btrfs_free_path(path);
6240 return ret;
6241}
6242
Chris Masond352ac62008-09-29 15:18:18 -04006243/*
6244 * helper to find a free sequence number in a given directory. This current
6245 * code is very simple, later versions will do smarter things in the btree
6246 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006247int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006248{
6249 int ret = 0;
6250
Nikolay Borisov877574e2017-02-20 13:50:33 +02006251 if (dir->index_cnt == (u64)-1) {
6252 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006253 if (ret) {
6254 ret = btrfs_set_inode_index_count(dir);
6255 if (ret)
6256 return ret;
6257 }
Josef Bacikaec74772008-07-24 12:12:38 -04006258 }
6259
Nikolay Borisov877574e2017-02-20 13:50:33 +02006260 *index = dir->index_cnt;
6261 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006262
6263 return ret;
6264}
6265
Chris Masonb0d5d102014-09-08 13:08:51 -07006266static int btrfs_insert_inode_locked(struct inode *inode)
6267{
6268 struct btrfs_iget_args args;
6269 args.location = &BTRFS_I(inode)->location;
6270 args.root = BTRFS_I(inode)->root;
6271
6272 return insert_inode_locked4(inode,
6273 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6274 btrfs_find_actor, &args);
6275}
6276
Anand Jain19aee8d2017-07-18 17:37:05 +08006277/*
6278 * Inherit flags from the parent inode.
6279 *
6280 * Currently only the compression flags and the cow flags are inherited.
6281 */
6282static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6283{
6284 unsigned int flags;
6285
6286 if (!dir)
6287 return;
6288
6289 flags = BTRFS_I(dir)->flags;
6290
6291 if (flags & BTRFS_INODE_NOCOMPRESS) {
6292 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6293 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6294 } else if (flags & BTRFS_INODE_COMPRESS) {
6295 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6296 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6297 }
6298
6299 if (flags & BTRFS_INODE_NODATACOW) {
6300 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6301 if (S_ISREG(inode->i_mode))
6302 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6303 }
6304
6305 btrfs_update_iflags(inode);
6306}
6307
Chris Mason39279cc2007-06-12 06:35:45 -04006308static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6309 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006310 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006311 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006312 u64 ref_objectid, u64 objectid,
6313 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006314{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006315 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006316 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006317 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006318 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006319 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006320 struct btrfs_inode_ref *ref;
6321 struct btrfs_key key[2];
6322 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006323 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006324 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006325 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006326
Chris Mason5f39d392007-10-15 16:14:19 -04006327 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006328 if (!path)
6329 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006330
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006331 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006332 if (!inode) {
6333 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006334 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006335 }
Chris Mason39279cc2007-06-12 06:35:45 -04006336
Li Zefan581bb052011-04-20 10:06:11 +08006337 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006338 * O_TMPFILE, set link count to 0, so that after this point,
6339 * we fill in an inode item with the correct link count.
6340 */
6341 if (!name)
6342 set_nlink(inode, 0);
6343
6344 /*
Li Zefan581bb052011-04-20 10:06:11 +08006345 * we have to initialize this early, so we can reclaim the inode
6346 * number if we fail afterwards in this function.
6347 */
6348 inode->i_ino = objectid;
6349
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006350 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006351 trace_btrfs_inode_request(dir);
6352
Nikolay Borisov877574e2017-02-20 13:50:33 +02006353 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006354 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006355 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006356 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006357 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006358 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006359 } else if (dir) {
6360 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006361 }
6362 /*
6363 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006364 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006365 * number
6366 */
6367 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006368 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006369 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006370 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006371 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006372
Josef Bacik5dc562c2012-08-17 13:14:17 -04006373 /*
6374 * We could have gotten an inode number from somebody who was fsynced
6375 * and then removed in this same transaction, so let's just set full
6376 * sync since it will be a full sync anyway and this will blow away the
6377 * old info in the log.
6378 */
6379 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6380
Chris Mason9c583092008-01-29 15:15:18 -05006381 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006382 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006383 key[0].offset = 0;
6384
Chris Mason9c583092008-01-29 15:15:18 -05006385 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006386
6387 if (name) {
6388 /*
6389 * Start new inodes with an inode_ref. This is slightly more
6390 * efficient for small numbers of hard links since they will
6391 * be packed into one item. Extended refs will kick in if we
6392 * add more hard links than can fit in the ref item.
6393 */
6394 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006395 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006396 key[1].offset = ref_objectid;
6397
6398 sizes[1] = name_len + sizeof(*ref);
6399 }
Chris Mason9c583092008-01-29 15:15:18 -05006400
Chris Masonb0d5d102014-09-08 13:08:51 -07006401 location = &BTRFS_I(inode)->location;
6402 location->objectid = objectid;
6403 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006404 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006405
6406 ret = btrfs_insert_inode_locked(inode);
6407 if (ret < 0)
6408 goto fail;
6409
Chris Masonb9473432009-03-13 11:00:37 -04006410 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006411 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006412 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006413 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006414
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006415 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006416 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306417
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006418 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306419 inode->i_atime = inode->i_mtime;
6420 inode->i_ctime = inode->i_mtime;
6421 BTRFS_I(inode)->i_otime = inode->i_mtime;
6422
Chris Mason5f39d392007-10-15 16:14:19 -04006423 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6424 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006425 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006426 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006427 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006428
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006429 if (name) {
6430 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6431 struct btrfs_inode_ref);
6432 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6433 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6434 ptr = (unsigned long)(ref + 1);
6435 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6436 }
Chris Mason9c583092008-01-29 15:15:18 -05006437
Chris Mason5f39d392007-10-15 16:14:19 -04006438 btrfs_mark_buffer_dirty(path->nodes[0]);
6439 btrfs_free_path(path);
6440
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006441 btrfs_inherit_iflags(inode, dir);
6442
Al Viro569254b2011-07-24 17:08:40 -04006443 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006444 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006445 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006446 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006447 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6448 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006449 }
6450
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006451 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006452
6453 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006454 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006455
Alexander Block8ea05e32012-07-25 17:35:53 +02006456 btrfs_update_root_times(trans, root);
6457
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006458 ret = btrfs_inode_inherit_props(trans, inode, dir);
6459 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006460 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006461 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006462 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006463
Chris Mason39279cc2007-06-12 06:35:45 -04006464 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006465
6466fail_unlock:
6467 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006468fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006469 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006470 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006471 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006472 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006473 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006474}
6475
6476static inline u8 btrfs_inode_type(struct inode *inode)
6477{
6478 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6479}
6480
Chris Masond352ac62008-09-29 15:18:18 -04006481/*
6482 * utility function to add 'inode' into 'parent_inode' with
6483 * a give name and a given sequence number.
6484 * if 'add_backref' is true, also insert a backref from the
6485 * inode to the parent directory.
6486 */
Chris Masone02119d2008-09-05 16:13:11 -04006487int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006488 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006489 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006490{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006491 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006492 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006493 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006494 struct btrfs_root *root = parent_inode->root;
6495 u64 ino = btrfs_ino(inode);
6496 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006497
Li Zefan33345d012011-04-20 10:31:50 +08006498 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006499 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006500 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006501 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006502 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006503 key.offset = 0;
6504 }
Chris Mason39279cc2007-06-12 06:35:45 -04006505
Li Zefan33345d012011-04-20 10:31:50 +08006506 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006507 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6508 root->root_key.objectid, parent_ino,
6509 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006510 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006511 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6512 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006513 }
6514
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006515 /* Nothing to clean up yet */
6516 if (ret)
6517 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006518
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006519 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6520 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006521 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006522 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006523 goto fail_dir_item;
6524 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006525 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006526 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006527 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006528
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006529 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006530 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006531 inode_inc_iversion(&parent_inode->vfs_inode);
6532 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6533 current_time(&parent_inode->vfs_inode);
6534 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006535 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006536 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006537 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006538
6539fail_dir_item:
6540 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6541 u64 local_index;
6542 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006543 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6544 root->root_key.objectid, parent_ino,
6545 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006546
6547 } else if (add_backref) {
6548 u64 local_index;
6549 int err;
6550
6551 err = btrfs_del_inode_ref(trans, root, name, name_len,
6552 ino, parent_ino, &local_index);
6553 }
6554 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006555}
6556
6557static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006558 struct btrfs_inode *dir, struct dentry *dentry,
6559 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006560{
Josef Bacika1b075d2010-11-19 20:36:11 +00006561 int err = btrfs_add_link(trans, dir, inode,
6562 dentry->d_name.name, dentry->d_name.len,
6563 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006564 if (err > 0)
6565 err = -EEXIST;
6566 return err;
6567}
6568
Josef Bacik618e21d2007-07-11 10:18:17 -04006569static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006570 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006571{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006572 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006573 struct btrfs_trans_handle *trans;
6574 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006575 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006576 int err;
6577 int drop_inode = 0;
6578 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006579 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006580
Josef Bacik9ed74f22009-09-11 16:12:44 -04006581 /*
6582 * 2 for inode item and ref
6583 * 2 for dir items
6584 * 1 for xattr if selinux is on
6585 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006586 trans = btrfs_start_transaction(root, 5);
6587 if (IS_ERR(trans))
6588 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006589
Li Zefan581bb052011-04-20 10:06:11 +08006590 err = btrfs_find_free_ino(root, &objectid);
6591 if (err)
6592 goto out_unlock;
6593
Josef Bacikaec74772008-07-24 12:12:38 -04006594 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006595 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6596 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006597 if (IS_ERR(inode)) {
6598 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006599 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006600 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006601
Casey Schauflerad19db72011-12-15 10:09:07 -05006602 /*
6603 * If the active LSM wants to access the inode during
6604 * d_instantiate it needs these. Smack checks to see
6605 * if the filesystem supports xattrs by looking at the
6606 * ops vector.
6607 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006608 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006609 init_special_inode(inode, inode->i_mode, rdev);
6610
6611 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006612 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006613 goto out_unlock_inode;
6614
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006615 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6616 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006617 if (err) {
6618 goto out_unlock_inode;
6619 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006620 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006621 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006622 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006623 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006624
Josef Bacik618e21d2007-07-11 10:18:17 -04006625out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006626 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006627 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006628 if (drop_inode) {
6629 inode_dec_link_count(inode);
6630 iput(inode);
6631 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006632 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006633
6634out_unlock_inode:
6635 drop_inode = 1;
6636 unlock_new_inode(inode);
6637 goto out_unlock;
6638
Josef Bacik618e21d2007-07-11 10:18:17 -04006639}
6640
Chris Mason39279cc2007-06-12 06:35:45 -04006641static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006642 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006643{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006644 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006645 struct btrfs_trans_handle *trans;
6646 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006647 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006648 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006649 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006650 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006651 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006652
Josef Bacik9ed74f22009-09-11 16:12:44 -04006653 /*
6654 * 2 for inode item and ref
6655 * 2 for dir items
6656 * 1 for xattr if selinux is on
6657 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006658 trans = btrfs_start_transaction(root, 5);
6659 if (IS_ERR(trans))
6660 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006661
Li Zefan581bb052011-04-20 10:06:11 +08006662 err = btrfs_find_free_ino(root, &objectid);
6663 if (err)
6664 goto out_unlock;
6665
Josef Bacikaec74772008-07-24 12:12:38 -04006666 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006667 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6668 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006669 if (IS_ERR(inode)) {
6670 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006671 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006672 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006673 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006674 /*
6675 * If the active LSM wants to access the inode during
6676 * d_instantiate it needs these. Smack checks to see
6677 * if the filesystem supports xattrs by looking at the
6678 * ops vector.
6679 */
6680 inode->i_fop = &btrfs_file_operations;
6681 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006682 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006683
6684 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6685 if (err)
6686 goto out_unlock_inode;
6687
6688 err = btrfs_update_inode(trans, root, inode);
6689 if (err)
6690 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006691
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006692 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6693 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006694 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006695 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006696
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006697 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006698 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006699 d_instantiate(dentry, inode);
6700
Chris Mason39279cc2007-06-12 06:35:45 -04006701out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006702 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006703 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006704 inode_dec_link_count(inode);
6705 iput(inode);
6706 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006707 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006708 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006709
6710out_unlock_inode:
6711 unlock_new_inode(inode);
6712 goto out_unlock;
6713
Chris Mason39279cc2007-06-12 06:35:45 -04006714}
6715
6716static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6717 struct dentry *dentry)
6718{
Filipe Manana271dba42016-01-05 16:24:05 +00006719 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006720 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006721 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006722 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006723 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006724 int err;
6725 int drop_inode = 0;
6726
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006727 /* do not allow sys_link's with other subvols of the same device */
6728 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006729 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006730
Mark Fashehf1863732012-08-08 11:32:27 -07006731 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006732 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006733
Nikolay Borisov877574e2017-02-20 13:50:33 +02006734 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006735 if (err)
6736 goto fail;
6737
Yan, Zhenga22285a2010-05-16 10:48:46 -04006738 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006739 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006740 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006741 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006742 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006743 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006744 if (IS_ERR(trans)) {
6745 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006746 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006747 goto fail;
6748 }
Chris Mason5f39d392007-10-15 16:14:19 -04006749
Miao Xie67de1172013-12-26 13:07:06 +08006750 /* There are several dir indexes for this inode, clear the cache. */
6751 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006752 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006753 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006754 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006755 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006756 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006757
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006758 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6759 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006760
Yan, Zhenga5719522009-09-24 09:17:31 -04006761 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006762 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006763 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006764 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006765 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006766 if (err)
6767 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006768 if (inode->i_nlink == 1) {
6769 /*
6770 * If new hard link count is 1, it's a file created
6771 * with open(2) O_TMPFILE flag.
6772 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006773 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006774 if (err)
6775 goto fail;
6776 }
Al Viro08c422c2011-12-23 07:58:13 -05006777 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006778 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006779 }
Chris Mason39279cc2007-06-12 06:35:45 -04006780
Chris Mason1832a6d2007-12-21 16:27:21 -05006781fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006782 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006783 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006784 if (drop_inode) {
6785 inode_dec_link_count(inode);
6786 iput(inode);
6787 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006788 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006789 return err;
6790}
6791
Al Viro18bb1db2011-07-26 01:41:39 -04006792static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006793{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006794 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006795 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006796 struct btrfs_trans_handle *trans;
6797 struct btrfs_root *root = BTRFS_I(dir)->root;
6798 int err = 0;
6799 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006800 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006801 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006802
Josef Bacik9ed74f22009-09-11 16:12:44 -04006803 /*
6804 * 2 items for inode and ref
6805 * 2 items for dir items
6806 * 1 for xattr if selinux is on
6807 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006808 trans = btrfs_start_transaction(root, 5);
6809 if (IS_ERR(trans))
6810 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006811
Li Zefan581bb052011-04-20 10:06:11 +08006812 err = btrfs_find_free_ino(root, &objectid);
6813 if (err)
6814 goto out_fail;
6815
Josef Bacikaec74772008-07-24 12:12:38 -04006816 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006817 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6818 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006819 if (IS_ERR(inode)) {
6820 err = PTR_ERR(inode);
6821 goto out_fail;
6822 }
Chris Mason5f39d392007-10-15 16:14:19 -04006823
Chris Mason39279cc2007-06-12 06:35:45 -04006824 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006825 /* these must be set before we unlock the inode */
6826 inode->i_op = &btrfs_dir_inode_operations;
6827 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006828
Eric Paris2a7dba32011-02-01 11:05:39 -05006829 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006830 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006831 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006832
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006833 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006834 err = btrfs_update_inode(trans, root, inode);
6835 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006836 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006837
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006838 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6839 dentry->d_name.name,
6840 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006841 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006842 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006843
Chris Mason39279cc2007-06-12 06:35:45 -04006844 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006845 /*
6846 * mkdir is special. We're unlocking after we call d_instantiate
6847 * to avoid a race with nfsd calling d_instantiate.
6848 */
6849 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006850 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006851
6852out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006853 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006854 if (drop_on_err) {
6855 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006856 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006857 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006858 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006859 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006860
6861out_fail_inode:
6862 unlock_new_inode(inode);
6863 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006864}
6865
Chris Masonc8b97812008-10-29 14:49:59 -04006866static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006867 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006868 size_t pg_offset, u64 extent_offset,
6869 struct btrfs_file_extent_item *item)
6870{
6871 int ret;
6872 struct extent_buffer *leaf = path->nodes[0];
6873 char *tmp;
6874 size_t max_size;
6875 unsigned long inline_size;
6876 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006877 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006878
6879 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006880 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006881 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6882 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006883 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006884 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006885 if (!tmp)
6886 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006887 ptr = btrfs_file_extent_inline_start(item);
6888
6889 read_extent_buffer(leaf, tmp, ptr, inline_size);
6890
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006891 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006892 ret = btrfs_decompress(compress_type, tmp, page,
6893 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006894
6895 /*
6896 * decompression code contains a memset to fill in any space between the end
6897 * of the uncompressed data and the end of max_size in case the decompressed
6898 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6899 * the end of an inline extent and the beginning of the next block, so we
6900 * cover that region here.
6901 */
6902
6903 if (max_size + pg_offset < PAGE_SIZE) {
6904 char *map = kmap(page);
6905 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6906 kunmap(page);
6907 }
Chris Masonc8b97812008-10-29 14:49:59 -04006908 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006909 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006910}
6911
Chris Masond352ac62008-09-29 15:18:18 -04006912/*
6913 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006914 * the ugly parts come from merging extents from the disk with the in-ram
6915 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006916 * where the in-ram extents might be locked pending data=ordered completion.
6917 *
6918 * This also copies inline extents directly into the page.
6919 */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006920struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6921 struct page *page,
6922 size_t pg_offset, u64 start, u64 len,
6923 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006924{
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006925 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006926 int ret;
6927 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006928 u64 extent_start = 0;
6929 u64 extent_end = 0;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006930 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006931 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006932 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006933 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006934 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006935 struct extent_buffer *leaf;
6936 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006937 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006938 struct extent_map_tree *em_tree = &inode->extent_tree;
6939 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006940 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006941
Chris Mason890871b2009-09-02 16:24:52 -04006942 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006943 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006944 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006945 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006946 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006947
Chris Masona52d9a82007-08-27 16:49:44 -04006948 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006949 if (em->start > start || em->start + em->len <= start)
6950 free_extent_map(em);
6951 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006952 free_extent_map(em);
6953 else
6954 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006955 }
David Sterba172ddd62011-04-21 00:48:27 +02006956 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006957 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006958 err = -ENOMEM;
6959 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006960 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006961 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006962 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006963 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006964 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006965 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006966
6967 if (!path) {
6968 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006969 if (!path) {
6970 err = -ENOMEM;
6971 goto out;
6972 }
6973 /*
6974 * Chances are we'll be called again, so go ahead and do
6975 * readahead
6976 */
David Sterbae4058b52015-11-27 16:31:35 +01006977 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006978 }
6979
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006980 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006981 if (ret < 0) {
6982 err = ret;
6983 goto out;
6984 }
6985
6986 if (ret != 0) {
6987 if (path->slots[0] == 0)
6988 goto not_found;
6989 path->slots[0]--;
6990 }
6991
Chris Mason5f39d392007-10-15 16:14:19 -04006992 leaf = path->nodes[0];
6993 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006994 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006995 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006996 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006997 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006998 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006999 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007000 /*
7001 * If we backup past the first extent we want to move forward
7002 * and see if there is an extent in front of us, otherwise we'll
7003 * say there is a hole for our whole search range which can
7004 * cause problems.
7005 */
7006 extent_end = start;
7007 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007008 }
7009
Chris Mason5f39d392007-10-15 16:14:19 -04007010 found_type = btrfs_file_extent_type(leaf, item);
7011 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007012 if (found_type == BTRFS_FILE_EXTENT_REG ||
7013 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007014 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007015 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007016
7017 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7018 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007019 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7020 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007021 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007022 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007023 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007024
7025 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7026 path->slots[0],
7027 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007028 }
Josef Bacik25a50342013-10-14 12:08:38 -04007029next:
Yan Zheng9036c102008-10-30 14:19:41 -04007030 if (start >= extent_end) {
7031 path->slots[0]++;
7032 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7033 ret = btrfs_next_leaf(root, path);
7034 if (ret < 0) {
7035 err = ret;
7036 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007037 }
Yan Zheng9036c102008-10-30 14:19:41 -04007038 if (ret > 0)
7039 goto not_found;
7040 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007041 }
Yan Zheng9036c102008-10-30 14:19:41 -04007042 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7043 if (found_key.objectid != objectid ||
7044 found_key.type != BTRFS_EXTENT_DATA_KEY)
7045 goto not_found;
7046 if (start + len <= found_key.offset)
7047 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007048 if (start > found_key.offset)
7049 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007050 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007051 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007052 em->len = found_key.offset - start;
7053 goto not_found_em;
7054 }
7055
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007056 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007057 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007058
Yan Zhengd899e052008-10-30 14:25:28 -04007059 if (found_type == BTRFS_FILE_EXTENT_REG ||
7060 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007061 goto insert;
7062 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007063 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007064 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007065 size_t size;
7066 size_t extent_offset;
7067 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007068
Filipe Manana7ffbb592014-06-09 03:48:05 +01007069 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007070 goto out;
Yan689f9342007-10-29 11:41:07 -04007071
Chris Mason514ac8a2014-01-03 21:07:00 -08007072 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007073 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007074 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7075 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007076 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007077 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007078 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007079 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007080 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007081 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007082 if (btrfs_file_extent_compression(leaf, item) !=
7083 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007084 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007085 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007086 if (ret) {
7087 err = ret;
7088 goto out;
7089 }
Chris Masonc8b97812008-10-29 14:49:59 -04007090 } else {
7091 map = kmap(page);
7092 read_extent_buffer(leaf, map + pg_offset, ptr,
7093 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007094 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007095 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007096 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007097 copy_size);
7098 }
Chris Masonc8b97812008-10-29 14:49:59 -04007099 kunmap(page);
7100 }
Chris Mason179e29e2007-11-01 11:28:41 -04007101 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007102 }
Chris Masond1310b22008-01-24 16:13:08 -05007103 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007104 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007105 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007106 }
7107not_found:
7108 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007109 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007110 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007111not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007112 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007113insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007114 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007115 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007116 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007117 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7118 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007119 err = -EIO;
7120 goto out;
7121 }
Chris Masond1310b22008-01-24 16:13:08 -05007122
7123 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007124 write_lock(&em_tree->lock);
Liu Bo7b4df0582018-01-05 12:51:10 -07007125 err = btrfs_add_extent_mapping(em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007126 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007127out:
liubo1abe9b82011-03-24 11:18:59 +00007128
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007129 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007130
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007131 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007132 if (err) {
7133 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007134 return ERR_PTR(err);
7135 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007136 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007137 return em;
7138}
7139
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007140struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7141 struct page *page,
7142 size_t pg_offset, u64 start, u64 len,
7143 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007144{
7145 struct extent_map *em;
7146 struct extent_map *hole_em = NULL;
7147 u64 range_start = start;
7148 u64 end;
7149 u64 found;
7150 u64 found_end;
7151 int err = 0;
7152
7153 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7154 if (IS_ERR(em))
7155 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007156 /*
7157 * If our em maps to:
7158 * - a hole or
7159 * - a pre-alloc extent,
7160 * there might actually be delalloc bytes behind it.
7161 */
7162 if (em->block_start != EXTENT_MAP_HOLE &&
7163 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7164 return em;
7165 else
7166 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007167
7168 /* check to see if we've wrapped (len == -1 or similar) */
7169 end = start + len;
7170 if (end < start)
7171 end = (u64)-1;
7172 else
7173 end -= 1;
7174
7175 em = NULL;
7176
7177 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007178 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007179 end, len, EXTENT_DELALLOC, 1);
7180 found_end = range_start + found;
7181 if (found_end < range_start)
7182 found_end = (u64)-1;
7183
7184 /*
7185 * we didn't find anything useful, return
7186 * the original results from get_extent()
7187 */
7188 if (range_start > end || found_end <= start) {
7189 em = hole_em;
7190 hole_em = NULL;
7191 goto out;
7192 }
7193
7194 /* adjust the range_start to make sure it doesn't
7195 * go backwards from the start they passed in
7196 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307197 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007198 found = found_end - range_start;
7199
7200 if (found > 0) {
7201 u64 hole_start = start;
7202 u64 hole_len = len;
7203
David Sterba172ddd62011-04-21 00:48:27 +02007204 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007205 if (!em) {
7206 err = -ENOMEM;
7207 goto out;
7208 }
7209 /*
7210 * when btrfs_get_extent can't find anything it
7211 * returns one huge hole
7212 *
7213 * make sure what it found really fits our range, and
7214 * adjust to make sure it is based on the start from
7215 * the caller
7216 */
7217 if (hole_em) {
7218 u64 calc_end = extent_map_end(hole_em);
7219
7220 if (calc_end <= start || (hole_em->start > end)) {
7221 free_extent_map(hole_em);
7222 hole_em = NULL;
7223 } else {
7224 hole_start = max(hole_em->start, start);
7225 hole_len = calc_end - hole_start;
7226 }
7227 }
7228 em->bdev = NULL;
7229 if (hole_em && range_start > hole_start) {
7230 /* our hole starts before our delalloc, so we
7231 * have to return just the parts of the hole
7232 * that go until the delalloc starts
7233 */
7234 em->len = min(hole_len,
7235 range_start - hole_start);
7236 em->start = hole_start;
7237 em->orig_start = hole_start;
7238 /*
7239 * don't adjust block start at all,
7240 * it is fixed at EXTENT_MAP_HOLE
7241 */
7242 em->block_start = hole_em->block_start;
7243 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007244 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7245 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007246 } else {
7247 em->start = range_start;
7248 em->len = found;
7249 em->orig_start = range_start;
7250 em->block_start = EXTENT_MAP_DELALLOC;
7251 em->block_len = found;
7252 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007253 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007254 return hole_em;
7255 }
7256out:
7257
7258 free_extent_map(hole_em);
7259 if (err) {
7260 free_extent_map(em);
7261 return ERR_PTR(err);
7262 }
7263 return em;
7264}
7265
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007266static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7267 const u64 start,
7268 const u64 len,
7269 const u64 orig_start,
7270 const u64 block_start,
7271 const u64 block_len,
7272 const u64 orig_block_len,
7273 const u64 ram_bytes,
7274 const int type)
7275{
7276 struct extent_map *em = NULL;
7277 int ret;
7278
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007279 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007280 em = create_io_em(inode, start, len, orig_start,
7281 block_start, block_len, orig_block_len,
7282 ram_bytes,
7283 BTRFS_COMPRESS_NONE, /* compress_type */
7284 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007285 if (IS_ERR(em))
7286 goto out;
7287 }
7288 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7289 len, block_len, type);
7290 if (ret) {
7291 if (em) {
7292 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007293 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007294 start + len - 1, 0);
7295 }
7296 em = ERR_PTR(ret);
7297 }
7298 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007299
7300 return em;
7301}
7302
Josef Bacik4b46fce2010-05-23 11:00:55 -04007303static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7304 u64 start, u64 len)
7305{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007306 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007307 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007308 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007309 struct btrfs_key ins;
7310 u64 alloc_hint;
7311 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007312
Josef Bacik4b46fce2010-05-23 11:00:55 -04007313 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007314 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007315 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007316 if (ret)
7317 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007318
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007319 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7320 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007321 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007322 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007323 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007324 btrfs_free_reserved_extent(fs_info, ins.objectid,
7325 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007326
Josef Bacik4b46fce2010-05-23 11:00:55 -04007327 return em;
7328}
7329
Chris Mason46bfbb52010-05-26 11:04:10 -04007330/*
7331 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7332 * block must be cow'd
7333 */
Josef Bacik00361582013-08-14 14:02:47 -04007334noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007335 u64 *orig_start, u64 *orig_block_len,
7336 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007337{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007338 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007339 struct btrfs_path *path;
7340 int ret;
7341 struct extent_buffer *leaf;
7342 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007343 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007344 struct btrfs_file_extent_item *fi;
7345 struct btrfs_key key;
7346 u64 disk_bytenr;
7347 u64 backref_offset;
7348 u64 extent_end;
7349 u64 num_bytes;
7350 int slot;
7351 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007352 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007353
Chris Mason46bfbb52010-05-26 11:04:10 -04007354 path = btrfs_alloc_path();
7355 if (!path)
7356 return -ENOMEM;
7357
David Sterbaf85b7372017-01-20 14:54:07 +01007358 ret = btrfs_lookup_file_extent(NULL, root, path,
7359 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007360 if (ret < 0)
7361 goto out;
7362
7363 slot = path->slots[0];
7364 if (ret == 1) {
7365 if (slot == 0) {
7366 /* can't find the item, must cow */
7367 ret = 0;
7368 goto out;
7369 }
7370 slot--;
7371 }
7372 ret = 0;
7373 leaf = path->nodes[0];
7374 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007375 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007376 key.type != BTRFS_EXTENT_DATA_KEY) {
7377 /* not our file or wrong item type, must cow */
7378 goto out;
7379 }
7380
7381 if (key.offset > offset) {
7382 /* Wrong offset, must cow */
7383 goto out;
7384 }
7385
7386 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7387 found_type = btrfs_file_extent_type(leaf, fi);
7388 if (found_type != BTRFS_FILE_EXTENT_REG &&
7389 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7390 /* not a regular extent, must cow */
7391 goto out;
7392 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007393
7394 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7395 goto out;
7396
Miao Xiee77751a2013-12-27 21:11:50 +08007397 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7398 if (extent_end <= offset)
7399 goto out;
7400
Chris Mason46bfbb52010-05-26 11:04:10 -04007401 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007402 if (disk_bytenr == 0)
7403 goto out;
7404
7405 if (btrfs_file_extent_compression(leaf, fi) ||
7406 btrfs_file_extent_encryption(leaf, fi) ||
7407 btrfs_file_extent_other_encoding(leaf, fi))
7408 goto out;
7409
Chris Mason46bfbb52010-05-26 11:04:10 -04007410 backref_offset = btrfs_file_extent_offset(leaf, fi);
7411
Josef Bacik7ee9e442013-06-21 16:37:03 -04007412 if (orig_start) {
7413 *orig_start = key.offset - backref_offset;
7414 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7415 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7416 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007417
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007418 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007419 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007420
7421 num_bytes = min(offset + *len, extent_end) - offset;
7422 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7423 u64 range_end;
7424
Jeff Mahoneyda170662016-06-15 09:22:56 -04007425 range_end = round_up(offset + num_bytes,
7426 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007427 ret = test_range_bit(io_tree, offset, range_end,
7428 EXTENT_DELALLOC, 0, NULL);
7429 if (ret) {
7430 ret = -EAGAIN;
7431 goto out;
7432 }
7433 }
7434
Josef Bacik1bda19e2013-10-18 12:10:36 -04007435 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007436
7437 /*
7438 * look for other files referencing this extent, if we
7439 * find any we must cow
7440 */
Josef Bacik00361582013-08-14 14:02:47 -04007441
Liu Boe4c3b2d2017-01-30 12:25:28 -08007442 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007443 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007444 if (ret) {
7445 ret = 0;
7446 goto out;
7447 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007448
7449 /*
7450 * adjust disk_bytenr and num_bytes to cover just the bytes
7451 * in this extent we are about to write. If there
7452 * are any csums in that range we have to cow in order
7453 * to keep the csums correct
7454 */
7455 disk_bytenr += backref_offset;
7456 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007457 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7458 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007459 /*
7460 * all of the above have passed, it is safe to overwrite this extent
7461 * without cow
7462 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007463 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007464 ret = 1;
7465out:
7466 btrfs_free_path(path);
7467 return ret;
7468}
7469
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007470bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7471{
7472 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007473 bool found = false;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007474 void **pagep = NULL;
7475 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007476 unsigned long start_idx;
7477 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007478
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007479 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007480
7481 /*
7482 * end is the last byte in the last page. end == start is legal
7483 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007484 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007485
7486 rcu_read_lock();
7487
7488 /* Most of the code in this while loop is lifted from
7489 * find_get_page. It's been modified to begin searching from a
7490 * page and return just the first page found in that range. If the
7491 * found idx is less than or equal to the end idx then we know that
7492 * a page exists. If no pages are found or if those pages are
7493 * outside of the range then we're fine (yay!) */
7494 while (page == NULL &&
7495 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7496 page = radix_tree_deref_slot(pagep);
7497 if (unlikely(!page))
7498 break;
7499
7500 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007501 if (radix_tree_deref_retry(page)) {
7502 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007503 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007504 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007505 /*
7506 * Otherwise, shmem/tmpfs must be storing a swap entry
7507 * here as an exceptional entry: so return it without
7508 * attempting to raise page count.
7509 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007510 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007511 break; /* TODO: Is this relevant for this use case? */
7512 }
7513
Filipe Manana91405152014-06-05 13:22:24 +01007514 if (!page_cache_get_speculative(page)) {
7515 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007516 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007517 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007518
7519 /*
7520 * Has the page moved?
7521 * This is part of the lockless pagecache protocol. See
7522 * include/linux/pagemap.h for details.
7523 */
7524 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007525 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007526 page = NULL;
7527 }
7528 }
7529
7530 if (page) {
7531 if (page->index <= end_idx)
7532 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007533 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007534 }
7535
7536 rcu_read_unlock();
7537 return found;
7538}
7539
Josef Bacikeb838e72012-07-31 16:28:48 -04007540static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7541 struct extent_state **cached_state, int writing)
7542{
7543 struct btrfs_ordered_extent *ordered;
7544 int ret = 0;
7545
7546 while (1) {
7547 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007548 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007549 /*
7550 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007551 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007552 * extents in this range.
7553 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007554 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007555 lockend - lockstart + 1);
7556
7557 /*
7558 * We need to make sure there are no buffered pages in this
7559 * range either, we could have raced between the invalidate in
7560 * generic_file_direct_write and locking the extent. The
7561 * invalidate needs to happen so that reads after a write do not
7562 * get stale data.
7563 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007564 if (!ordered &&
7565 (!writing ||
7566 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007567 break;
7568
7569 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007570 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007571
7572 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007573 /*
7574 * If we are doing a DIO read and the ordered extent we
7575 * found is for a buffered write, we can not wait for it
7576 * to complete and retry, because if we do so we can
7577 * deadlock with concurrent buffered writes on page
7578 * locks. This happens only if our DIO read covers more
7579 * than one extent map, if at this point has already
7580 * created an ordered extent for a previous extent map
7581 * and locked its range in the inode's io tree, and a
7582 * concurrent write against that previous extent map's
7583 * range and this range started (we unlock the ranges
7584 * in the io tree only when the bios complete and
7585 * buffered writes always lock pages before attempting
7586 * to lock range in the io tree).
7587 */
7588 if (writing ||
7589 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7590 btrfs_start_ordered_extent(inode, ordered, 1);
7591 else
7592 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007593 btrfs_put_ordered_extent(ordered);
7594 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007595 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007596 * We could trigger writeback for this range (and wait
7597 * for it to complete) and then invalidate the pages for
7598 * this range (through invalidate_inode_pages2_range()),
7599 * but that can lead us to a deadlock with a concurrent
7600 * call to readpages() (a buffered read or a defrag call
7601 * triggered a readahead) on a page lock due to an
7602 * ordered dio extent we created before but did not have
7603 * yet a corresponding bio submitted (whence it can not
7604 * complete), which makes readpages() wait for that
7605 * ordered extent to complete while holding a lock on
7606 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007607 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007608 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007609 }
7610
Filipe Mananaade77022016-02-18 14:28:55 +00007611 if (ret)
7612 break;
7613
Josef Bacikeb838e72012-07-31 16:28:48 -04007614 cond_resched();
7615 }
7616
7617 return ret;
7618}
7619
Liu Bo6f9994d2017-01-31 07:50:22 -08007620/* The callers of this must take lock_extent() */
7621static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7622 u64 orig_start, u64 block_start,
7623 u64 block_len, u64 orig_block_len,
7624 u64 ram_bytes, int compress_type,
7625 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007626{
7627 struct extent_map_tree *em_tree;
7628 struct extent_map *em;
7629 struct btrfs_root *root = BTRFS_I(inode)->root;
7630 int ret;
7631
Liu Bo6f9994d2017-01-31 07:50:22 -08007632 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7633 type == BTRFS_ORDERED_COMPRESSED ||
7634 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007635 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007636
Josef Bacik69ffb542012-09-11 15:40:07 -04007637 em_tree = &BTRFS_I(inode)->extent_tree;
7638 em = alloc_extent_map();
7639 if (!em)
7640 return ERR_PTR(-ENOMEM);
7641
7642 em->start = start;
7643 em->orig_start = orig_start;
7644 em->len = len;
7645 em->block_len = block_len;
7646 em->block_start = block_start;
7647 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007648 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007649 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007650 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007651 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007652 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007653 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007654 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007655 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7656 em->compress_type = compress_type;
7657 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007658
7659 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007660 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007661 em->start + em->len - 1, 0);
7662 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007663 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007664 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007665 /*
7666 * The caller has taken lock_extent(), who could race with us
7667 * to add em?
7668 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007669 } while (ret == -EEXIST);
7670
7671 if (ret) {
7672 free_extent_map(em);
7673 return ERR_PTR(ret);
7674 }
7675
Liu Bo6f9994d2017-01-31 07:50:22 -08007676 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007677 return em;
7678}
7679
Josef Bacik4b46fce2010-05-23 11:00:55 -04007680static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7681 struct buffer_head *bh_result, int create)
7682{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007683 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007684 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007685 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307686 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007687 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007688 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007689 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007690 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007691 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007692
Miao Xie172a5042013-02-21 02:48:22 -07007693 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007694 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007695 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007696 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007697
Josef Bacikc3298612012-08-03 16:49:19 -04007698 lockstart = start;
7699 lockend = start + len - 1;
7700
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007701 if (current->journal_info) {
7702 /*
7703 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007704 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007705 * confused.
7706 */
chandan50745b02015-08-28 21:10:13 +05307707 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007708 current->journal_info = NULL;
7709 }
7710
Josef Bacikeb838e72012-07-31 16:28:48 -04007711 /*
7712 * If this errors out it's because we couldn't invalidate pagecache for
7713 * this range and we need to fallback to buffered.
7714 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007715 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7716 create)) {
7717 ret = -ENOTBLK;
7718 goto err;
7719 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007720
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007721 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007722 if (IS_ERR(em)) {
7723 ret = PTR_ERR(em);
7724 goto unlock_err;
7725 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007726
7727 /*
7728 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7729 * io. INLINE is special, and we could probably kludge it in here, but
7730 * it's still buffered so for safety lets just fall back to the generic
7731 * buffered path.
7732 *
7733 * For COMPRESSED we _have_ to read the entire extent in so we can
7734 * decompress it, so there will be buffering required no matter what we
7735 * do, so go ahead and fallback to buffered.
7736 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007737 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007738 * to buffered IO. Don't blame me, this is the price we pay for using
7739 * the generic code.
7740 */
7741 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7742 em->block_start == EXTENT_MAP_INLINE) {
7743 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007744 ret = -ENOTBLK;
7745 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007746 }
7747
7748 /* Just a good old fashioned hole, return */
7749 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7750 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7751 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007752 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007753 }
7754
7755 /*
7756 * We don't allocate a new extent in the following cases
7757 *
7758 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7759 * existing extent.
7760 * 2) The extent is marked as PREALLOC. We're good to go here and can
7761 * just use the extent.
7762 *
7763 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007764 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007765 len = min(len, em->len - (start - em->start));
7766 lockstart = start + len;
7767 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007768 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007769
7770 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7771 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7772 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007773 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007774 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007775
7776 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7777 type = BTRFS_ORDERED_PREALLOC;
7778 else
7779 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007780 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007781 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007782
Josef Bacik00361582013-08-14 14:02:47 -04007783 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007784 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007785 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007786 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007787
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007788 em2 = btrfs_create_dio_extent(inode, start, len,
7789 orig_start, block_start,
7790 len, orig_block_len,
7791 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007792 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007793 if (type == BTRFS_ORDERED_PREALLOC) {
7794 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007795 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007796 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007797 if (em2 && IS_ERR(em2)) {
7798 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007799 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007800 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007801 /*
7802 * For inode marked NODATACOW or extent marked PREALLOC,
7803 * use the existing or preallocated extent, so does not
7804 * need to adjust btrfs_space_info's bytes_may_use.
7805 */
7806 btrfs_free_reserved_data_space_noquota(inode,
7807 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007808 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007809 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007810 }
Josef Bacik00361582013-08-14 14:02:47 -04007811
Chris Mason46bfbb52010-05-26 11:04:10 -04007812 /*
7813 * this will cow the extent, reset the len in case we changed
7814 * it above
7815 */
7816 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007817 free_extent_map(em);
7818 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007819 if (IS_ERR(em)) {
7820 ret = PTR_ERR(em);
7821 goto unlock_err;
7822 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007823 len = min(len, em->len - (start - em->start));
7824unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007825 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7826 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007827 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007828 bh_result->b_bdev = em->bdev;
7829 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007830 if (create) {
7831 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7832 set_buffer_new(bh_result);
7833
7834 /*
7835 * Need to update the i_size under the extent lock so buffered
7836 * readers will get the updated i_size when we unlock.
7837 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007838 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007839 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007840
chandan50745b02015-08-28 21:10:13 +05307841 WARN_ON(dio_data->reserve < len);
7842 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007843 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307844 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007845 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007846
Josef Bacikeb838e72012-07-31 16:28:48 -04007847 /*
7848 * In the case of write we need to clear and unlock the entire range,
7849 * in the case of read we need to unlock only the end area that we
7850 * aren't using if there is any left over space.
7851 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007852 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007853 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7854 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007855 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007856 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007857 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007858 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007859
Josef Bacik4b46fce2010-05-23 11:00:55 -04007860 free_extent_map(em);
7861
7862 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007863
7864unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007865 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007866 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007867err:
chandan50745b02015-08-28 21:10:13 +05307868 if (dio_data)
7869 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007870 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007871}
7872
Omar Sandoval58efbc92017-08-22 23:45:59 -07007873static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7874 struct bio *bio,
7875 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007876{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007877 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007878 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007879
Mike Christie37226b22016-06-05 14:31:52 -05007880 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007881
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007882 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007883 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007884 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007885
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007886 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007887
Miao Xie8b110e32014-09-12 18:44:03 +08007888 return ret;
7889}
7890
7891static int btrfs_check_dio_repairable(struct inode *inode,
7892 struct bio *failed_bio,
7893 struct io_failure_record *failrec,
7894 int failed_mirror)
7895{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007896 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007897 int num_copies;
7898
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007899 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007900 if (num_copies == 1) {
7901 /*
7902 * we only have a single copy of the data, so don't bother with
7903 * all the retry and error correction code that follows. no
7904 * matter what the error is, it is very likely to persist.
7905 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007906 btrfs_debug(fs_info,
7907 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7908 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007909 return 0;
7910 }
7911
7912 failrec->failed_mirror = failed_mirror;
7913 failrec->this_mirror++;
7914 if (failrec->this_mirror == failed_mirror)
7915 failrec->this_mirror++;
7916
7917 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007918 btrfs_debug(fs_info,
7919 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7920 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007921 return 0;
7922 }
7923
7924 return 1;
7925}
7926
Omar Sandoval58efbc92017-08-22 23:45:59 -07007927static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7928 struct page *page, unsigned int pgoff,
7929 u64 start, u64 end, int failed_mirror,
7930 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007931{
7932 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007933 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7934 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007935 struct bio *bio;
7936 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007937 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007938 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007939 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007940 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007941 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007942
Mike Christie37226b22016-06-05 14:31:52 -05007943 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007944
7945 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7946 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007947 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007948
7949 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7950 failed_mirror);
7951 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007952 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007953 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007954 }
7955
Liu Bo17347ce2017-05-15 15:33:27 -07007956 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007957 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007958 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007959 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007960 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007961
7962 isector = start - btrfs_io_bio(failed_bio)->logical;
7963 isector >>= inode->i_sb->s_blocksize_bits;
7964 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307965 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05007966 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007967
7968 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007969 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007970 read_mode, failrec->this_mirror, failrec->in_validation);
7971
Omar Sandoval58efbc92017-08-22 23:45:59 -07007972 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7973 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007974 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007975 bio_put(bio);
7976 }
7977
Omar Sandoval58efbc92017-08-22 23:45:59 -07007978 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007979}
7980
7981struct btrfs_retry_complete {
7982 struct completion done;
7983 struct inode *inode;
7984 u64 start;
7985 int uptodate;
7986};
7987
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007988static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007989{
7990 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007991 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007992 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007993 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007994 int i;
7995
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007996 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007997 goto end;
7998
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307999 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008000 io_tree = &BTRFS_I(inode)->io_tree;
8001 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08008002 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308003
Miao Xie8b110e32014-09-12 18:44:03 +08008004 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008005 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008006 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008007 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8008 io_tree, done->start, bvec->bv_page,
8009 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008010end:
8011 complete(&done->done);
8012 bio_put(bio);
8013}
8014
Omar Sandoval58efbc92017-08-22 23:45:59 -07008015static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8016 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008017{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308018 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008019 struct bio_vec bvec;
8020 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008021 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008022 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308023 unsigned int pgoff;
8024 u32 sectorsize;
8025 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008026 blk_status_t ret;
8027 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008028
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308029 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008030 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308031
Miao Xiec1dc0892014-09-12 18:43:56 +08008032 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008033 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008034 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008035
Liu Bo17347ce2017-05-15 15:33:27 -07008036 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8037 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8038 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308039
8040next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008041 done.uptodate = 0;
8042 done.start = start;
8043 init_completion(&done.done);
8044
Liu Bo17347ce2017-05-15 15:33:27 -07008045 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308046 pgoff, start, start + sectorsize - 1,
8047 io_bio->mirror_num,
8048 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008049 if (ret) {
8050 err = ret;
8051 goto next;
8052 }
Miao Xie8b110e32014-09-12 18:44:03 +08008053
David Sterba9c17f6c2017-07-19 19:26:45 +02008054 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008055
8056 if (!done.uptodate) {
8057 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308058 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008059 }
8060
Liu Bo629ebf42017-05-15 17:20:07 -07008061next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308062 start += sectorsize;
8063
Liu Bo97bf5a52017-04-07 13:11:10 -07008064 nr_sectors--;
8065 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308066 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008067 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308068 goto next_block_or_try_again;
8069 }
Miao Xie8b110e32014-09-12 18:44:03 +08008070 }
8071
Liu Bo629ebf42017-05-15 17:20:07 -07008072 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008073}
8074
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008075static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008076{
8077 struct btrfs_retry_complete *done = bio->bi_private;
8078 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008079 struct extent_io_tree *io_tree, *failure_tree;
8080 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008081 struct bio_vec *bvec;
8082 int uptodate;
8083 int ret;
8084 int i;
8085
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008086 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008087 goto end;
8088
8089 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308090
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308091 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08008092 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308093
Josef Bacik7870d082017-05-05 11:57:15 -04008094 io_tree = &BTRFS_I(inode)->io_tree;
8095 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8096
David Sterbac09abff2017-07-13 18:10:07 +02008097 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008098 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008099 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8100 bvec->bv_offset, done->start,
8101 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008102 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008103 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8104 failure_tree, io_tree, done->start,
8105 bvec->bv_page,
8106 btrfs_ino(BTRFS_I(inode)),
8107 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008108 else
8109 uptodate = 0;
8110 }
8111
8112 done->uptodate = uptodate;
8113end:
8114 complete(&done->done);
8115 bio_put(bio);
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{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308121 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008122 struct bio_vec bvec;
8123 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008124 struct btrfs_retry_complete done;
8125 u64 start;
8126 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308127 u32 sectorsize;
8128 int nr_sectors;
8129 unsigned int pgoff;
8130 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008131 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008132 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008133 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008134
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308135 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008136 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308137
Omar Sandoval58efbc92017-08-22 23:45:59 -07008138 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008139 start = io_bio->logical;
8140 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008141 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008142
Liu Bo17347ce2017-05-15 15:33:27 -07008143 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8144 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308145
Liu Bo17347ce2017-05-15 15:33:27 -07008146 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308147next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008148 if (uptodate) {
8149 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8150 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8151 bvec.bv_page, pgoff, start, sectorsize);
8152 if (likely(!ret))
8153 goto next;
8154 }
Miao Xie8b110e32014-09-12 18:44:03 +08008155try_again:
8156 done.uptodate = 0;
8157 done.start = start;
8158 init_completion(&done.done);
8159
Omar Sandoval58efbc92017-08-22 23:45:59 -07008160 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8161 pgoff, start, start + sectorsize - 1,
8162 io_bio->mirror_num, btrfs_retry_endio,
8163 &done);
8164 if (status) {
8165 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008166 goto next;
8167 }
8168
David Sterba9c17f6c2017-07-19 19:26:45 +02008169 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008170
8171 if (!done.uptodate) {
8172 /* We might have another mirror, so try again */
8173 goto try_again;
8174 }
8175next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308176 offset += sectorsize;
8177 start += sectorsize;
8178
8179 ASSERT(nr_sectors);
8180
Liu Bo97bf5a52017-04-07 13:11:10 -07008181 nr_sectors--;
8182 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308183 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008184 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308185 goto next_block;
8186 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008187 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008188
8189 return err;
8190}
8191
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008192static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8193 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008194{
8195 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8196
8197 if (skip_csum) {
8198 if (unlikely(err))
8199 return __btrfs_correct_data_nocsum(inode, io_bio);
8200 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008201 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008202 } else {
8203 return __btrfs_subio_endio_read(inode, io_bio, err);
8204 }
8205}
8206
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008207static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008208{
8209 struct btrfs_dio_private *dip = bio->bi_private;
8210 struct inode *inode = dip->inode;
8211 struct bio *dio_bio;
8212 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008213 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008214
Liu Bo99c4e3b2017-09-15 15:06:51 -06008215 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008216 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008217
Josef Bacik4b46fce2010-05-23 11:00:55 -04008218 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008219 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008220 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008221
Josef Bacik4b46fce2010-05-23 11:00:55 -04008222 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008223
Liu Bo99c4e3b2017-09-15 15:06:51 -06008224 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008225 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008226
8227 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008228 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008229 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008230}
8231
Qu Wenruo524272602017-03-08 10:25:52 +08008232static void __endio_write_update_ordered(struct inode *inode,
8233 const u64 offset, const u64 bytes,
8234 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008235{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008236 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008237 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008238 struct btrfs_workqueue *wq;
8239 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008240 u64 ordered_offset = offset;
8241 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008242 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008243 int ret;
8244
Qu Wenruo524272602017-03-08 10:25:52 +08008245 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8246 wq = fs_info->endio_freespace_worker;
8247 func = btrfs_freespace_write_helper;
8248 } else {
8249 wq = fs_info->endio_write_workers;
8250 func = btrfs_endio_write_helper;
8251 }
8252
Chris Mason163cf092010-11-28 19:56:33 -05008253again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008254 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008255 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8256 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008257 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008258 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008259 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008260 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008261
Qu Wenruo524272602017-03-08 10:25:52 +08008262 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8263 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008264out_test:
8265 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008266 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8267 * in the range, we can exit.
8268 */
8269 if (ordered_offset == last_offset)
8270 return;
8271 /*
Chris Mason163cf092010-11-28 19:56:33 -05008272 * our bio might span multiple ordered extents. If we haven't
8273 * completed the accounting for the whole dio, go back and try again
8274 */
Filipe Manana14543772015-11-24 16:23:54 +00008275 if (ordered_offset < offset + bytes) {
8276 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008277 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008278 goto again;
8279 }
Filipe Manana14543772015-11-24 16:23:54 +00008280}
8281
8282static void btrfs_endio_direct_write(struct bio *bio)
8283{
8284 struct btrfs_dio_private *dip = bio->bi_private;
8285 struct bio *dio_bio = dip->dio_bio;
8286
Qu Wenruo524272602017-03-08 10:25:52 +08008287 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008288 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008289
Josef Bacik4b46fce2010-05-23 11:00:55 -04008290 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008291
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008292 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008293 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008294 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008295}
8296
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008297static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008298 struct bio *bio, int mirror_num,
8299 unsigned long bio_flags, u64 offset)
8300{
Josef Bacikc6100a42017-05-05 11:57:13 -04008301 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008302 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008303 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008304 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008305 return 0;
8306}
8307
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008308static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008309{
8310 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008311 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008312
Miao Xie8b110e32014-09-12 18:44:03 +08008313 if (err)
8314 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008315 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008316 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8317 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008318 (unsigned long long)bio->bi_iter.bi_sector,
8319 bio->bi_iter.bi_size, err);
8320
8321 if (dip->subio_endio)
8322 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008323
8324 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008325 dip->errors = 1;
8326
8327 /*
8328 * before atomic variable goto zero, we must make sure
8329 * dip->errors is perceived to be set.
8330 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008331 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008332 }
8333
8334 /* if there are more bios still pending for this dio, just exit */
8335 if (!atomic_dec_and_test(&dip->pending_bios))
8336 goto out;
8337
Chris Mason9be33952013-05-17 18:30:14 -04008338 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008339 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008340 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008341 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008342 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008343 }
8344out:
8345 bio_put(bio);
8346}
8347
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008348static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008349 struct btrfs_dio_private *dip,
8350 struct bio *bio,
8351 u64 file_offset)
8352{
8353 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8354 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008355 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008356
8357 /*
8358 * We load all the csum data we need when we submit
8359 * the first bio to reduce the csum tree search and
8360 * contention.
8361 */
8362 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008363 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008364 file_offset);
8365 if (ret)
8366 return ret;
8367 }
8368
8369 if (bio == dip->orig_bio)
8370 return 0;
8371
8372 file_offset -= dip->logical_offset;
8373 file_offset >>= inode->i_sb->s_blocksize_bits;
8374 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8375
8376 return 0;
8377}
8378
Omar Sandoval58efbc92017-08-22 23:45:59 -07008379static inline blk_status_t
8380__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008381 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008382{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008383 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008384 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008385 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008386 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008387
Liu Bo4c274bc2017-11-01 17:19:27 -06008388 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008389 if (async_submit)
8390 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8391
Josef Bacik5fd02042012-05-02 14:00:54 -04008392 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008393 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008394 if (ret)
8395 goto err;
8396 }
Miao Xiee65e1532010-11-22 03:04:43 +00008397
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008398 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008399 goto map;
8400
8401 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008402 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8403 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008404 __btrfs_submit_bio_start_direct_io,
8405 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008406 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008407 } else if (write) {
8408 /*
8409 * If we aren't doing async submit, calculate the csum of the
8410 * bio now.
8411 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008412 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008413 if (ret)
8414 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008415 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008416 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008417 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008418 if (ret)
8419 goto err;
8420 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008421map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008422 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008423err:
Miao Xiee65e1532010-11-22 03:04:43 +00008424 return ret;
8425}
8426
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008427static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008428{
8429 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008430 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008431 struct bio *bio;
8432 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008433 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008434 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008435 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008436 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008437 u64 submit_len;
8438 int clone_offset = 0;
8439 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308440 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008441 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008442
Kent Overstreet4f024f32013-10-11 15:44:27 -07008443 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008444 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008445 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8446 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008447 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008448 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008449
Liu Bo725130b2017-05-16 09:51:39 -07008450 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008451 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008452 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008453 goto submit;
8454 }
8455
David Woodhouse53b381b2013-01-29 18:40:14 -05008456 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008457 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008458 async_submit = 0;
8459 else
8460 async_submit = 1;
8461
Liu Bo725130b2017-05-16 09:51:39 -07008462 /* bio split */
8463 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008464 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008465 do {
Liu Bo725130b2017-05-16 09:51:39 -07008466 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008467
Liu Bo725130b2017-05-16 09:51:39 -07008468 /*
8469 * This will never fail as it's passing GPF_NOFS and
8470 * the allocation is backed by btrfs_bioset.
8471 */
Liu Boe4770942017-05-16 10:57:14 -07008472 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008473 clone_len);
8474 bio->bi_private = dip;
8475 bio->bi_end_io = btrfs_end_dio_bio;
8476 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008477
Liu Bo725130b2017-05-16 09:51:39 -07008478 ASSERT(submit_len >= clone_len);
8479 submit_len -= clone_len;
8480 if (submit_len == 0)
8481 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008482
Liu Bo725130b2017-05-16 09:51:39 -07008483 /*
8484 * Increase the count before we submit the bio so we know
8485 * the end IO handler won't happen before we increase the
8486 * count. Otherwise, the dip might get freed before we're
8487 * done setting it up.
8488 */
8489 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008490
Linus Torvalds66ba7722017-09-09 13:27:51 -07008491 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008492 async_submit);
8493 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008494 bio_put(bio);
8495 atomic_dec(&dip->pending_bios);
8496 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008497 }
Liu Bo725130b2017-05-16 09:51:39 -07008498
8499 clone_offset += clone_len;
8500 start_sector += clone_len >> 9;
8501 file_offset += clone_len;
8502
8503 map_length = submit_len;
8504 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8505 start_sector << 9, &map_length, NULL, 0);
8506 if (ret)
8507 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008508 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008509
Josef Bacik02f57c72011-04-06 14:25:44 -04008510submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008511 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008512 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008513 return 0;
8514
8515 bio_put(bio);
8516out_err:
8517 dip->errors = 1;
8518 /*
8519 * before atomic variable goto zero, we must
8520 * make sure dip->errors is perceived to be set.
8521 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008522 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008523 if (atomic_dec_and_test(&dip->pending_bios))
8524 bio_io_error(dip->orig_bio);
8525
8526 /* bio_end_io() will handle error, so we needn't return it */
8527 return 0;
8528}
8529
Mike Christie8a4c1e42016-06-05 14:31:50 -05008530static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8531 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008532{
Filipe Manana61de7182015-07-01 12:13:10 +01008533 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008534 struct bio *bio = NULL;
8535 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008536 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008537 int ret = 0;
8538
David Sterba8b6c1d52017-06-02 17:48:13 +02008539 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008540
Miao Xiec1dc0892014-09-12 18:43:56 +08008541 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008542 if (!dip) {
8543 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008544 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008545 }
8546
8547 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008548 dip->inode = inode;
8549 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008550 dip->bytes = dio_bio->bi_iter.bi_size;
8551 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008552 bio->bi_private = dip;
8553 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008554 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008555 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008556 io_bio = btrfs_io_bio(bio);
8557 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008558
Miao Xiec1dc0892014-09-12 18:43:56 +08008559 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008560 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008561 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008562 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008563 dip->subio_endio = btrfs_subio_endio_read;
8564 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008565
Filipe Mananaf28a4922015-12-08 19:23:20 +00008566 /*
8567 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8568 * even if we fail to submit a bio, because in such case we do the
8569 * corresponding error handling below and it must not be done a second
8570 * time by btrfs_direct_IO().
8571 */
8572 if (write) {
8573 struct btrfs_dio_data *dio_data = current->journal_info;
8574
8575 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8576 dip->bytes;
8577 dio_data->unsubmitted_oe_range_start =
8578 dio_data->unsubmitted_oe_range_end;
8579 }
8580
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008581 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008582 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008583 return;
Chris Mason9be33952013-05-17 18:30:14 -04008584
Liu Bo3892ac92017-04-17 15:00:28 -07008585 if (io_bio->end_io)
8586 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008587
Josef Bacik4b46fce2010-05-23 11:00:55 -04008588free_ordered:
8589 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008590 * If we arrived here it means either we failed to submit the dip
8591 * or we either failed to clone the dio_bio or failed to allocate the
8592 * dip. If we cloned the dio_bio and allocated the dip, we can just
8593 * call bio_endio against our io_bio so that we get proper resource
8594 * cleanup if we fail to submit the dip, otherwise, we must do the
8595 * same as btrfs_endio_direct_[write|read] because we can't call these
8596 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008597 */
Liu Bo3892ac92017-04-17 15:00:28 -07008598 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008599 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008600 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008601 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008602 * and all the cleanup and final put for dio_bio (through
8603 * dio_end_io()).
8604 */
8605 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008606 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008607 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008608 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008609 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008610 file_offset,
8611 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008612 false);
Filipe Manana14543772015-11-24 16:23:54 +00008613 else
Filipe Manana61de7182015-07-01 12:13:10 +01008614 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8615 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008616
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008617 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008618 /*
8619 * Releases and cleans up our dio_bio, no need to bio_put()
8620 * nor bio_endio()/bio_io_error() against dio_bio.
8621 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008622 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008623 }
Liu Bo3892ac92017-04-17 15:00:28 -07008624 if (bio)
8625 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008626 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008627}
8628
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008629static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008630 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008631{
8632 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008633 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008634 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008635 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008636
8637 if (offset & blocksize_mask)
8638 goto out;
8639
Al Viro28060d52014-03-22 05:15:17 -04008640 if (iov_iter_alignment(iter) & blocksize_mask)
8641 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008642
Al Viro28060d52014-03-22 05:15:17 -04008643 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008644 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008645 return 0;
8646 /*
8647 * Check to make sure we don't have duplicate iov_base's in this
8648 * iovec, if so return EINVAL, otherwise we'll get csum errors
8649 * when reading back.
8650 */
8651 for (seg = 0; seg < iter->nr_segs; seg++) {
8652 for (i = seg + 1; i < iter->nr_segs; i++) {
8653 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008654 goto out;
8655 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008656 }
8657 retval = 0;
8658out:
8659 return retval;
8660}
Josef Bacikeb838e72012-07-31 16:28:48 -04008661
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008662static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008663{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008664 struct file *file = iocb->ki_filp;
8665 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008666 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308667 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008668 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008669 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008670 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008671 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008672 bool wakeup = true;
8673 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008674 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008675
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008676 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008677 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008678
Jens Axboefe0f07d2015-04-15 17:05:48 -06008679 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008680
Josef Bacik0e267c42013-07-02 10:38:02 -04008681 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008682 * The generic stuff only does filemap_write_and_wait_range, which
8683 * isn't enough if we've written compressed pages to this area, so
8684 * we need to flush the dirty pages again to make absolutely sure
8685 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008686 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008687 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008688 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8689 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008690 filemap_fdatawrite_range(inode->i_mapping, offset,
8691 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008692
Omar Sandoval6f673762015-03-16 04:33:52 -07008693 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008694 /*
8695 * If the write DIO is beyond the EOF, we need update
8696 * the isize, but it is protected by i_mutex. So we can
8697 * not unlock the i_mutex at this case.
8698 */
8699 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008700 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008701 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008702 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008703 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8704 ret = -EAGAIN;
8705 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008706 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008707 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8708 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008709 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008710 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008711
8712 /*
8713 * We need to know how many extents we reserved so that we can
8714 * do the accounting properly if we go over the number we
8715 * originally calculated. Abuse current->journal_info for this.
8716 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008717 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008718 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008719 dio_data.unsubmitted_oe_range_start = (u64)offset;
8720 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308721 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308722 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008723 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8724 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008725 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008726 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8727 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008728 }
8729
Omar Sandoval17f8c842015-03-16 04:33:50 -07008730 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008731 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008732 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008733 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008734 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308735 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008736 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008737 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308738 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008739 btrfs_delalloc_release_space(inode, data_reserved,
8740 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008741 /*
8742 * On error we might have left some ordered extents
8743 * without submitting corresponding bios for them, so
8744 * cleanup them up to avoid other tasks getting them
8745 * and waiting for them to complete forever.
8746 */
8747 if (dio_data.unsubmitted_oe_range_start <
8748 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008749 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008750 dio_data.unsubmitted_oe_range_start,
8751 dio_data.unsubmitted_oe_range_end -
8752 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008753 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008754 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008755 btrfs_delalloc_release_space(inode, data_reserved,
8756 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008757 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008758 }
Miao Xie38851cc2013-02-08 07:04:11 +00008759out:
Miao Xie2e60a512013-02-08 07:01:08 +00008760 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008761 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008762 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008763 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008764
Qu Wenruo364ecf32017-02-27 15:10:38 +08008765 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008766 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008767}
8768
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008769#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8770
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008771static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8772 __u64 start, __u64 len)
8773{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008774 int ret;
8775
8776 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8777 if (ret)
8778 return ret;
8779
David Sterba2135fb92017-06-23 04:09:57 +02008780 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008781}
8782
Chris Mason9ebefb182007-06-15 13:50:00 -04008783int btrfs_readpage(struct file *file, struct page *page)
8784{
Chris Masond1310b22008-01-24 16:13:08 -05008785 struct extent_io_tree *tree;
8786 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008787 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008788}
Chris Mason1832a6d2007-12-21 16:27:21 -05008789
Chris Mason39279cc2007-06-12 06:35:45 -04008790static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8791{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008792 struct inode *inode = page->mapping->host;
8793 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008794
8795 if (current->flags & PF_MEMALLOC) {
8796 redirty_page_for_writepage(wbc, page);
8797 unlock_page(page);
8798 return 0;
8799 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008800
8801 /*
8802 * If we are under memory pressure we will call this directly from the
8803 * VM, we need to make sure we have the inode referenced for the ordered
8804 * extent. If not just return like we didn't do anything.
8805 */
8806 if (!igrab(inode)) {
8807 redirty_page_for_writepage(wbc, page);
8808 return AOP_WRITEPAGE_ACTIVATE;
8809 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008810 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008811 btrfs_add_delayed_iput(inode);
8812 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008813}
Chris Mason39279cc2007-06-12 06:35:45 -04008814
Eric Sandeen48a3b632013-04-25 20:41:01 +00008815static int btrfs_writepages(struct address_space *mapping,
8816 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008817{
Chris Masond1310b22008-01-24 16:13:08 -05008818 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008819
Chris Masond1310b22008-01-24 16:13:08 -05008820 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba43317592017-06-23 03:46:07 +02008821 return extent_writepages(tree, mapping, wbc);
Chris Masonb293f022007-11-01 19:45:34 -04008822}
8823
Chris Mason3ab2fb52007-11-08 10:59:22 -05008824static int
8825btrfs_readpages(struct file *file, struct address_space *mapping,
8826 struct list_head *pages, unsigned nr_pages)
8827{
Chris Masond1310b22008-01-24 16:13:08 -05008828 struct extent_io_tree *tree;
8829 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba09325842017-06-23 04:09:57 +02008830 return extent_readpages(tree, mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008831}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008832static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008833{
Chris Masond1310b22008-01-24 16:13:08 -05008834 struct extent_io_tree *tree;
8835 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008836 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008837
Chris Masond1310b22008-01-24 16:13:08 -05008838 tree = &BTRFS_I(page->mapping->host)->io_tree;
8839 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008840 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008841 if (ret == 1) {
8842 ClearPagePrivate(page);
8843 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008844 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008845 }
8846 return ret;
8847}
Chris Mason39279cc2007-06-12 06:35:45 -04008848
Chris Masone6dcd2d2008-07-17 12:53:50 -04008849static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8850{
Chris Mason98509cf2008-09-11 15:51:43 -04008851 if (PageWriteback(page) || PageDirty(page))
8852 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008853 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008854}
8855
Lukas Czernerd47992f2013-05-21 23:17:23 -04008856static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8857 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008858{
Josef Bacik5fd02042012-05-02 14:00:54 -04008859 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008860 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008861 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008862 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008863 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008864 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308865 u64 start;
8866 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008867 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008868
Chris Mason8b62b722009-09-02 16:53:46 -04008869 /*
8870 * we have the page locked, so new writeback can't start,
8871 * and the dirty bit won't be cleared while we are here.
8872 *
8873 * Wait for IO on this page so that we can safely clear
8874 * the PagePrivate2 bit and do ordered accounting
8875 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008876 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008877
Josef Bacik5fd02042012-05-02 14:00:54 -04008878 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008879 if (offset) {
8880 btrfs_releasepage(page, GFP_NOFS);
8881 return;
8882 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008883
8884 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008885 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308886again:
8887 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008888 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308889 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008890 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308891 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008892 /*
8893 * IO on this page will never be started, so we need
8894 * to account for any ordered extents now
8895 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008896 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308897 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008898 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008899 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008900 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008901 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008902 /*
8903 * whoever cleared the private bit is responsible
8904 * for the finish_ordered_io
8905 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008906 if (TestClearPagePrivate2(page)) {
8907 struct btrfs_ordered_inode_tree *tree;
8908 u64 new_len;
8909
8910 tree = &BTRFS_I(inode)->ordered_tree;
8911
8912 spin_lock_irq(&tree->lock);
8913 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308914 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008915 if (new_len < ordered->truncated_len)
8916 ordered->truncated_len = new_len;
8917 spin_unlock_irq(&tree->lock);
8918
8919 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308920 start,
8921 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008922 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008923 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008924 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008925 if (!inode_evicting) {
8926 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308927 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008928 &cached_state);
8929 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308930
8931 start = end + 1;
8932 if (start < page_end)
8933 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008934 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008935
Qu Wenruob9d0b382015-09-29 10:35:16 +08008936 /*
8937 * Qgroup reserved space handler
8938 * Page here will be either
8939 * 1) Already written to disk
8940 * In this case, its reserved space is released from data rsv map
8941 * and will be freed by delayed_ref handler finally.
8942 * So even we call qgroup_free_data(), it won't decrease reserved
8943 * space.
8944 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008945 * This means the reserved space should be freed here. However,
8946 * if a truncate invalidates the page (by clearing PageDirty)
8947 * and the page is accounted for while allocating extent
8948 * in btrfs_check_data_free_space() we let delayed_ref to
8949 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008950 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008951 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008952 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008953 if (!inode_evicting) {
8954 clear_extent_bit(tree, page_start, page_end,
8955 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008956 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8957 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008958 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008959
8960 __btrfs_releasepage(page, GFP_NOFS);
8961 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008962
Chris Mason4a096752008-07-21 10:29:44 -04008963 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008964 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008965 ClearPagePrivate(page);
8966 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008967 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008968 }
Chris Mason39279cc2007-06-12 06:35:45 -04008969}
8970
Chris Mason9ebefb182007-06-15 13:50:00 -04008971/*
8972 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8973 * called from a page fault handler when a page is first dirtied. Hence we must
8974 * be careful to check for EOF conditions here. We set the page up correctly
8975 * for a written page which means we get ENOSPC checking when writing into
8976 * holes and correct delalloc and unwritten extent mapping on filesystems that
8977 * support these features.
8978 *
8979 * We are not allowed to take the i_mutex here so we have to play games to
8980 * protect against truncate races as the page could now be beyond EOF. Because
8981 * vmtruncate() writes the inode size before removing pages, once we have the
8982 * page lock we can determine safely if the page is beyond EOF. If it is not
8983 * beyond EOF, then the page is guaranteed safe against truncation until we
8984 * unlock the page.
8985 */
Dave Jiang11bac802017-02-24 14:56:41 -08008986int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008987{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008988 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008989 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008990 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008991 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8992 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008993 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008994 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008995 char *kaddr;
8996 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008997 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008998 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008999 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309000 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009001 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009002 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309003 u64 end;
9004
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009005 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009006
Jan Karab2b5ef52012-06-12 16:20:45 +02009007 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009008 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009009 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309010 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009011
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309012 /*
9013 * Reserving delalloc space after obtaining the page lock can lead to
9014 * deadlock. For example, if a dirty page is locked by this function
9015 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9016 * dirty page write out, then the btrfs_writepage() function could
9017 * end up waiting indefinitely to get a lock on the page currently
9018 * being processed by btrfs_page_mkwrite() function.
9019 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009020 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309021 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009022 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009023 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009024 reserved = 1;
9025 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009026 if (ret) {
9027 if (ret == -ENOMEM)
9028 ret = VM_FAULT_OOM;
9029 else /* -ENOSPC, -EIO, etc */
9030 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009031 if (reserved)
9032 goto out;
9033 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009034 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009035
Nick Piggin56a76f82009-03-31 15:23:23 -07009036 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009037again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009038 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009039 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009040
Chris Mason9ebefb182007-06-15 13:50:00 -04009041 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009042 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009043 /* page got truncated out from underneath us */
9044 goto out_unlock;
9045 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009046 wait_on_page_writeback(page);
9047
David Sterbaff13db42015-12-03 14:30:40 +01009048 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009049 set_page_extent_mapped(page);
9050
Chris Masoneb84ae02008-07-17 13:53:27 -04009051 /*
9052 * we can't set the delalloc bits if there are pending ordered
9053 * extents. Drop our locks and wait for them to finish
9054 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009055 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9056 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009057 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009058 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009059 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009060 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009061 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009062 btrfs_put_ordered_extent(ordered);
9063 goto again;
9064 }
9065
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009066 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009067 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009068 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009069 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309070 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009071 btrfs_delalloc_release_space(inode, data_reserved,
9072 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309073 }
9074 }
9075
Josef Bacikfbf19082009-10-01 17:10:23 -04009076 /*
Liu Bo54160342016-12-13 12:15:19 -08009077 * page_mkwrite gets called when the page is firstly dirtied after it's
9078 * faulted in, but write(2) could also dirty a page and set delalloc
9079 * bits, thus in this case for space account reason, we still need to
9080 * clear any delalloc bits within this page range since we have to
9081 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009082 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309083 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009084 EXTENT_DIRTY | EXTENT_DELALLOC |
9085 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009086 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009087
Filipe Mananae3b8a482017-11-04 00:16:59 +00009088 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009089 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009090 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009091 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009092 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009093 ret = VM_FAULT_SIGBUS;
9094 goto out_unlock;
9095 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009096 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009097
9098 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009099 if (page_start + PAGE_SIZE > size)
9100 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009101 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009102 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009103
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009104 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009105 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009106 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009107 flush_dcache_page(page);
9108 kunmap(page);
9109 }
Chris Mason247e7432008-07-17 12:53:51 -04009110 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009111 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009112 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009113
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009114 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009115 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009116 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009117
David Sterbae43bbe52017-12-12 21:43:52 +01009118 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009119
9120out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009121 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009122 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009123 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009124 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009125 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009126 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009127 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009128out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009129 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009130 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9131 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009132out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009133 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009134 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009135 return ret;
9136}
9137
Josef Bacika41ad392011-01-31 15:30:16 -05009138static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009139{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009140 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009141 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009142 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009143 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009144 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009145 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009146 u64 mask = fs_info->sectorsize - 1;
9147 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009148
Josef Bacik0ef8b722013-10-25 16:13:35 -04009149 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9150 (u64)-1);
9151 if (ret)
9152 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009153
Josef Bacikfcb80c22011-05-03 10:40:22 -04009154 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009155 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009156 * 3 things going on here
9157 *
9158 * 1) We need to reserve space for our orphan item and the space to
9159 * delete our orphan item. Lord knows we don't want to have a dangling
9160 * orphan item because we didn't reserve space to remove it.
9161 *
9162 * 2) We need to reserve space to update our inode.
9163 *
9164 * 3) We need to have something to cache all the space that is going to
9165 * be free'd up by the truncate operation, but also have some slack
9166 * space reserved in case it uses space during the truncate (thank you
9167 * very much snapshotting).
9168 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009169 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009170 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009171 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009172 * doesn't end up using space reserved for updating the inode or
9173 * removing the orphan item. We also need to be able to stop the
9174 * transaction and start a new one, which means we need to be able to
9175 * update the inode several times, and we have no idea of knowing how
9176 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009177 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009178 * Then there is the orphan item, which does indeed need to be held on
9179 * to for the whole operation, and we need nobody to touch this reserved
9180 * space except the orphan code.
9181 *
9182 * So that leaves us with
9183 *
9184 * 1) root->orphan_block_rsv - for the orphan deletion.
9185 * 2) rsv - for the truncate reservation, which we will steal from the
9186 * transaction reservation.
9187 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9188 * updating the inode.
9189 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009190 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009191 if (!rsv)
9192 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009193 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009194 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009195
Josef Bacik907cbce2011-08-08 13:46:15 -04009196 /*
Josef Bacik07127182011-08-19 10:29:59 -04009197 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009198 * 1 for updating the inode.
9199 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009200 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009201 if (IS_ERR(trans)) {
9202 err = PTR_ERR(trans);
9203 goto out;
9204 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009205
Josef Bacik907cbce2011-08-08 13:46:15 -04009206 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009207 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009208 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009209 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009210
Chris Mason5a3f23d2009-03-31 13:27:11 -04009211 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009212 * So if we truncate and then write and fsync we normally would just
9213 * write the extents that changed, which is a problem if we need to
9214 * first truncate that entire inode. So set this flag so we write out
9215 * all of the extents in the inode to the sync log so we're completely
9216 * safe.
9217 */
9218 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009219 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009220
Yan, Zheng80825102009-11-12 09:35:36 +00009221 while (1) {
9222 ret = btrfs_truncate_inode_items(trans, root, inode,
9223 inode->i_size,
9224 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009225 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009226 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009227 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009228 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009229 }
Chris Mason39279cc2007-06-12 06:35:45 -04009230
Yan, Zheng80825102009-11-12 09:35:36 +00009231 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009232 if (ret) {
9233 err = ret;
9234 break;
9235 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009236
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009237 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009238 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009239
9240 trans = btrfs_start_transaction(root, 2);
9241 if (IS_ERR(trans)) {
9242 ret = err = PTR_ERR(trans);
9243 trans = NULL;
9244 break;
9245 }
9246
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009247 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009248 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009249 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009250 BUG_ON(ret); /* shouldn't happen */
9251 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009252 }
9253
Josef Bacikddfae632017-10-19 14:16:02 -04009254 /*
9255 * We can't call btrfs_truncate_block inside a trans handle as we could
9256 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9257 * we've truncated everything except the last little bit, and can do
9258 * btrfs_truncate_block and then update the disk_i_size.
9259 */
9260 if (ret == NEED_TRUNCATE_BLOCK) {
9261 btrfs_end_transaction(trans);
9262 btrfs_btree_balance_dirty(fs_info);
9263
9264 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9265 if (ret)
9266 goto out;
9267 trans = btrfs_start_transaction(root, 1);
9268 if (IS_ERR(trans)) {
9269 ret = PTR_ERR(trans);
9270 goto out;
9271 }
9272 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9273 }
9274
Yan, Zheng80825102009-11-12 09:35:36 +00009275 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009276 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009277 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009278 if (ret)
9279 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009280 }
9281
Chris Mason917c16b2011-11-08 14:49:59 -05009282 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009283 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009284 ret = btrfs_update_inode(trans, root, inode);
9285 if (ret && !err)
9286 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009287
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009288 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009289 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009290 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009291out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009292 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009293
Josef Bacik3893e332011-01-31 16:03:11 -05009294 if (ret && !err)
9295 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009296
Josef Bacik3893e332011-01-31 16:03:11 -05009297 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009298}
9299
Sven Wegener3b963622008-06-09 21:57:42 -04009300/*
Chris Masond352ac62008-09-29 15:18:18 -04009301 * create a new subvolume directory/inode (helper for the ioctl).
9302 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009303int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009304 struct btrfs_root *new_root,
9305 struct btrfs_root *parent_root,
9306 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009307{
Chris Mason39279cc2007-06-12 06:35:45 -04009308 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009309 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009310 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009311
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009312 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9313 new_dirid, new_dirid,
9314 S_IFDIR | (~current_umask() & S_IRWXUGO),
9315 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009316 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009317 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009318 inode->i_op = &btrfs_dir_inode_operations;
9319 inode->i_fop = &btrfs_dir_file_operations;
9320
Miklos Szeredibfe86842011-10-28 14:13:29 +02009321 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009322 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009323 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009324
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009325 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9326 if (err)
9327 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009328 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009329 new_root->root_key.objectid, err);
9330
Yan, Zheng76dda932009-09-21 16:00:26 -04009331 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009332
Yan, Zheng76dda932009-09-21 16:00:26 -04009333 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009334 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009335}
9336
Chris Mason39279cc2007-06-12 06:35:45 -04009337struct inode *btrfs_alloc_inode(struct super_block *sb)
9338{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009339 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009340 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009341 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009342
David Sterba712e36c2017-10-31 17:08:27 +01009343 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009344 if (!ei)
9345 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009346
9347 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009348 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009349 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009350 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009351 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009352 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009353 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009354 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009355 ei->disk_i_size = 0;
9356 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009357 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009358 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009359 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009360 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009361 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009362
Josef Bacik9e0baf62011-07-15 15:16:44 +00009363 spin_lock_init(&ei->lock);
9364 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009365 if (sb->s_magic != BTRFS_TEST_MAGIC)
9366 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9367 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009368 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009369 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009370 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009371
Miao Xie16cdcec2011-04-22 18:12:22 +08009372 ei->delayed_node = NULL;
9373
chandan r9cc97d62012-07-04 12:48:07 +05309374 ei->i_otime.tv_sec = 0;
9375 ei->i_otime.tv_nsec = 0;
9376
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009377 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009378 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009379 extent_io_tree_init(&ei->io_tree, inode);
9380 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009381 ei->io_tree.track_uptodate = 1;
9382 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009383 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009384 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009385 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009386 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009387 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009388 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009389 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009390 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009391
9392 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009393}
9394
Josef Bacikaaedb552013-10-11 14:44:09 -04009395#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9396void btrfs_test_destroy_inode(struct inode *inode)
9397{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009398 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009399 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9400}
9401#endif
9402
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009403static void btrfs_i_callback(struct rcu_head *head)
9404{
9405 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009406 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9407}
9408
Chris Mason39279cc2007-06-12 06:35:45 -04009409void btrfs_destroy_inode(struct inode *inode)
9410{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009411 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009412 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009413 struct btrfs_root *root = BTRFS_I(inode)->root;
9414
Al Virob3d9b7a2012-06-09 13:51:19 -04009415 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009416 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009417 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9418 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009419 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009420 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009421 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009422 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009423 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009424
Chris Mason5a3f23d2009-03-31 13:27:11 -04009425 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009426 * This can happen where we create an inode, but somebody else also
9427 * created the same inode and we need to destroy the one we already
9428 * created.
9429 */
9430 if (!root)
9431 goto free;
9432
Josef Bacik8a35d952012-05-23 14:26:42 -04009433 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9434 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009435 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009436 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009437 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009438 }
Josef Bacik7b128762008-07-24 12:17:14 -04009439
Chris Masond3977122009-01-05 21:25:51 -05009440 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009441 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9442 if (!ordered)
9443 break;
9444 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009445 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009446 "found ordered extent %llu %llu on inode cleanup",
9447 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009448 btrfs_remove_ordered_extent(inode, ordered);
9449 btrfs_put_ordered_extent(ordered);
9450 btrfs_put_ordered_extent(ordered);
9451 }
9452 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009453 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009454 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009455 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009456free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009457 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009458}
9459
Al Viro45321ac2010-06-07 13:43:19 -04009460int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009461{
9462 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009463
Naohiro Aota6379ef92013-06-06 09:56:34 +00009464 if (root == NULL)
9465 return 1;
9466
Liu Bofa6ac872013-02-20 14:10:23 +00009467 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009468 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009469 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009470 else
Al Viro45321ac2010-06-07 13:43:19 -04009471 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009472}
9473
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009474static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009475{
9476 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9477
9478 inode_init_once(&ei->vfs_inode);
9479}
9480
David Sterbae67c7182018-02-19 17:24:18 +01009481void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009482{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009483 /*
9484 * Make sure all delayed rcu free inodes are flushed before we
9485 * destroy cache.
9486 */
9487 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009488 kmem_cache_destroy(btrfs_inode_cachep);
9489 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009490 kmem_cache_destroy(btrfs_path_cachep);
9491 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009492}
9493
Liu Bof5c29bd2017-11-02 17:21:50 -06009494int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009495{
David Sterba837e1972012-09-07 03:00:48 -06009496 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009497 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009498 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9499 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009500 if (!btrfs_inode_cachep)
9501 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009502
David Sterba837e1972012-09-07 03:00:48 -06009503 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009504 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009505 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009506 if (!btrfs_trans_handle_cachep)
9507 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009508
David Sterba837e1972012-09-07 03:00:48 -06009509 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009510 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009511 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009512 if (!btrfs_path_cachep)
9513 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009514
David Sterba837e1972012-09-07 03:00:48 -06009515 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009516 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009517 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009518 if (!btrfs_free_space_cachep)
9519 goto fail;
9520
Chris Mason39279cc2007-06-12 06:35:45 -04009521 return 0;
9522fail:
9523 btrfs_destroy_cachep();
9524 return -ENOMEM;
9525}
9526
David Howellsa528d352017-01-31 16:46:22 +00009527static int btrfs_getattr(const struct path *path, struct kstat *stat,
9528 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009529{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009530 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009531 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009532 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009533 u32 bi_flags = BTRFS_I(inode)->flags;
9534
9535 stat->result_mask |= STATX_BTIME;
9536 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9537 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9538 if (bi_flags & BTRFS_INODE_APPEND)
9539 stat->attributes |= STATX_ATTR_APPEND;
9540 if (bi_flags & BTRFS_INODE_COMPRESS)
9541 stat->attributes |= STATX_ATTR_COMPRESSED;
9542 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9543 stat->attributes |= STATX_ATTR_IMMUTABLE;
9544 if (bi_flags & BTRFS_INODE_NODUMP)
9545 stat->attributes |= STATX_ATTR_NODUMP;
9546
9547 stat->attributes_mask |= (STATX_ATTR_APPEND |
9548 STATX_ATTR_COMPRESSED |
9549 STATX_ATTR_IMMUTABLE |
9550 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009551
Chris Mason39279cc2007-06-12 06:35:45 -04009552 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009553 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009554
9555 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009556 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009557 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009558 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009559 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009560 return 0;
9561}
9562
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009563static int btrfs_rename_exchange(struct inode *old_dir,
9564 struct dentry *old_dentry,
9565 struct inode *new_dir,
9566 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009567{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009568 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009569 struct btrfs_trans_handle *trans;
9570 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009571 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009572 struct inode *new_inode = new_dentry->d_inode;
9573 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009574 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009575 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009576 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9577 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009578 u64 old_idx = 0;
9579 u64 new_idx = 0;
9580 u64 root_objectid;
9581 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009582 bool root_log_pinned = false;
9583 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009584
9585 /* we only allow rename subvolume link between subvolumes */
9586 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9587 return -EXDEV;
9588
9589 /* close the race window with snapshot create/destroy ioctl */
9590 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009591 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009592 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009593 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009594
9595 /*
9596 * We want to reserve the absolute worst case amount of items. So if
9597 * both inodes are subvols and we need to unlink them then that would
9598 * require 4 item modifications, but if they are both normal inodes it
9599 * would require 5 item modifications, so we'll assume their normal
9600 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9601 * should cover the worst case number of items we'll modify.
9602 */
9603 trans = btrfs_start_transaction(root, 12);
9604 if (IS_ERR(trans)) {
9605 ret = PTR_ERR(trans);
9606 goto out_notrans;
9607 }
9608
9609 /*
9610 * We need to find a free sequence number both in the source and
9611 * in the destination directory for the exchange.
9612 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009613 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009614 if (ret)
9615 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009616 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009617 if (ret)
9618 goto out_fail;
9619
9620 BTRFS_I(old_inode)->dir_index = 0ULL;
9621 BTRFS_I(new_inode)->dir_index = 0ULL;
9622
9623 /* Reference for the source. */
9624 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9625 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009626 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009627 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009628 btrfs_pin_log_trans(root);
9629 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009630 ret = btrfs_insert_inode_ref(trans, dest,
9631 new_dentry->d_name.name,
9632 new_dentry->d_name.len,
9633 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009634 btrfs_ino(BTRFS_I(new_dir)),
9635 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009636 if (ret)
9637 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009638 }
9639
9640 /* And now for the dest. */
9641 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9642 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009643 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009644 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009645 btrfs_pin_log_trans(dest);
9646 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009647 ret = btrfs_insert_inode_ref(trans, root,
9648 old_dentry->d_name.name,
9649 old_dentry->d_name.len,
9650 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009651 btrfs_ino(BTRFS_I(old_dir)),
9652 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009653 if (ret)
9654 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009655 }
9656
9657 /* Update inode version and ctime/mtime. */
9658 inode_inc_iversion(old_dir);
9659 inode_inc_iversion(new_dir);
9660 inode_inc_iversion(old_inode);
9661 inode_inc_iversion(new_inode);
9662 old_dir->i_ctime = old_dir->i_mtime = ctime;
9663 new_dir->i_ctime = new_dir->i_mtime = ctime;
9664 old_inode->i_ctime = ctime;
9665 new_inode->i_ctime = ctime;
9666
9667 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009668 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9669 BTRFS_I(old_inode), 1);
9670 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9671 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009672 }
9673
9674 /* src is a subvolume */
9675 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9676 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9677 ret = btrfs_unlink_subvol(trans, root, old_dir,
9678 root_objectid,
9679 old_dentry->d_name.name,
9680 old_dentry->d_name.len);
9681 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009682 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9683 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009684 old_dentry->d_name.name,
9685 old_dentry->d_name.len);
9686 if (!ret)
9687 ret = btrfs_update_inode(trans, root, old_inode);
9688 }
9689 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009690 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009691 goto out_fail;
9692 }
9693
9694 /* dest is a subvolume */
9695 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9696 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9697 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9698 root_objectid,
9699 new_dentry->d_name.name,
9700 new_dentry->d_name.len);
9701 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009702 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9703 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009704 new_dentry->d_name.name,
9705 new_dentry->d_name.len);
9706 if (!ret)
9707 ret = btrfs_update_inode(trans, dest, new_inode);
9708 }
9709 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009710 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009711 goto out_fail;
9712 }
9713
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009714 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009715 new_dentry->d_name.name,
9716 new_dentry->d_name.len, 0, old_idx);
9717 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009718 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009719 goto out_fail;
9720 }
9721
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009722 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009723 old_dentry->d_name.name,
9724 old_dentry->d_name.len, 0, new_idx);
9725 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009726 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009727 goto out_fail;
9728 }
9729
9730 if (old_inode->i_nlink == 1)
9731 BTRFS_I(old_inode)->dir_index = old_idx;
9732 if (new_inode->i_nlink == 1)
9733 BTRFS_I(new_inode)->dir_index = new_idx;
9734
Filipe Manana86e8aa02016-05-05 02:02:27 +01009735 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009736 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009737 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9738 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009739 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009740 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009741 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009742 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009743 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009744 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9745 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009746 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009747 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009748 }
9749out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009750 /*
9751 * If we have pinned a log and an error happened, we unpin tasks
9752 * trying to sync the log and force them to fallback to a transaction
9753 * commit if the log currently contains any of the inodes involved in
9754 * this rename operation (to ensure we do not persist a log with an
9755 * inconsistent state for any of these inodes or leading to any
9756 * inconsistencies when replayed). If the transaction was aborted, the
9757 * abortion reason is propagated to userspace when attempting to commit
9758 * the transaction. If the log does not contain any of these inodes, we
9759 * allow the tasks to sync it.
9760 */
9761 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009762 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9763 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9764 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009765 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009766 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009767 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009768
9769 if (root_log_pinned) {
9770 btrfs_end_log_trans(root);
9771 root_log_pinned = false;
9772 }
9773 if (dest_log_pinned) {
9774 btrfs_end_log_trans(dest);
9775 dest_log_pinned = false;
9776 }
9777 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009778 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009779out_notrans:
9780 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009781 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009782 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009783 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009784
9785 return ret;
9786}
9787
9788static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9789 struct btrfs_root *root,
9790 struct inode *dir,
9791 struct dentry *dentry)
9792{
9793 int ret;
9794 struct inode *inode;
9795 u64 objectid;
9796 u64 index;
9797
9798 ret = btrfs_find_free_ino(root, &objectid);
9799 if (ret)
9800 return ret;
9801
9802 inode = btrfs_new_inode(trans, root, dir,
9803 dentry->d_name.name,
9804 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009805 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009806 objectid,
9807 S_IFCHR | WHITEOUT_MODE,
9808 &index);
9809
9810 if (IS_ERR(inode)) {
9811 ret = PTR_ERR(inode);
9812 return ret;
9813 }
9814
9815 inode->i_op = &btrfs_special_inode_operations;
9816 init_special_inode(inode, inode->i_mode,
9817 WHITEOUT_DEV);
9818
9819 ret = btrfs_init_inode_security(trans, inode, dir,
9820 &dentry->d_name);
9821 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009822 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009823
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009824 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9825 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009826 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009827 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009828
9829 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009830out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009831 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009832 if (ret)
9833 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009834 iput(inode);
9835
Filipe Mananac9901612016-05-05 01:41:57 +01009836 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009837}
9838
Chris Mason39279cc2007-06-12 06:35:45 -04009839static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009840 struct inode *new_dir, struct dentry *new_dentry,
9841 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009842{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009843 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009844 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009845 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009846 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9847 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009848 struct inode *new_inode = d_inode(new_dentry);
9849 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009850 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009851 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009852 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009853 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009854 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009855
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009856 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009857 return -EPERM;
9858
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009859 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009860 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009861 return -EXDEV;
9862
Li Zefan33345d012011-04-20 10:31:50 +08009863 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009864 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009865 return -ENOTEMPTY;
9866
Chris Mason39279cc2007-06-12 06:35:45 -04009867 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009868 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009869 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009870
9871
9872 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009873 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009874 new_dentry->d_name.name,
9875 new_dentry->d_name.len);
9876
9877 if (ret) {
9878 if (ret == -EEXIST) {
9879 /* we shouldn't get
9880 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309881 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009882 return ret;
9883 }
9884 } else {
9885 /* maybe -EOVERFLOW */
9886 return ret;
9887 }
9888 }
9889 ret = 0;
9890
Chris Mason5a3f23d2009-03-31 13:27:11 -04009891 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009892 * we're using rename to replace one file with another. Start IO on it
9893 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009894 */
Chris Mason8d875f92014-08-12 10:47:42 -07009895 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009896 filemap_flush(old_inode->i_mapping);
9897
Yan, Zheng76dda932009-09-21 16:00:26 -04009898 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009899 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009900 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009901 /*
9902 * We want to reserve the absolute worst case amount of items. So if
9903 * both inodes are subvols and we need to unlink them then that would
9904 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009905 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009906 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9907 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009908 * If our rename has the whiteout flag, we need more 5 units for the
9909 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9910 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009911 */
Filipe Manana5062af32016-05-05 10:26:26 +01009912 trans_num_items = 11;
9913 if (flags & RENAME_WHITEOUT)
9914 trans_num_items += 5;
9915 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009916 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009917 ret = PTR_ERR(trans);
9918 goto out_notrans;
9919 }
Chris Mason5f39d392007-10-15 16:14:19 -04009920
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009921 if (dest != root)
9922 btrfs_record_root_in_trans(trans, dest);
9923
Nikolay Borisov877574e2017-02-20 13:50:33 +02009924 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009925 if (ret)
9926 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009927
Miao Xie67de1172013-12-26 13:07:06 +08009928 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009929 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009930 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009931 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009932 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009933 btrfs_pin_log_trans(root);
9934 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009935 ret = btrfs_insert_inode_ref(trans, dest,
9936 new_dentry->d_name.name,
9937 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009938 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009939 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009940 if (ret)
9941 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009942 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009943
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009944 inode_inc_iversion(old_dir);
9945 inode_inc_iversion(new_dir);
9946 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009947 old_dir->i_ctime = old_dir->i_mtime =
9948 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009949 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009950
Chris Mason12fcfd22009-03-24 10:24:20 -04009951 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009952 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9953 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009954
Li Zefan33345d012011-04-20 10:31:50 +08009955 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009956 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9957 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9958 old_dentry->d_name.name,
9959 old_dentry->d_name.len);
9960 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009961 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9962 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009963 old_dentry->d_name.name,
9964 old_dentry->d_name.len);
9965 if (!ret)
9966 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009967 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009968 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009969 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009970 goto out_fail;
9971 }
Chris Mason39279cc2007-06-12 06:35:45 -04009972
9973 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009974 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009975 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009976 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009977 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9978 root_objectid = BTRFS_I(new_inode)->location.objectid;
9979 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9980 root_objectid,
9981 new_dentry->d_name.name,
9982 new_dentry->d_name.len);
9983 BUG_ON(new_inode->i_nlink == 0);
9984 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009985 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9986 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009987 new_dentry->d_name.name,
9988 new_dentry->d_name.len);
9989 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009990 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009991 ret = btrfs_orphan_add(trans,
9992 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009993 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009994 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009995 goto out_fail;
9996 }
Chris Mason39279cc2007-06-12 06:35:45 -04009997 }
Josef Bacikaec74772008-07-24 12:12:38 -04009998
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009999 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010000 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010001 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010002 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010003 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010004 goto out_fail;
10005 }
Chris Mason39279cc2007-06-12 06:35:45 -040010006
Miao Xie67de1172013-12-26 13:07:06 +080010007 if (old_inode->i_nlink == 1)
10008 BTRFS_I(old_inode)->dir_index = index;
10009
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010010 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010011 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010012
David Sterbaf85b7372017-01-20 14:54:07 +010010013 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10014 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010015 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010016 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010017 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010018
10019 if (flags & RENAME_WHITEOUT) {
10020 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10021 old_dentry);
10022
10023 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010024 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010025 goto out_fail;
10026 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010027 }
Chris Mason39279cc2007-06-12 06:35:45 -040010028out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010029 /*
10030 * If we have pinned the log and an error happened, we unpin tasks
10031 * trying to sync the log and force them to fallback to a transaction
10032 * commit if the log currently contains any of the inodes involved in
10033 * this rename operation (to ensure we do not persist a log with an
10034 * inconsistent state for any of these inodes or leading to any
10035 * inconsistencies when replayed). If the transaction was aborted, the
10036 * abortion reason is propagated to userspace when attempting to commit
10037 * the transaction. If the log does not contain any of these inodes, we
10038 * allow the tasks to sync it.
10039 */
10040 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010041 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10042 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10043 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010044 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010045 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010046 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010047
10048 btrfs_end_log_trans(root);
10049 log_pinned = false;
10050 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010051 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010052out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010053 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010054 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010055
Chris Mason39279cc2007-06-12 06:35:45 -040010056 return ret;
10057}
10058
Miklos Szeredi80ace852014-07-23 15:15:32 +020010059static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10060 struct inode *new_dir, struct dentry *new_dentry,
10061 unsigned int flags)
10062{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010063 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010064 return -EINVAL;
10065
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010066 if (flags & RENAME_EXCHANGE)
10067 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10068 new_dentry);
10069
10070 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010071}
10072
Miao Xie8ccf6f192012-10-25 09:28:04 +000010073static void btrfs_run_delalloc_work(struct btrfs_work *work)
10074{
10075 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010076 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010077
10078 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10079 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010080 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010081 filemap_flush(inode->i_mapping);
10082 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10083 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010084 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010085
10086 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010087 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010088 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010089 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010090 complete(&delalloc_work->completion);
10091}
10092
10093struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010094 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010095{
10096 struct btrfs_delalloc_work *work;
10097
David Sterba100d5702015-12-08 14:39:32 +010010098 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010099 if (!work)
10100 return NULL;
10101
10102 init_completion(&work->completion);
10103 INIT_LIST_HEAD(&work->list);
10104 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010105 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010106 WARN_ON_ONCE(!inode);
10107 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10108 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010109
10110 return work;
10111}
10112
10113void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10114{
10115 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010116 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010117}
10118
Chris Masond352ac62008-09-29 15:18:18 -040010119/*
10120 * some fairly slow code that needs optimization. This walks the list
10121 * of all the inodes with pending delalloc and forces them to disk.
10122 */
Miao Xie6c255e62014-03-06 13:55:01 +080010123static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10124 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010125{
Chris Masonea8c2812008-08-04 23:17:27 -040010126 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010127 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010128 struct btrfs_delalloc_work *work, *next;
10129 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010130 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010131 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010132
Miao Xie8ccf6f192012-10-25 09:28:04 +000010133 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010134 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010135
Miao Xie573bfb72014-03-06 13:55:03 +080010136 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010137 spin_lock(&root->delalloc_lock);
10138 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010139 while (!list_empty(&splice)) {
10140 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010141 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010142
Miao Xieeb73c1b2013-05-15 07:48:22 +000010143 list_move_tail(&binode->delalloc_inodes,
10144 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010145 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010146 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010147 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010148 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010149 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010150 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010151
David Sterba651d4942015-11-27 19:24:16 +010010152 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010153 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010154 if (delay_iput)
10155 btrfs_add_delayed_iput(inode);
10156 else
10157 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010158 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010159 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010160 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010161 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010162 btrfs_queue_work(root->fs_info->flush_workers,
10163 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010164 ret++;
10165 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010166 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010167 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010168 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010169 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010170 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010171
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010172out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010173 list_for_each_entry_safe(work, next, &works, list) {
10174 list_del_init(&work->list);
10175 btrfs_wait_and_free_delalloc_work(work);
10176 }
10177
Miao Xieeb73c1b2013-05-15 07:48:22 +000010178 if (!list_empty_careful(&splice)) {
10179 spin_lock(&root->delalloc_lock);
10180 list_splice_tail(&splice, &root->delalloc_inodes);
10181 spin_unlock(&root->delalloc_lock);
10182 }
Miao Xie573bfb72014-03-06 13:55:03 +080010183 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010184 return ret;
10185}
10186
10187int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10188{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010189 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010190 int ret;
10191
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010192 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010193 return -EROFS;
10194
Miao Xie6c255e62014-03-06 13:55:01 +080010195 ret = __start_delalloc_inodes(root, delay_iput, -1);
10196 if (ret > 0)
10197 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010198 return ret;
10199}
10200
Miao Xie6c255e62014-03-06 13:55:01 +080010201int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10202 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010203{
10204 struct btrfs_root *root;
10205 struct list_head splice;
10206 int ret;
10207
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010208 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010209 return -EROFS;
10210
10211 INIT_LIST_HEAD(&splice);
10212
Miao Xie573bfb72014-03-06 13:55:03 +080010213 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010214 spin_lock(&fs_info->delalloc_root_lock);
10215 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010216 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010217 root = list_first_entry(&splice, struct btrfs_root,
10218 delalloc_root);
10219 root = btrfs_grab_fs_root(root);
10220 BUG_ON(!root);
10221 list_move_tail(&root->delalloc_root,
10222 &fs_info->delalloc_roots);
10223 spin_unlock(&fs_info->delalloc_root_lock);
10224
Miao Xie6c255e62014-03-06 13:55:01 +080010225 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010226 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010227 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010228 goto out;
10229
Miao Xie6c255e62014-03-06 13:55:01 +080010230 if (nr != -1) {
10231 nr -= ret;
10232 WARN_ON(nr < 0);
10233 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010234 spin_lock(&fs_info->delalloc_root_lock);
10235 }
10236 spin_unlock(&fs_info->delalloc_root_lock);
10237
Miao Xie6c255e62014-03-06 13:55:01 +080010238 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010239out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010240 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010241 spin_lock(&fs_info->delalloc_root_lock);
10242 list_splice_tail(&splice, &fs_info->delalloc_roots);
10243 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010244 }
Miao Xie573bfb72014-03-06 13:55:03 +080010245 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010246 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010247}
10248
Chris Mason39279cc2007-06-12 06:35:45 -040010249static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10250 const char *symname)
10251{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010252 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010253 struct btrfs_trans_handle *trans;
10254 struct btrfs_root *root = BTRFS_I(dir)->root;
10255 struct btrfs_path *path;
10256 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010257 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010258 int err;
10259 int drop_inode = 0;
10260 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010261 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010262 int name_len;
10263 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010264 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010265 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010266 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010267
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010268 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010269 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010270 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010271
Josef Bacik9ed74f22009-09-11 16:12:44 -040010272 /*
10273 * 2 items for inode item and ref
10274 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010275 * 1 item for updating parent inode item
10276 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010277 * 1 item for xattr if selinux is on
10278 */
Filipe Manana9269d122015-12-31 18:16:29 +000010279 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010280 if (IS_ERR(trans))
10281 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010282
Li Zefan581bb052011-04-20 10:06:11 +080010283 err = btrfs_find_free_ino(root, &objectid);
10284 if (err)
10285 goto out_unlock;
10286
Josef Bacikaec74772008-07-24 12:12:38 -040010287 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010288 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10289 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010290 if (IS_ERR(inode)) {
10291 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010292 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010293 }
Chris Mason39279cc2007-06-12 06:35:45 -040010294
Casey Schauflerad19db72011-12-15 10:09:07 -050010295 /*
10296 * If the active LSM wants to access the inode during
10297 * d_instantiate it needs these. Smack checks to see
10298 * if the filesystem supports xattrs by looking at the
10299 * ops vector.
10300 */
10301 inode->i_fop = &btrfs_file_operations;
10302 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010303 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010304 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10305
10306 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10307 if (err)
10308 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010309
Chris Mason39279cc2007-06-12 06:35:45 -040010310 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010311 if (!path) {
10312 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010313 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010314 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010315 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010316 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010317 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010318 datasize = btrfs_file_extent_calc_inline_size(name_len);
10319 err = btrfs_insert_empty_item(trans, root, path, &key,
10320 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010321 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010322 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010323 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010324 }
Chris Mason5f39d392007-10-15 16:14:19 -040010325 leaf = path->nodes[0];
10326 ei = btrfs_item_ptr(leaf, path->slots[0],
10327 struct btrfs_file_extent_item);
10328 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10329 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010330 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010331 btrfs_set_file_extent_encryption(leaf, ei, 0);
10332 btrfs_set_file_extent_compression(leaf, ei, 0);
10333 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10334 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10335
Chris Mason39279cc2007-06-12 06:35:45 -040010336 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010337 write_extent_buffer(leaf, symname, ptr, name_len);
10338 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010339 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010340
Chris Mason39279cc2007-06-12 06:35:45 -040010341 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010342 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010343 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010344 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010345 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010346 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010347 /*
10348 * Last step, add directory indexes for our symlink inode. This is the
10349 * last step to avoid extra cleanup of these indexes if an error happens
10350 * elsewhere above.
10351 */
10352 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010353 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10354 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010355 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010356 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010357 goto out_unlock_inode;
10358 }
10359
10360 unlock_new_inode(inode);
10361 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010362
10363out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010364 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010365 if (drop_inode) {
10366 inode_dec_link_count(inode);
10367 iput(inode);
10368 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010369 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010370 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010371
10372out_unlock_inode:
10373 drop_inode = 1;
10374 unlock_new_inode(inode);
10375 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010376}
Chris Mason16432982008-04-10 10:23:21 -040010377
Josef Bacik0af3d002010-06-21 14:48:16 -040010378static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10379 u64 start, u64 num_bytes, u64 min_size,
10380 loff_t actual_len, u64 *alloc_hint,
10381 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010382{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010383 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010384 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10385 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010386 struct btrfs_root *root = BTRFS_I(inode)->root;
10387 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010388 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010389 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010390 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010391 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010392 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010393 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010394 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010395
Josef Bacik0af3d002010-06-21 14:48:16 -040010396 if (trans)
10397 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010398 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010399 if (own_trans) {
10400 trans = btrfs_start_transaction(root, 3);
10401 if (IS_ERR(trans)) {
10402 ret = PTR_ERR(trans);
10403 break;
10404 }
Yan Zhengd899e052008-10-30 14:25:28 -040010405 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010406
Byongho Leeee221842015-12-15 01:42:10 +090010407 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010408 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010409 /*
10410 * If we are severely fragmented we could end up with really
10411 * small allocations, so if the allocator is returning small
10412 * chunks lets make its job easier by only searching for those
10413 * sized chunks.
10414 */
10415 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010416 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10417 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010418 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010419 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010420 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010421 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010422 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010423 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010424
Josef Bacik0b670dc2015-09-23 17:11:16 -040010425 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010426 ret = insert_reserved_file_extent(trans, inode,
10427 cur_offset, ins.objectid,
10428 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010429 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010430 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010431 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010432 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010433 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010434 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010435 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010436 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010437 break;
10438 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010439
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010440 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010441 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010442
Josef Bacik5dc562c2012-08-17 13:14:17 -040010443 em = alloc_extent_map();
10444 if (!em) {
10445 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10446 &BTRFS_I(inode)->runtime_flags);
10447 goto next;
10448 }
10449
10450 em->start = cur_offset;
10451 em->orig_start = cur_offset;
10452 em->len = ins.offset;
10453 em->block_start = ins.objectid;
10454 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010455 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010456 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010457 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010458 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10459 em->generation = trans->transid;
10460
10461 while (1) {
10462 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010463 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010464 write_unlock(&em_tree->lock);
10465 if (ret != -EEXIST)
10466 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010467 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010468 cur_offset + ins.offset - 1,
10469 0);
10470 }
10471 free_extent_map(em);
10472next:
Yan Zhengd899e052008-10-30 14:25:28 -040010473 num_bytes -= ins.offset;
10474 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010475 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010476
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010477 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010478 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010479 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010480 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010481 (actual_len > inode->i_size) &&
10482 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010483 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010484 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010485 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010486 i_size = cur_offset;
10487 i_size_write(inode, i_size);
10488 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010489 }
10490
Yan Zhengd899e052008-10-30 14:25:28 -040010491 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010492
10493 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010494 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010495 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010496 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010497 break;
10498 }
Yan Zhengd899e052008-10-30 14:25:28 -040010499
Josef Bacik0af3d002010-06-21 14:48:16 -040010500 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010501 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010502 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010503 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010504 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010505 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010506 return ret;
10507}
10508
Josef Bacik0af3d002010-06-21 14:48:16 -040010509int btrfs_prealloc_file_range(struct inode *inode, int mode,
10510 u64 start, u64 num_bytes, u64 min_size,
10511 loff_t actual_len, u64 *alloc_hint)
10512{
10513 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10514 min_size, actual_len, alloc_hint,
10515 NULL);
10516}
10517
10518int btrfs_prealloc_file_range_trans(struct inode *inode,
10519 struct btrfs_trans_handle *trans, int mode,
10520 u64 start, u64 num_bytes, u64 min_size,
10521 loff_t actual_len, u64 *alloc_hint)
10522{
10523 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10524 min_size, actual_len, alloc_hint, trans);
10525}
10526
Chris Masone6dcd2d2008-07-17 12:53:50 -040010527static int btrfs_set_page_dirty(struct page *page)
10528{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010529 return __set_page_dirty_nobuffers(page);
10530}
10531
Al Viro10556cb2011-06-20 19:28:19 -040010532static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010533{
Li Zefanb83cc962010-12-20 16:04:08 +080010534 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010535 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010536
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010537 if (mask & MAY_WRITE &&
10538 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10539 if (btrfs_root_readonly(root))
10540 return -EROFS;
10541 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10542 return -EACCES;
10543 }
Al Viro2830ba72011-06-20 19:16:29 -040010544 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010545}
Chris Mason39279cc2007-06-12 06:35:45 -040010546
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010547static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10548{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010549 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010550 struct btrfs_trans_handle *trans;
10551 struct btrfs_root *root = BTRFS_I(dir)->root;
10552 struct inode *inode = NULL;
10553 u64 objectid;
10554 u64 index;
10555 int ret = 0;
10556
10557 /*
10558 * 5 units required for adding orphan entry
10559 */
10560 trans = btrfs_start_transaction(root, 5);
10561 if (IS_ERR(trans))
10562 return PTR_ERR(trans);
10563
10564 ret = btrfs_find_free_ino(root, &objectid);
10565 if (ret)
10566 goto out;
10567
10568 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010569 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010570 if (IS_ERR(inode)) {
10571 ret = PTR_ERR(inode);
10572 inode = NULL;
10573 goto out;
10574 }
10575
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010576 inode->i_fop = &btrfs_file_operations;
10577 inode->i_op = &btrfs_file_inode_operations;
10578
10579 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010580 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10581
Chris Masonb0d5d102014-09-08 13:08:51 -070010582 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10583 if (ret)
10584 goto out_inode;
10585
10586 ret = btrfs_update_inode(trans, root, inode);
10587 if (ret)
10588 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010589 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010590 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010591 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010592
Filipe Manana5762b5c2014-08-01 00:10:32 +010010593 /*
10594 * We set number of links to 0 in btrfs_new_inode(), and here we set
10595 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10596 * through:
10597 *
10598 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10599 */
10600 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010601 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010602 d_tmpfile(dentry, inode);
10603 mark_inode_dirty(inode);
10604
10605out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010606 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010607 if (ret)
10608 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010609 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010610 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010611
10612out_inode:
10613 unlock_new_inode(inode);
10614 goto out;
10615
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010616}
10617
David Sterba20a7db82017-02-17 16:24:29 +010010618__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010619static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010620{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010621 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010622}
10623
Josef Bacikc6100a42017-05-05 11:57:13 -040010624static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10625{
10626 struct inode *inode = private_data;
10627 return btrfs_sb(inode->i_sb);
10628}
10629
10630static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10631 u64 start, u64 end)
10632{
10633 struct inode *inode = private_data;
10634 u64 isize;
10635
10636 isize = i_size_read(inode);
10637 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10638 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10639 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10640 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10641 }
10642}
10643
10644void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10645{
10646 struct inode *inode = private_data;
10647 unsigned long index = start >> PAGE_SHIFT;
10648 unsigned long end_index = end >> PAGE_SHIFT;
10649 struct page *page;
10650
10651 while (index <= end_index) {
10652 page = find_get_page(inode->i_mapping, index);
10653 ASSERT(page); /* Pages should be in the extent_io_tree */
10654 set_page_writeback(page);
10655 put_page(page);
10656 index++;
10657 }
10658}
10659
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010660static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010661 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010662 .lookup = btrfs_lookup,
10663 .create = btrfs_create,
10664 .unlink = btrfs_unlink,
10665 .link = btrfs_link,
10666 .mkdir = btrfs_mkdir,
10667 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010668 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010669 .symlink = btrfs_symlink,
10670 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010671 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010672 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010673 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010674 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010675 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010676 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010677 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010678};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010679static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010680 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010681 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010682 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010683};
Yan, Zheng76dda932009-09-21 16:00:26 -040010684
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010685static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010686 .llseek = generic_file_llseek,
10687 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010688 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010689 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010690 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010691#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010692 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010693#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010694 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010695 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010696};
10697
David Sterba20e55062015-11-19 11:42:28 +010010698static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010699 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010700 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010701 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010702 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010703 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010704 .tree_fs_info = iotree_fs_info,
10705 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010706
10707 /* optional callbacks */
10708 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010709 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010710 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010711 .set_bit_hook = btrfs_set_bit_hook,
10712 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010713 .merge_extent_hook = btrfs_merge_extent_hook,
10714 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010715 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010716};
10717
Chris Mason35054392009-01-21 13:11:13 -050010718/*
10719 * btrfs doesn't support the bmap operation because swapfiles
10720 * use bmap to make a mapping of extents in the file. They assume
10721 * these extents won't change over the life of the file and they
10722 * use the bmap result to do IO directly to the drive.
10723 *
10724 * the btrfs bmap call would return logical addresses that aren't
10725 * suitable for IO and they also will change frequently as COW
10726 * operations happen. So, swapfile + btrfs == corruption.
10727 *
10728 * For now we're avoiding this by dropping bmap.
10729 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010730static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010731 .readpage = btrfs_readpage,
10732 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010733 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010734 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010735 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010736 .invalidatepage = btrfs_invalidatepage,
10737 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010738 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010739 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010740};
10741
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010742static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010743 .readpage = btrfs_readpage,
10744 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010745 .invalidatepage = btrfs_invalidatepage,
10746 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010747};
10748
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010749static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010750 .getattr = btrfs_getattr,
10751 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010752 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010753 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010754 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010755 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010756 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010757 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010758};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010759static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010760 .getattr = btrfs_getattr,
10761 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010762 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010763 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010764 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010765 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010766 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010767};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010768static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010769 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010770 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010771 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010772 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010773 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010774 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010775};
Yan, Zheng76dda932009-09-21 16:00:26 -040010776
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010777const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010778 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010779 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010780};