blob: c5f31817778bc1fe9dc5027153de3bfcd2cc759a [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040045#include <linux/magic.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080063#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040064
65struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080066 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040067 struct btrfs_root *root;
68};
69
Filipe Mananaf28a4922015-12-08 19:23:20 +000070struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000071 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040089struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050090struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040091
92#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010093static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040094 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
95 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
96 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
97 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
98 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
99 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
100 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
101};
102
Eric Sandeen3972f262013-01-12 02:57:22 +0000103static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500104static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400105static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500106static noinline int cow_file_range(struct inode *inode,
107 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800108 u64 start, u64 end, u64 delalloc_end,
109 int *page_started, unsigned long *nr_written,
110 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800111static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
112 u64 orig_start, u64 block_start,
113 u64 block_len, u64 orig_block_len,
114 u64 ram_bytes, int compress_type,
115 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400116
Qu Wenruo524272602017-03-08 10:25:52 +0800117static void __endio_write_update_ordered(struct inode *inode,
118 const u64 offset, const u64 bytes,
119 const bool uptodate);
120
121/*
122 * Cleanup all submitted ordered extents in specified range to handle errors
123 * from the fill_dellaloc() callback.
124 *
125 * NOTE: caller must ensure that when an error happens, it can not call
126 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
127 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
128 * to be released, which we want to happen only when finishing the ordered
129 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
130 * fill_delalloc() callback already does proper cleanup for the first page of
131 * the range, that is, it invokes the callback writepage_end_io_hook() for the
132 * range of the first page.
133 */
134static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
135 const u64 offset,
136 const u64 bytes)
137{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900138 unsigned long index = offset >> PAGE_SHIFT;
139 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
140 struct page *page;
141
142 while (index <= end_index) {
143 page = find_get_page(inode->i_mapping, index);
144 index++;
145 if (!page)
146 continue;
147 ClearPagePrivate2(page);
148 put_page(page);
149 }
Qu Wenruo524272602017-03-08 10:25:52 +0800150 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
151 bytes - PAGE_SIZE, false);
152}
153
Eric Sandeen48a3b632013-04-25 20:41:01 +0000154static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400155
Josef Bacik6a3891c2015-03-16 17:38:52 -0400156#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
157void btrfs_test_inode_set_ops(struct inode *inode)
158{
159 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
160}
161#endif
162
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000163static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500164 struct inode *inode, struct inode *dir,
165 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500166{
167 int err;
168
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000169 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500170 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500171 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500172 return err;
173}
174
Chris Masond352ac62008-09-29 15:18:18 -0400175/*
Chris Masonc8b97812008-10-29 14:49:59 -0400176 * this does all the hard work for inserting an inline extent into
177 * the btree. The caller should have done a btrfs_drop_extents so that
178 * no overlapping inline items exist in the btree
179 */
Chris Mason40f76582014-05-21 13:35:51 -0700180static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000181 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400182 struct btrfs_root *root, struct inode *inode,
183 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000184 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400185 struct page **compressed_pages)
186{
Chris Masonc8b97812008-10-29 14:49:59 -0400187 struct extent_buffer *leaf;
188 struct page *page = NULL;
189 char *kaddr;
190 unsigned long ptr;
191 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400192 int ret;
193 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400194 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400195
Li Zefanfe3f5662011-03-28 08:30:38 +0000196 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400198
Chris Masonc8b97812008-10-29 14:49:59 -0400199 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000200
201 if (!extent_inserted) {
202 struct btrfs_key key;
203 size_t datasize;
204
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200205 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200207 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000208
209 datasize = btrfs_file_extent_calc_inline_size(cur_size);
210 path->leave_spinning = 1;
211 ret = btrfs_insert_empty_item(trans, root, path, &key,
212 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200213 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000214 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400215 }
216 leaf = path->nodes[0];
217 ei = btrfs_item_ptr(leaf, path->slots[0],
218 struct btrfs_file_extent_item);
219 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
220 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
221 btrfs_set_file_extent_encryption(leaf, ei, 0);
222 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
223 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
224 ptr = btrfs_file_extent_inline_start(ei);
225
Li Zefan261507a02010-12-17 14:21:50 +0800226 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400227 struct page *cpage;
228 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500229 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400230 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500231 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300232 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400233
Cong Wang7ac687d2011-11-25 23:14:28 +0800234 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800236 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400237
238 i++;
239 ptr += cur_size;
240 compressed_size -= cur_size;
241 }
242 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800243 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400244 } else {
245 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300246 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800248 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300249 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400250 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800251 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300252 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400253 }
254 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000255 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400256
Yan, Zhengc2167752009-11-12 09:34:21 +0000257 /*
258 * we're an inline extent, so nobody can
259 * extend the file past i_size without locking
260 * a page we already have locked.
261 *
262 * We must do any isize and inode updates
263 * before we unlock the pages. Otherwise we
264 * could end up racing with unlink.
265 */
Chris Masonc8b97812008-10-29 14:49:59 -0400266 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000268
Chris Masonc8b97812008-10-29 14:49:59 -0400269fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200270 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400271}
272
273
274/*
275 * conditionally insert an inline extent into the file. This
276 * does the checks required to make sure the data is small enough
277 * to fit as an inline extent.
278 */
Josef Bacik00361582013-08-14 14:02:47 -0400279static noinline int cow_file_range_inline(struct btrfs_root *root,
280 struct inode *inode, u64 start,
281 u64 end, size_t compressed_size,
282 int compress_type,
283 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400284{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400285 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400286 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400287 u64 isize = i_size_read(inode);
288 u64 actual_end = min(end + 1, isize);
289 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400291 u64 data_len = inline_len;
292 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000293 struct btrfs_path *path;
294 int extent_inserted = 0;
295 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400296
297 if (compressed_size)
298 data_len = compressed_size;
299
300 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400301 actual_end > fs_info->sectorsize ||
302 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400303 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400305 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400306 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400307 return 1;
308 }
309
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 path = btrfs_alloc_path();
311 if (!path)
312 return -ENOMEM;
313
Josef Bacik00361582013-08-14 14:02:47 -0400314 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000315 if (IS_ERR(trans)) {
316 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400317 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000318 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400319 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400320
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000321 if (compressed_size && compressed_pages)
322 extent_item_size = btrfs_file_extent_calc_inline_size(
323 compressed_size);
324 else
325 extent_item_size = btrfs_file_extent_calc_inline_size(
326 inline_len);
327
328 ret = __btrfs_drop_extents(trans, root, inode, path,
329 start, aligned_end, NULL,
330 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400331 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400332 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400333 goto out;
334 }
Chris Masonc8b97812008-10-29 14:49:59 -0400335
336 if (isize > actual_end)
337 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000338 ret = insert_inline_extent(trans, path, extent_inserted,
339 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400340 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000341 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400342 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400343 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400344 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400345 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400346 ret = 1;
347 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400349
Josef Bacikbdc20e62013-02-28 13:23:38 -0500350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200351 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400352out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800353 /*
354 * Don't forget to free the reserved space, as for inlined extent
355 * it won't count as data extent, free them directly here.
356 * And at reserve time, it's always aligned to page size, so
357 * just free one page here.
358 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800359 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000360 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400361 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400362 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400363}
364
Chris Mason771ed682008-11-06 22:02:51 -0500365struct async_extent {
366 u64 start;
367 u64 ram_size;
368 u64 compressed_size;
369 struct page **pages;
370 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800371 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500372 struct list_head list;
373};
374
375struct async_cow {
376 struct inode *inode;
377 struct btrfs_root *root;
378 struct page *locked_page;
379 u64 start;
380 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600381 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500382 struct list_head extents;
383 struct btrfs_work work;
384};
385
386static noinline int add_async_extent(struct async_cow *cow,
387 u64 start, u64 ram_size,
388 u64 compressed_size,
389 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800390 unsigned long nr_pages,
391 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500392{
393 struct async_extent *async_extent;
394
395 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100396 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500397 async_extent->start = start;
398 async_extent->ram_size = ram_size;
399 async_extent->compressed_size = compressed_size;
400 async_extent->pages = pages;
401 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800402 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500403 list_add_tail(&async_extent->list, &cow->extents);
404 return 0;
405}
406
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300407static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400409 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800410
411 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400412 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800413 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200414 /* defrag ioctl */
415 if (BTRFS_I(inode)->defrag_compress)
416 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800417 /* bad compression ratios */
418 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
419 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400420 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800421 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200422 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300423 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800424 return 0;
425}
426
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200427static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800428 u64 start, u64 end, u64 num_bytes, u64 small_write)
429{
430 /* If this is a small write inside eof, kick off a defrag */
431 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200432 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800433 btrfs_add_inode_defrag(NULL, inode);
434}
435
Chris Masonc8b97812008-10-29 14:49:59 -0400436/*
Chris Mason771ed682008-11-06 22:02:51 -0500437 * we create compressed extents in two phases. The first
438 * phase compresses a range of pages that have already been
439 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400440 *
Chris Mason771ed682008-11-06 22:02:51 -0500441 * This is done inside an ordered work queue, and the compression
442 * is spread across many cpus. The actual IO submission is step
443 * two, and the ordered work queue takes care of making sure that
444 * happens in the same order things were put onto the queue by
445 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400446 *
Chris Mason771ed682008-11-06 22:02:51 -0500447 * If this code finds it can't get good compression, it puts an
448 * entry onto the work queue to write the uncompressed bytes. This
449 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300450 * are written in the same order that the flusher thread sent them
451 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400452 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100453static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500454 struct page *locked_page,
455 u64 start, u64 end,
456 struct async_cow *async_cow,
457 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400458{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400459 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400460 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400461 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400462 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500463 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400464 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400465 struct page **pages = NULL;
466 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400467 unsigned long total_compressed = 0;
468 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400469 int i;
470 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400471 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400472 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400473
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200474 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
475 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400476
Chris Mason42dc7ba2008-12-15 11:44:56 -0500477 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400478again:
479 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300480 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100481 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
482 nr_pages = min_t(unsigned long, nr_pages,
483 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400484
Chris Masonf03d9301f2009-02-04 09:31:06 -0500485 /*
486 * we don't want to send crud past the end of i_size through
487 * compression, that's just a waste of CPU time. So, if the
488 * end of the file is before the start of our current
489 * requested range of bytes, we bail out to the uncompressed
490 * cleanup code that can deal with all of this.
491 *
492 * It isn't really the fastest way to fix things, but this is a
493 * very uncommon corner.
494 */
495 if (actual_end <= start)
496 goto cleanup_and_bail_uncompressed;
497
Chris Masonc8b97812008-10-29 14:49:59 -0400498 total_compressed = actual_end - start;
499
Shilong Wang4bcbb332014-10-07 18:44:35 -0400500 /*
501 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400502 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400503 */
504 if (total_compressed <= blocksize &&
505 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
506 goto cleanup_and_bail_uncompressed;
507
David Sterba069eac72017-02-14 19:36:54 +0100508 total_compressed = min_t(unsigned long, total_compressed,
509 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400510 total_in = 0;
511 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400512
Chris Mason771ed682008-11-06 22:02:51 -0500513 /*
514 * we do compression for mount -o compress and when the
515 * inode has not been flagged as nocompress. This flag can
516 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400517 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300518 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400519 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100520 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800521 if (!pages) {
522 /* just bail out to the uncompressed code */
523 goto cont;
524 }
Chris Mason179e29e2007-11-01 11:28:41 -0400525
David Sterbaeec63c62017-07-17 19:41:31 +0200526 if (BTRFS_I(inode)->defrag_compress)
527 compress_type = BTRFS_I(inode)->defrag_compress;
528 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200529 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800530
Chris Mason4adaa612013-03-26 13:07:00 -0400531 /*
532 * we need to call clear_page_dirty_for_io on each
533 * page in the range. Otherwise applications with the file
534 * mmap'd can wander in and change the page contents while
535 * we are compressing them.
536 *
537 * If the compression fails for any reason, we set the pages
538 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300539 *
540 * Note that the remaining part is redirtied, the start pointer
541 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400542 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300543 if (!redirty) {
544 extent_range_clear_dirty_for_io(inode, start, end);
545 redirty = 1;
546 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200547
548 /* Compression level is applied here and only here */
549 ret = btrfs_compress_pages(
550 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800551 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100552 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100553 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800554 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100555 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400556
557 if (!ret) {
558 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300559 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100560 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400561 char *kaddr;
562
563 /* zero the tail end of the last page, we might be
564 * sending it down to disk
565 */
566 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800567 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400568 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300569 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800570 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400571 }
572 will_compress = 1;
573 }
574 }
Li Zefan560f7d72011-09-08 10:22:01 +0800575cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400576 if (start == 0) {
577 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300578 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400579 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500580 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400581 */
Josef Bacik00361582013-08-14 14:02:47 -0400582 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800583 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400584 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500585 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400586 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000587 total_compressed,
588 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400589 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100590 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400591 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400592 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
593 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100594 unsigned long page_error_op;
595
Filipe Mananae6eb4312014-10-10 10:45:12 +0100596 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400597
Chris Mason771ed682008-11-06 22:02:51 -0500598 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100599 * inline extent creation worked or returned error,
600 * we don't need to create any more async work items.
601 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400602 *
603 * We use DO_ACCOUNTING here because we need the
604 * delalloc_release_metadata to be done _after_ we drop
605 * our outstanding extent for clearing delalloc for this
606 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500607 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800608 extent_clear_unlock_delalloc(inode, start, end, end,
609 NULL, clear_flags,
610 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400611 PAGE_CLEAR_DIRTY |
612 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100613 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400614 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400615 goto free_pages_out;
616 }
617 }
618
619 if (will_compress) {
620 /*
621 * we aren't doing an inline extent round the compressed size
622 * up to a block size boundary so the allocator does sane
623 * things
624 */
Qu Wenruofda28322013-02-26 08:10:22 +0000625 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400626
627 /*
628 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300629 * win, compare the page count read with the blocks on disk,
630 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400631 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300632 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300633 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700634 *num_added += 1;
635
636 /*
637 * The async work queues will take care of doing actual
638 * allocation on disk for these compressed pages, and
639 * will submit them to the elevator.
640 */
Timofey Titovets11708622017-10-03 18:06:01 +0300641 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100642 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700643 compress_type);
644
Timofey Titovets11708622017-10-03 18:06:01 +0300645 if (start + total_in < end) {
646 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700647 pages = NULL;
648 cond_resched();
649 goto again;
650 }
651 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400652 }
653 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700654 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400655 /*
656 * the compression code ran but failed to make things smaller,
657 * free any pages it allocated and our page pointer array
658 */
David Sterba4d3a8002017-02-14 19:04:07 +0100659 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400660 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300661 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400662 }
663 kfree(pages);
664 pages = NULL;
665 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100666 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400667
668 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400669 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200670 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500671 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500672 }
Chris Masonc8b97812008-10-29 14:49:59 -0400673 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500674cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700675 /*
676 * No compression, but we still need to write the pages in the file
677 * we've been given so far. redirty the locked page if it corresponds
678 * to our extent and set things up for the async work queue to run
679 * cow_file_range to do the normal delalloc dance.
680 */
681 if (page_offset(locked_page) >= start &&
682 page_offset(locked_page) <= end)
683 __set_page_dirty_nobuffers(locked_page);
684 /* unlocked later on in the async handlers */
685
686 if (redirty)
687 extent_range_redirty_for_io(inode, start, end);
688 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
689 BTRFS_COMPRESS_NONE);
690 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500691
Filipe Mananac44f6492014-10-09 21:15:44 +0100692 return;
Chris Mason771ed682008-11-06 22:02:51 -0500693
694free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100695 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500696 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300697 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500698 }
Chris Masond3977122009-01-05 21:25:51 -0500699 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500700}
Chris Mason771ed682008-11-06 22:02:51 -0500701
Filipe Manana40ae8372014-10-06 22:14:24 +0100702static void free_async_extent_pages(struct async_extent *async_extent)
703{
704 int i;
705
706 if (!async_extent->pages)
707 return;
708
709 for (i = 0; i < async_extent->nr_pages; i++) {
710 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300711 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100712 }
713 kfree(async_extent->pages);
714 async_extent->nr_pages = 0;
715 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500716}
717
718/*
719 * phase two of compressed writeback. This is the ordered portion
720 * of the code, which only gets called in the order the work was
721 * queued. We walk all the async extents created by compress_file_range
722 * and send them down to the disk.
723 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100724static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500725 struct async_cow *async_cow)
726{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400727 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500728 struct async_extent *async_extent;
729 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500730 struct btrfs_key ins;
731 struct extent_map *em;
732 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500733 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500734 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500735
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500736again:
Chris Masond3977122009-01-05 21:25:51 -0500737 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500738 async_extent = list_entry(async_cow->extents.next,
739 struct async_extent, list);
740 list_del(&async_extent->list);
741
742 io_tree = &BTRFS_I(inode)->io_tree;
743
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500744retry:
Chris Mason771ed682008-11-06 22:02:51 -0500745 /* did the compression code fall back to uncompressed IO? */
746 if (!async_extent->pages) {
747 int page_started = 0;
748 unsigned long nr_written = 0;
749
750 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000751 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100752 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500753
754 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500755 ret = cow_file_range(inode, async_cow->locked_page,
756 async_extent->start,
757 async_extent->start +
758 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800759 async_extent->start +
760 async_extent->ram_size - 1,
761 &page_started, &nr_written, 0,
762 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500763
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100764 /* JDM XXX */
765
Chris Mason771ed682008-11-06 22:02:51 -0500766 /*
767 * if page_started, cow_file_range inserted an
768 * inline extent and took care of all the unlocking
769 * and IO for us. Otherwise, we need to submit
770 * all those pages down to the drive.
771 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500772 if (!page_started && !ret)
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;
963 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000964 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400965 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500966 struct btrfs_key ins;
967 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000968 unsigned clear_bits;
969 unsigned long page_ops;
970 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500971 int ret = 0;
972
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200973 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400974 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500975 ret = -EINVAL;
976 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400977 }
Chris Mason771ed682008-11-06 22:02:51 -0500978
Qu Wenruofda28322013-02-26 08:10:22 +0000979 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500980 num_bytes = max(blocksize, num_bytes);
981 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500982
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200983 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400984
Chris Mason771ed682008-11-06 22:02:51 -0500985 if (start == 0) {
986 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800987 ret = cow_file_range_inline(root, inode, start, end, 0,
988 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500989 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400990 /*
991 * We use DO_ACCOUNTING here because we need the
992 * delalloc_release_metadata to be run _after_ we drop
993 * our outstanding extent for clearing delalloc for this
994 * range.
995 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800996 extent_clear_unlock_delalloc(inode, start, end,
997 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400998 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400999 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1000 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001001 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1002 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001003 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001004 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001005 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001006 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001007 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001008 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001009 }
1010 }
Chris Masonc8b97812008-10-29 14:49:59 -04001011
1012 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001013 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001014
Josef Bacik4b46fce2010-05-23 11:00:55 -04001015 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001016 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1017 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001018
Chris Masond3977122009-01-05 21:25:51 -05001019 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +00001020 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001021 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001022 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001023 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001024 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001025 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001026 cur_alloc_size = ins.offset;
1027 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001028
Chris Mason771ed682008-11-06 22:02:51 -05001029 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001030 em = create_io_em(inode, start, ins.offset, /* len */
1031 start, /* orig_start */
1032 ins.objectid, /* block_start */
1033 ins.offset, /* block_len */
1034 ins.offset, /* orig_block_len */
1035 ram_size, /* ram_bytes */
1036 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001037 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001038 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001039 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001040 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001041
Chris Masone6dcd2d2008-07-17 12:53:50 -04001042 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001043 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001044 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001045 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001046
Yan Zheng17d217f2008-12-12 10:03:38 -05001047 if (root->root_key.objectid ==
1048 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1049 ret = btrfs_reloc_clone_csums(inode, start,
1050 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001051 /*
1052 * Only drop cache here, and process as normal.
1053 *
1054 * We must not allow extent_clear_unlock_delalloc()
1055 * at out_unlock label to free meta of this ordered
1056 * extent, as its meta should be freed by
1057 * btrfs_finish_ordered_io().
1058 *
1059 * So we must continue until @start is increased to
1060 * skip current ordered extent.
1061 */
Josef Bacik00361582013-08-14 14:02:47 -04001062 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001063 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1064 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001065 }
1066
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001067 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001068
Chris Masonc8b97812008-10-29 14:49:59 -04001069 /* we're not doing compressed IO, don't unlock the first
1070 * page (which the caller expects to stay locked), don't
1071 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001072 *
1073 * Do set the Private2 bit so we know this page was properly
1074 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001075 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001076 page_ops = unlock ? PAGE_UNLOCK : 0;
1077 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001078
Josef Bacikc2790a22013-07-29 11:20:47 -04001079 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001080 start + ram_size - 1,
1081 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001082 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001083 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001084 if (disk_num_bytes < cur_alloc_size)
1085 disk_num_bytes = 0;
1086 else
1087 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001088 num_bytes -= cur_alloc_size;
1089 alloc_hint = ins.objectid + ins.offset;
1090 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001091 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001092
1093 /*
1094 * btrfs_reloc_clone_csums() error, since start is increased
1095 * extent_clear_unlock_delalloc() at out_unlock label won't
1096 * free metadata of current ordered extent, we're OK to exit.
1097 */
1098 if (ret)
1099 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001100 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001101out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001102 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001103
Filipe Mananad9f85962014-08-25 10:43:00 +01001104out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001105 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001106out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001107 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001108 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001109out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001110 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1111 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001112 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1113 PAGE_END_WRITEBACK;
1114 /*
1115 * If we reserved an extent for our delalloc range (or a subrange) and
1116 * failed to create the respective ordered extent, then it means that
1117 * when we reserved the extent we decremented the extent's size from
1118 * the data space_info's bytes_may_use counter and incremented the
1119 * space_info's bytes_reserved counter by the same amount. We must make
1120 * sure extent_clear_unlock_delalloc() does not try to decrement again
1121 * the data space_info's bytes_may_use counter, therefore we do not pass
1122 * it the flag EXTENT_CLEAR_DATA_RESV.
1123 */
1124 if (extent_reserved) {
1125 extent_clear_unlock_delalloc(inode, start,
1126 start + cur_alloc_size,
1127 start + cur_alloc_size,
1128 locked_page,
1129 clear_bits,
1130 page_ops);
1131 start += cur_alloc_size;
1132 if (start >= end)
1133 goto out;
1134 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001135 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1136 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001137 clear_bits | EXTENT_CLEAR_DATA_RESV,
1138 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001139 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001140}
Chris Masonc8b97812008-10-29 14:49:59 -04001141
Chris Mason771ed682008-11-06 22:02:51 -05001142/*
1143 * work queue call back to started compression on a file and pages
1144 */
1145static noinline void async_cow_start(struct btrfs_work *work)
1146{
1147 struct async_cow *async_cow;
1148 int num_added = 0;
1149 async_cow = container_of(work, struct async_cow, work);
1150
1151 compress_file_range(async_cow->inode, async_cow->locked_page,
1152 async_cow->start, async_cow->end, async_cow,
1153 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001154 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001155 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001156 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001157 }
Chris Mason771ed682008-11-06 22:02:51 -05001158}
1159
1160/*
1161 * work queue call back to submit previously compressed pages
1162 */
1163static noinline void async_cow_submit(struct btrfs_work *work)
1164{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001165 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001166 struct async_cow *async_cow;
1167 struct btrfs_root *root;
1168 unsigned long nr_pages;
1169
1170 async_cow = container_of(work, struct async_cow, work);
1171
1172 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001173 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001174 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1175 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001176
David Sterbaee863952015-02-16 19:41:40 +01001177 /*
1178 * atomic_sub_return implies a barrier for waitqueue_active
1179 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001180 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001181 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001182 waitqueue_active(&fs_info->async_submit_wait))
1183 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001184
Chris Masond3977122009-01-05 21:25:51 -05001185 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001186 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001187}
Chris Masonc8b97812008-10-29 14:49:59 -04001188
Chris Mason771ed682008-11-06 22:02:51 -05001189static noinline void async_cow_free(struct btrfs_work *work)
1190{
1191 struct async_cow *async_cow;
1192 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001193 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001194 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001195 kfree(async_cow);
1196}
1197
1198static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1199 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001200 unsigned long *nr_written,
1201 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001202{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001203 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001204 struct async_cow *async_cow;
1205 struct btrfs_root *root = BTRFS_I(inode)->root;
1206 unsigned long nr_pages;
1207 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001208
Chris Masona3429ab2009-10-08 12:30:20 -04001209 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001210 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001211 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001212 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001213 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001214 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001215 async_cow->root = root;
1216 async_cow->locked_page = locked_page;
1217 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001218 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001219
Wang Shilongf79707b2014-07-17 11:44:09 +08001220 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001221 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001222 cur_end = end;
1223 else
Byongho Leeee221842015-12-15 01:42:10 +09001224 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001225
1226 async_cow->end = cur_end;
1227 INIT_LIST_HEAD(&async_cow->extents);
1228
Liu Bo9e0af232014-08-15 23:36:53 +08001229 btrfs_init_work(&async_cow->work,
1230 btrfs_delalloc_helper,
1231 async_cow_start, async_cow_submit,
1232 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001233
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001234 nr_pages = (cur_end - start + PAGE_SIZE) >>
1235 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001236 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001237
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001238 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001239
Chris Mason771ed682008-11-06 22:02:51 -05001240 *nr_written += nr_pages;
1241 start = cur_end + 1;
1242 }
1243 *page_started = 1;
1244 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001245}
1246
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001247static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001248 u64 bytenr, u64 num_bytes)
1249{
1250 int ret;
1251 struct btrfs_ordered_sum *sums;
1252 LIST_HEAD(list);
1253
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001254 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001255 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001256 if (ret == 0 && list_empty(&list))
1257 return 0;
1258
1259 while (!list_empty(&list)) {
1260 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1261 list_del(&sums->list);
1262 kfree(sums);
1263 }
1264 return 1;
1265}
1266
Chris Masond352ac62008-09-29 15:18:18 -04001267/*
1268 * when nowcow writeback call back. This checks for snapshots or COW copies
1269 * of the extents that exist in the file, and COWs the file as required.
1270 *
1271 * If no cow copies or snapshots exist, we write directly to the existing
1272 * blocks on disk
1273 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001274static noinline int run_delalloc_nocow(struct inode *inode,
1275 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001276 u64 start, u64 end, int *page_started, int force,
1277 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001278{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001279 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001280 struct btrfs_root *root = BTRFS_I(inode)->root;
1281 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001282 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001284 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001285 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 u64 cow_start;
1287 u64 cur_offset;
1288 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001289 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 u64 disk_bytenr;
1291 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001292 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001293 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001295 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001296 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 int nocow;
1298 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001299 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001300 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001301
1302 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001303 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001304 extent_clear_unlock_delalloc(inode, start, end, end,
1305 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001306 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001307 EXTENT_DO_ACCOUNTING |
1308 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001309 PAGE_CLEAR_DIRTY |
1310 PAGE_SET_WRITEBACK |
1311 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001312 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001313 }
Li Zefan82d59022011-04-20 10:33:24 +08001314
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001315 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001316
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 cow_start = (u64)-1;
1318 cur_offset = start;
1319 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001320 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001322 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001323 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001324 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1325 leaf = path->nodes[0];
1326 btrfs_item_key_to_cpu(leaf, &found_key,
1327 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001328 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 found_key.type == BTRFS_EXTENT_DATA_KEY)
1330 path->slots[0]--;
1331 }
1332 check_prev = 0;
1333next_slot:
1334 leaf = path->nodes[0];
1335 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1336 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001337 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001338 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 if (ret > 0)
1340 break;
1341 leaf = path->nodes[0];
1342 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001343
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 nocow = 0;
1345 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001346 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001348
Filipe Manana1d512cb2015-11-09 00:33:58 +00001349 if (found_key.objectid > ino)
1350 break;
1351 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1352 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1353 path->slots[0]++;
1354 goto next_slot;
1355 }
1356 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 found_key.offset > end)
1358 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001359
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 if (found_key.offset > cur_offset) {
1361 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001362 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 goto out_check;
1364 }
Chris Masonc31f8832008-01-08 15:46:31 -05001365
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 fi = btrfs_item_ptr(leaf, path->slots[0],
1367 struct btrfs_file_extent_item);
1368 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001369
Josef Bacikcc95bef2013-04-04 14:31:27 -04001370 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001371 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1372 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001374 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001375 extent_end = found_key.offset +
1376 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001377 disk_num_bytes =
1378 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001379 if (extent_end <= start) {
1380 path->slots[0]++;
1381 goto next_slot;
1382 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001383 if (disk_bytenr == 0)
1384 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001385 if (btrfs_file_extent_compression(leaf, fi) ||
1386 btrfs_file_extent_encryption(leaf, fi) ||
1387 btrfs_file_extent_other_encoding(leaf, fi))
1388 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001389 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1390 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001391 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001392 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001393 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001394 found_key.offset -
1395 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001396 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001397 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001398 disk_bytenr += cur_offset - found_key.offset;
1399 num_bytes = min(end + 1, extent_end) - cur_offset;
1400 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001401 * if there are pending snapshots for this root,
1402 * we fall into common COW way.
1403 */
1404 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001405 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001406 if (!err)
1407 goto out_check;
1408 }
1409 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001410 * force cow if csum exists in the range.
1411 * this ensure that csum for a given extent are
1412 * either valid or do not exist.
1413 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001414 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001415 num_bytes)) {
1416 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001417 btrfs_end_write_no_snapshotting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001418 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001419 }
1420 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1421 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001422 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001423 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001424 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001425 nocow = 1;
1426 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1427 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001428 btrfs_file_extent_inline_len(leaf,
1429 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001430 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001431 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001432 } else {
1433 BUG_ON(1);
1434 }
1435out_check:
1436 if (extent_end <= start) {
1437 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001438 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001439 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001440 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001441 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001442 goto next_slot;
1443 }
1444 if (!nocow) {
1445 if (cow_start == (u64)-1)
1446 cow_start = cur_offset;
1447 cur_offset = extent_end;
1448 if (cur_offset > end)
1449 break;
1450 path->slots[0]++;
1451 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001452 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001453
David Sterbab3b4aa72011-04-21 01:20:15 +02001454 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001455 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001456 ret = cow_file_range(inode, locked_page,
1457 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001458 end, page_started, nr_written, 1,
1459 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001460 if (ret) {
1461 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001462 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001463 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001464 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001465 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001466 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001467 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001468 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001469 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001470
Yan Zhengd899e052008-10-30 14:25:28 -04001471 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001472 u64 orig_start = found_key.offset - extent_offset;
1473
1474 em = create_io_em(inode, cur_offset, num_bytes,
1475 orig_start,
1476 disk_bytenr, /* block_start */
1477 num_bytes, /* block_len */
1478 disk_num_bytes, /* orig_block_len */
1479 ram_bytes, BTRFS_COMPRESS_NONE,
1480 BTRFS_ORDERED_PREALLOC);
1481 if (IS_ERR(em)) {
1482 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001483 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001484 if (nocow)
1485 btrfs_dec_nocow_writers(fs_info,
1486 disk_bytenr);
1487 ret = PTR_ERR(em);
1488 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001489 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001490 free_extent_map(em);
1491 }
1492
1493 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001494 type = BTRFS_ORDERED_PREALLOC;
1495 } else {
1496 type = BTRFS_ORDERED_NOCOW;
1497 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001498
1499 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001500 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001501 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001502 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001503 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001504
Yan, Zhengefa56462010-05-16 10:49:59 -04001505 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001506 BTRFS_DATA_RELOC_TREE_OBJECTID)
1507 /*
1508 * Error handled later, as we must prevent
1509 * extent_clear_unlock_delalloc() in error handler
1510 * from freeing metadata of created ordered extent.
1511 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001512 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1513 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001514
Josef Bacikc2790a22013-07-29 11:20:47 -04001515 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001516 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001517 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001518 EXTENT_DELALLOC |
1519 EXTENT_CLEAR_DATA_RESV,
1520 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1521
Wang Shilonge9894fd2014-03-27 11:12:25 +08001522 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001523 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001524 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001525
1526 /*
1527 * btrfs_reloc_clone_csums() error, now we're OK to call error
1528 * handler, as metadata for created ordered extent will only
1529 * be freed by btrfs_finish_ordered_io().
1530 */
1531 if (ret)
1532 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001533 if (cur_offset > end)
1534 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001535 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001536 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001537
Josef Bacik17ca04a2012-05-31 15:58:55 -04001538 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001539 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001540 cur_offset = end;
1541 }
1542
Yan Zheng80ff3852008-10-30 14:20:02 -04001543 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001544 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1545 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001546 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001547 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001548 }
1549
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001550error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001551 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001552 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001553 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001554 EXTENT_DELALLOC | EXTENT_DEFRAG |
1555 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1556 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001557 PAGE_SET_WRITEBACK |
1558 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001559 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001560 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001561}
1562
Wang Shilong47059d92014-07-03 18:22:07 +08001563static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1564{
1565
1566 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1567 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1568 return 0;
1569
1570 /*
1571 * @defrag_bytes is a hint value, no spinlock held here,
1572 * if is not zero, it means the file is defragging.
1573 * Force cow if given extent needs to be defragged.
1574 */
1575 if (BTRFS_I(inode)->defrag_bytes &&
1576 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1577 EXTENT_DEFRAG, 0, NULL))
1578 return 1;
1579
1580 return 0;
1581}
1582
Chris Masond352ac62008-09-29 15:18:18 -04001583/*
1584 * extent_io.c call back to do delayed allocation processing
1585 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001586static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001587 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001588 unsigned long *nr_written,
1589 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001590{
Josef Bacikc6100a42017-05-05 11:57:13 -04001591 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001592 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001593 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001594 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001595
Wang Shilong47059d92014-07-03 18:22:07 +08001596 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001597 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001598 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001599 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001600 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001601 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001602 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001603 ret = cow_file_range(inode, locked_page, start, end, end,
1604 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001605 } else {
1606 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1607 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001608 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001609 page_started, nr_written,
1610 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001611 }
Qu Wenruo524272602017-03-08 10:25:52 +08001612 if (ret)
1613 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001614 return ret;
1615}
1616
Josef Bacikc6100a42017-05-05 11:57:13 -04001617static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001618 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001619{
Josef Bacikc6100a42017-05-05 11:57:13 -04001620 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 u64 size;
1622
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001623 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001624 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001625 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001626
Josef Bacikdcab6a32015-02-11 15:08:59 -05001627 size = orig->end - orig->start + 1;
1628 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001629 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001630 u64 new_size;
1631
1632 /*
Josef Bacikba117212015-03-13 15:01:24 -04001633 * See the explanation in btrfs_merge_extent_hook, the same
1634 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001635 */
1636 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001637 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001638 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001639 num_extents += count_max_extents(new_size);
1640 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001641 return;
1642 }
1643
Josef Bacik9e0baf62011-07-15 15:16:44 +00001644 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001645 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001646 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001647}
1648
1649/*
1650 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1651 * extents so we can keep track of new extents that are just merged onto old
1652 * extents, such as when we are doing sequential writes, so we can properly
1653 * account for the metadata space we'll need.
1654 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001655static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001656 struct extent_state *new,
1657 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001658{
Josef Bacikc6100a42017-05-05 11:57:13 -04001659 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001660 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001661 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001662
Josef Bacik9ed74f22009-09-11 16:12:44 -04001663 /* not delalloc, ignore it */
1664 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001665 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001666
Josef Bacik8461a3d2015-03-13 15:12:08 -04001667 if (new->start > other->start)
1668 new_size = new->end - other->start + 1;
1669 else
1670 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001671
1672 /* we're not bigger than the max, unreserve the space and go */
1673 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1674 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001675 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001676 spin_unlock(&BTRFS_I(inode)->lock);
1677 return;
1678 }
1679
1680 /*
Josef Bacikba117212015-03-13 15:01:24 -04001681 * We have to add up either side to figure out how many extents were
1682 * accounted for before we merged into one big extent. If the number of
1683 * extents we accounted for is <= the amount we need for the new range
1684 * then we can return, otherwise drop. Think of it like this
1685 *
1686 * [ 4k][MAX_SIZE]
1687 *
1688 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1689 * need 2 outstanding extents, on one side we have 1 and the other side
1690 * we have 1 so they are == and we can return. But in this case
1691 *
1692 * [MAX_SIZE+4k][MAX_SIZE+4k]
1693 *
1694 * Each range on their own accounts for 2 extents, but merged together
1695 * they are only 3 extents worth of accounting, so we need to drop in
1696 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001697 */
Josef Bacikba117212015-03-13 15:01:24 -04001698 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001699 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001700 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001701 num_extents += count_max_extents(old_size);
1702 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001703 return;
1704
Josef Bacik9e0baf62011-07-15 15:16:44 +00001705 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001706 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001707 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001708}
1709
Miao Xieeb73c1b2013-05-15 07:48:22 +00001710static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1711 struct inode *inode)
1712{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001713 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1714
Miao Xieeb73c1b2013-05-15 07:48:22 +00001715 spin_lock(&root->delalloc_lock);
1716 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1717 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1718 &root->delalloc_inodes);
1719 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1720 &BTRFS_I(inode)->runtime_flags);
1721 root->nr_delalloc_inodes++;
1722 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001723 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724 BUG_ON(!list_empty(&root->delalloc_root));
1725 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001726 &fs_info->delalloc_roots);
1727 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001728 }
1729 }
1730 spin_unlock(&root->delalloc_lock);
1731}
1732
1733static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001734 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001735{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001736 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001737
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001739 if (!list_empty(&inode->delalloc_inodes)) {
1740 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001741 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001742 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001743 root->nr_delalloc_inodes--;
1744 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001745 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001746 BUG_ON(list_empty(&root->delalloc_root));
1747 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001748 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001749 }
1750 }
1751 spin_unlock(&root->delalloc_lock);
1752}
1753
Chris Masond352ac62008-09-29 15:18:18 -04001754/*
1755 * extent_io.c set_bit_hook, used to track delayed allocation
1756 * bytes in this file, and to maintain the list of inodes that
1757 * have pending delalloc work to be done.
1758 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001759static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001760 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001761{
Josef Bacikc6100a42017-05-05 11:57:13 -04001762 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001763
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001764 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1765
Wang Shilong47059d92014-07-03 18:22:07 +08001766 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1767 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001768 /*
1769 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001770 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001771 * bit, which is only set or cleared with irqs on
1772 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001773 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001774 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001775 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001776 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001777 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001778
Josef Bacik8b62f872017-10-19 14:15:55 -04001779 spin_lock(&BTRFS_I(inode)->lock);
1780 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1781 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001782
Josef Bacik6a3891c2015-03-16 17:38:52 -04001783 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001784 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001785 return;
1786
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001787 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1788 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001789 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001790 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001791 if (*bits & EXTENT_DEFRAG)
1792 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001793 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001794 &BTRFS_I(inode)->runtime_flags))
1795 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001796 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001797 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001798
1799 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1800 (*bits & EXTENT_DELALLOC_NEW)) {
1801 spin_lock(&BTRFS_I(inode)->lock);
1802 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1803 state->start;
1804 spin_unlock(&BTRFS_I(inode)->lock);
1805 }
Chris Mason291d6732008-01-29 15:55:23 -05001806}
1807
Chris Masond352ac62008-09-29 15:18:18 -04001808/*
1809 * extent_io.c clear_bit_hook, see set_bit_hook for why
1810 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001811static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001812 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001813 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001814{
Josef Bacikc6100a42017-05-05 11:57:13 -04001815 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001816 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001817 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001818 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001819
Filipe Manana4a4b9642017-07-27 19:52:55 +01001820 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1821 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001822 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001823 spin_unlock(&inode->lock);
1824 }
Wang Shilong47059d92014-07-03 18:22:07 +08001825
Chris Mason75eff682008-12-15 15:54:40 -05001826 /*
1827 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001828 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001829 * bit, which is only set or cleared with irqs on
1830 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001831 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001832 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001833 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001834
Josef Bacik8b62f872017-10-19 14:15:55 -04001835 spin_lock(&inode->lock);
1836 btrfs_mod_outstanding_extents(inode, -num_extents);
1837 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001838
Josef Bacikb6d08f02013-09-27 14:57:43 -04001839 /*
1840 * We don't reserve metadata space for space cache inodes so we
1841 * don't need to call dellalloc_release_metadata if there is an
1842 * error.
1843 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001844 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001845 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001846 btrfs_delalloc_release_metadata(inode, len);
1847
Josef Bacik6a3891c2015-03-16 17:38:52 -04001848 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001849 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001850 return;
1851
Filipe Mananaa315e682017-03-06 23:04:20 +00001852 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1853 do_list && !(state->state & EXTENT_NORESERVE) &&
1854 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001855 btrfs_free_reserved_data_space_noquota(
1856 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001857 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001858
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001859 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1860 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001861 spin_lock(&inode->lock);
1862 inode->delalloc_bytes -= len;
1863 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001864 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001865 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001866 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001867 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001868 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001869
1870 if ((state->state & EXTENT_DELALLOC_NEW) &&
1871 (*bits & EXTENT_DELALLOC_NEW)) {
1872 spin_lock(&inode->lock);
1873 ASSERT(inode->new_delalloc_bytes >= len);
1874 inode->new_delalloc_bytes -= len;
1875 spin_unlock(&inode->lock);
1876 }
Chris Mason291d6732008-01-29 15:55:23 -05001877}
1878
Chris Masond352ac62008-09-29 15:18:18 -04001879/*
1880 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1881 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001882 *
1883 * return 1 if page cannot be merged to bio
1884 * return 0 if page can be merged to bio
1885 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001886 */
Mike Christie81a75f672016-06-05 14:31:54 -05001887int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001888 size_t size, struct bio *bio,
1889 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001890{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001891 struct inode *inode = page->mapping->host;
1892 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001893 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001894 u64 length = 0;
1895 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001896 int ret;
1897
Chris Mason771ed682008-11-06 22:02:51 -05001898 if (bio_flags & EXTENT_BIO_COMPRESSED)
1899 return 0;
1900
Kent Overstreet4f024f32013-10-11 15:44:27 -07001901 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001902 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001903 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1904 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001905 if (ret < 0)
1906 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001907 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001908 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001909 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001910}
1911
Chris Masond352ac62008-09-29 15:18:18 -04001912/*
1913 * in order to insert checksums into the metadata in large chunks,
1914 * we wait until bio submission time. All the pages in the bio are
1915 * checksummed and sums are attached onto the ordered extent record.
1916 *
1917 * At IO completion time the cums attached on the ordered extent record
1918 * are inserted into the btree
1919 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001920static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001921 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001922 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001923{
Josef Bacikc6100a42017-05-05 11:57:13 -04001924 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001925 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001926
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001927 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001928 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001929 return 0;
1930}
Chris Masone0156402008-04-16 11:15:20 -04001931
Chris Mason4a69a412008-11-06 22:03:00 -05001932/*
1933 * in order to insert checksums into the metadata in large chunks,
1934 * we wait until bio submission time. All the pages in the bio are
1935 * checksummed and sums are attached onto the ordered extent record.
1936 *
1937 * At IO completion time the cums attached on the ordered extent record
1938 * are inserted into the btree
1939 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001940static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001941 int mirror_num, unsigned long bio_flags,
1942 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001943{
Josef Bacikc6100a42017-05-05 11:57:13 -04001944 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001945 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001946 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001947
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001948 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001949 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001950 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001951 bio_endio(bio);
1952 }
Stefan Behrens61891922012-11-05 18:51:52 +01001953 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001954}
1955
Chris Masond352ac62008-09-29 15:18:18 -04001956/*
Chris Masoncad321a2008-12-17 14:51:42 -05001957 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001958 * on write, or reading the csums from the tree before a read.
1959 *
1960 * Rules about async/sync submit,
1961 * a) read: sync submit
1962 *
1963 * b) write without checksum: sync submit
1964 *
1965 * c) write with checksum:
1966 * c-1) if bio is issued by fsync: sync submit
1967 * (sync_writers != 0)
1968 *
1969 * c-2) if root is reloc root: sync submit
1970 * (only in case of buffered IO)
1971 *
1972 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001973 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001974static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001975 int mirror_num, unsigned long bio_flags,
1976 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001977{
Josef Bacikc6100a42017-05-05 11:57:13 -04001978 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001979 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001980 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301981 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001982 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001983 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001984 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001985
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001986 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001987
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001988 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301989 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001990
Mike Christie37226b22016-06-05 14:31:52 -05001991 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001992 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001993 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001994 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001995
Chris Masond20f7042008-12-08 16:58:54 -05001996 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001997 ret = btrfs_submit_compressed_read(inode, bio,
1998 mirror_num,
1999 bio_flags);
2000 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002001 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002002 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002003 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002004 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002005 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002006 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002007 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002008 /* csum items have already been cloned */
2009 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2010 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002011 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002012 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2013 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002014 __btrfs_submit_bio_start,
2015 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002016 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002017 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002018 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002019 if (ret)
2020 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002021 }
2022
Chris Mason0b86a832008-03-24 15:01:56 -04002023mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002024 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002025
2026out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002027 if (ret) {
2028 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002029 bio_endio(bio);
2030 }
Stefan Behrens61891922012-11-05 18:51:52 +01002031 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002032}
Chris Mason6885f302008-02-20 16:11:05 -05002033
Chris Masond352ac62008-09-29 15:18:18 -04002034/*
2035 * given a list of ordered sums record them in the inode. This happens
2036 * at IO completion time based on sums calculated at bio submission time.
2037 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002038static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002039 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002040{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002041 struct btrfs_ordered_sum *sum;
2042
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002043 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002044 trans->adding_csums = true;
Chris Masond20f7042008-12-08 16:58:54 -05002045 btrfs_csum_file_blocks(trans,
2046 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002047 trans->adding_csums = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002048 }
2049 return 0;
2050}
2051
Josef Bacik2ac55d42010-02-03 19:33:23 +00002052int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002053 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002054 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002055{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002056 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002057 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002058 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002059}
2060
Chris Masond352ac62008-09-29 15:18:18 -04002061/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002062struct btrfs_writepage_fixup {
2063 struct page *page;
2064 struct btrfs_work work;
2065};
2066
Christoph Hellwigb2950862008-12-02 09:54:17 -05002067static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002068{
2069 struct btrfs_writepage_fixup *fixup;
2070 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002071 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002072 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002073 struct page *page;
2074 struct inode *inode;
2075 u64 page_start;
2076 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002077 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002078
2079 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2080 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002081again:
Chris Mason247e7432008-07-17 12:53:51 -04002082 lock_page(page);
2083 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2084 ClearPageChecked(page);
2085 goto out_page;
2086 }
2087
2088 inode = page->mapping->host;
2089 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002090 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002091
David Sterbaff13db42015-12-03 14:30:40 +01002092 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002093 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002094
2095 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002096 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002097 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002098
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002099 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002100 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002101 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002102 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002103 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002104 unlock_page(page);
2105 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002106 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002107 goto again;
2108 }
Chris Mason247e7432008-07-17 12:53:51 -04002109
Qu Wenruo364ecf32017-02-27 15:10:38 +08002110 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002111 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002112 if (ret) {
2113 mapping_set_error(page->mapping, ret);
2114 end_extent_writepage(page, ret, page_start, page_end);
2115 ClearPageChecked(page);
2116 goto out;
2117 }
2118
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002119 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2120 &cached_state, 0);
2121 if (ret) {
2122 mapping_set_error(page->mapping, ret);
2123 end_extent_writepage(page, ret, page_start, page_end);
2124 ClearPageChecked(page);
2125 goto out;
2126 }
2127
Chris Mason247e7432008-07-17 12:53:51 -04002128 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002129 set_page_dirty(page);
Josef Bacik8b62f872017-10-19 14:15:55 -04002130 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002131out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002132 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002133 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002134out_page:
2135 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002136 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002137 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002138 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002139}
2140
2141/*
2142 * There are a few paths in the higher layers of the kernel that directly
2143 * set the page dirty bit without asking the filesystem if it is a
2144 * good idea. This causes problems because we want to make sure COW
2145 * properly happens and the data=ordered rules are followed.
2146 *
Chris Masonc8b97812008-10-29 14:49:59 -04002147 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002148 * hasn't been properly setup for IO. We kick off an async process
2149 * to fix it up. The async helper will wait for ordered extents, set
2150 * the delalloc bit and make it safe to write the page.
2151 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002152static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002153{
2154 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002155 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002156 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002157
Chris Mason8b62b722009-09-02 16:53:46 -04002158 /* this page is properly in the ordered list */
2159 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002160 return 0;
2161
2162 if (PageChecked(page))
2163 return -EAGAIN;
2164
2165 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2166 if (!fixup)
2167 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002168
Chris Mason247e7432008-07-17 12:53:51 -04002169 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002170 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002171 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2172 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002173 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002174 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002175 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002176}
2177
Yan Zhengd899e052008-10-30 14:25:28 -04002178static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2179 struct inode *inode, u64 file_pos,
2180 u64 disk_bytenr, u64 disk_num_bytes,
2181 u64 num_bytes, u64 ram_bytes,
2182 u8 compression, u8 encryption,
2183 u16 other_encoding, int extent_type)
2184{
2185 struct btrfs_root *root = BTRFS_I(inode)->root;
2186 struct btrfs_file_extent_item *fi;
2187 struct btrfs_path *path;
2188 struct extent_buffer *leaf;
2189 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002190 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002191 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002192 int ret;
2193
2194 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002195 if (!path)
2196 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002197
Chris Masona1ed8352009-09-11 12:27:37 -04002198 /*
2199 * we may be replacing one extent in the tree with another.
2200 * The new extent is pinned in the extent map, and we don't want
2201 * to drop it from the cache until it is completely in the btree.
2202 *
2203 * So, tell btrfs_drop_extents to leave this extent in the cache.
2204 * the caller is expected to unpin it and allow it to be merged
2205 * with the others.
2206 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002207 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2208 file_pos + num_bytes, NULL, 0,
2209 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002210 if (ret)
2211 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002212
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002213 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002214 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002215 ins.offset = file_pos;
2216 ins.type = BTRFS_EXTENT_DATA_KEY;
2217
2218 path->leave_spinning = 1;
2219 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2220 sizeof(*fi));
2221 if (ret)
2222 goto out;
2223 }
Yan Zhengd899e052008-10-30 14:25:28 -04002224 leaf = path->nodes[0];
2225 fi = btrfs_item_ptr(leaf, path->slots[0],
2226 struct btrfs_file_extent_item);
2227 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2228 btrfs_set_file_extent_type(leaf, fi, extent_type);
2229 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2230 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2231 btrfs_set_file_extent_offset(leaf, fi, 0);
2232 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2233 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2234 btrfs_set_file_extent_compression(leaf, fi, compression);
2235 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2236 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002237
Yan Zhengd899e052008-10-30 14:25:28 -04002238 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002239 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002240
2241 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002242
2243 ins.objectid = disk_bytenr;
2244 ins.offset = disk_num_bytes;
2245 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002246
Qu Wenruo297d7502015-09-08 17:08:37 +08002247 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002248 * Release the reserved range from inode dirty range map, as it is
2249 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002250 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002251 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2252 if (ret < 0)
2253 goto out;
2254 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002255 ret = btrfs_alloc_reserved_file_extent(trans, root,
2256 btrfs_ino(BTRFS_I(inode)),
2257 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002258out:
Yan Zhengd899e052008-10-30 14:25:28 -04002259 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002260
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002261 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002262}
2263
Liu Bo38c227d2013-01-29 03:18:40 +00002264/* snapshot-aware defrag */
2265struct sa_defrag_extent_backref {
2266 struct rb_node node;
2267 struct old_sa_defrag_extent *old;
2268 u64 root_id;
2269 u64 inum;
2270 u64 file_pos;
2271 u64 extent_offset;
2272 u64 num_bytes;
2273 u64 generation;
2274};
2275
2276struct old_sa_defrag_extent {
2277 struct list_head list;
2278 struct new_sa_defrag_extent *new;
2279
2280 u64 extent_offset;
2281 u64 bytenr;
2282 u64 offset;
2283 u64 len;
2284 int count;
2285};
2286
2287struct new_sa_defrag_extent {
2288 struct rb_root root;
2289 struct list_head head;
2290 struct btrfs_path *path;
2291 struct inode *inode;
2292 u64 file_pos;
2293 u64 len;
2294 u64 bytenr;
2295 u64 disk_len;
2296 u8 compress_type;
2297};
2298
2299static int backref_comp(struct sa_defrag_extent_backref *b1,
2300 struct sa_defrag_extent_backref *b2)
2301{
2302 if (b1->root_id < b2->root_id)
2303 return -1;
2304 else if (b1->root_id > b2->root_id)
2305 return 1;
2306
2307 if (b1->inum < b2->inum)
2308 return -1;
2309 else if (b1->inum > b2->inum)
2310 return 1;
2311
2312 if (b1->file_pos < b2->file_pos)
2313 return -1;
2314 else if (b1->file_pos > b2->file_pos)
2315 return 1;
2316
2317 /*
2318 * [------------------------------] ===> (a range of space)
2319 * |<--->| |<---->| =============> (fs/file tree A)
2320 * |<---------------------------->| ===> (fs/file tree B)
2321 *
2322 * A range of space can refer to two file extents in one tree while
2323 * refer to only one file extent in another tree.
2324 *
2325 * So we may process a disk offset more than one time(two extents in A)
2326 * and locate at the same extent(one extent in B), then insert two same
2327 * backrefs(both refer to the extent in B).
2328 */
2329 return 0;
2330}
2331
2332static void backref_insert(struct rb_root *root,
2333 struct sa_defrag_extent_backref *backref)
2334{
2335 struct rb_node **p = &root->rb_node;
2336 struct rb_node *parent = NULL;
2337 struct sa_defrag_extent_backref *entry;
2338 int ret;
2339
2340 while (*p) {
2341 parent = *p;
2342 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2343
2344 ret = backref_comp(backref, entry);
2345 if (ret < 0)
2346 p = &(*p)->rb_left;
2347 else
2348 p = &(*p)->rb_right;
2349 }
2350
2351 rb_link_node(&backref->node, parent, p);
2352 rb_insert_color(&backref->node, root);
2353}
2354
2355/*
2356 * Note the backref might has changed, and in this case we just return 0.
2357 */
2358static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2359 void *ctx)
2360{
2361 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002362 struct old_sa_defrag_extent *old = ctx;
2363 struct new_sa_defrag_extent *new = old->new;
2364 struct btrfs_path *path = new->path;
2365 struct btrfs_key key;
2366 struct btrfs_root *root;
2367 struct sa_defrag_extent_backref *backref;
2368 struct extent_buffer *leaf;
2369 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002370 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002371 int slot;
2372 int ret;
2373 u64 extent_offset;
2374 u64 num_bytes;
2375
2376 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002377 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002378 return 0;
2379
2380 key.objectid = root_id;
2381 key.type = BTRFS_ROOT_ITEM_KEY;
2382 key.offset = (u64)-1;
2383
Liu Bo38c227d2013-01-29 03:18:40 +00002384 root = btrfs_read_fs_root_no_name(fs_info, &key);
2385 if (IS_ERR(root)) {
2386 if (PTR_ERR(root) == -ENOENT)
2387 return 0;
2388 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002389 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002390 inum, offset, root_id);
2391 return PTR_ERR(root);
2392 }
2393
2394 key.objectid = inum;
2395 key.type = BTRFS_EXTENT_DATA_KEY;
2396 if (offset > (u64)-1 << 32)
2397 key.offset = 0;
2398 else
2399 key.offset = offset;
2400
2401 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302402 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002403 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002404 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002405
2406 while (1) {
2407 cond_resched();
2408
2409 leaf = path->nodes[0];
2410 slot = path->slots[0];
2411
2412 if (slot >= btrfs_header_nritems(leaf)) {
2413 ret = btrfs_next_leaf(root, path);
2414 if (ret < 0) {
2415 goto out;
2416 } else if (ret > 0) {
2417 ret = 0;
2418 goto out;
2419 }
2420 continue;
2421 }
2422
2423 path->slots[0]++;
2424
2425 btrfs_item_key_to_cpu(leaf, &key, slot);
2426
2427 if (key.objectid > inum)
2428 goto out;
2429
2430 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2431 continue;
2432
2433 extent = btrfs_item_ptr(leaf, slot,
2434 struct btrfs_file_extent_item);
2435
2436 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2437 continue;
2438
Liu Boe68afa42013-07-01 22:13:26 +08002439 /*
2440 * 'offset' refers to the exact key.offset,
2441 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2442 * (key.offset - extent_offset).
2443 */
2444 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002445 continue;
2446
Liu Boe68afa42013-07-01 22:13:26 +08002447 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002448 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002449
Liu Bo38c227d2013-01-29 03:18:40 +00002450 if (extent_offset >= old->extent_offset + old->offset +
2451 old->len || extent_offset + num_bytes <=
2452 old->extent_offset + old->offset)
2453 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002454 break;
2455 }
2456
2457 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2458 if (!backref) {
2459 ret = -ENOENT;
2460 goto out;
2461 }
2462
2463 backref->root_id = root_id;
2464 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002465 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002466 backref->num_bytes = num_bytes;
2467 backref->extent_offset = extent_offset;
2468 backref->generation = btrfs_file_extent_generation(leaf, extent);
2469 backref->old = old;
2470 backref_insert(&new->root, backref);
2471 old->count++;
2472out:
2473 btrfs_release_path(path);
2474 WARN_ON(ret);
2475 return ret;
2476}
2477
2478static noinline bool record_extent_backrefs(struct btrfs_path *path,
2479 struct new_sa_defrag_extent *new)
2480{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002481 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002482 struct old_sa_defrag_extent *old, *tmp;
2483 int ret;
2484
2485 new->path = path;
2486
2487 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002488 ret = iterate_inodes_from_logical(old->bytenr +
2489 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002490 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002491 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002492 if (ret < 0 && ret != -ENOENT)
2493 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002494
2495 /* no backref to be processed for this extent */
2496 if (!old->count) {
2497 list_del(&old->list);
2498 kfree(old);
2499 }
2500 }
2501
2502 if (list_empty(&new->head))
2503 return false;
2504
2505 return true;
2506}
2507
2508static int relink_is_mergable(struct extent_buffer *leaf,
2509 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002510 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002511{
Liu Bo116e0022013-08-02 16:30:40 +08002512 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002513 return 0;
2514
2515 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2516 return 0;
2517
Liu Bo116e0022013-08-02 16:30:40 +08002518 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2519 return 0;
2520
2521 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002522 btrfs_file_extent_other_encoding(leaf, fi))
2523 return 0;
2524
2525 return 1;
2526}
2527
2528/*
2529 * Note the backref might has changed, and in this case we just return 0.
2530 */
2531static noinline int relink_extent_backref(struct btrfs_path *path,
2532 struct sa_defrag_extent_backref *prev,
2533 struct sa_defrag_extent_backref *backref)
2534{
2535 struct btrfs_file_extent_item *extent;
2536 struct btrfs_file_extent_item *item;
2537 struct btrfs_ordered_extent *ordered;
2538 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002539 struct btrfs_root *root;
2540 struct btrfs_key key;
2541 struct extent_buffer *leaf;
2542 struct old_sa_defrag_extent *old = backref->old;
2543 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002544 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002545 struct inode *inode;
2546 struct extent_state *cached = NULL;
2547 int ret = 0;
2548 u64 start;
2549 u64 len;
2550 u64 lock_start;
2551 u64 lock_end;
2552 bool merge = false;
2553 int index;
2554
2555 if (prev && prev->root_id == backref->root_id &&
2556 prev->inum == backref->inum &&
2557 prev->file_pos + prev->num_bytes == backref->file_pos)
2558 merge = true;
2559
2560 /* step 1: get root */
2561 key.objectid = backref->root_id;
2562 key.type = BTRFS_ROOT_ITEM_KEY;
2563 key.offset = (u64)-1;
2564
Liu Bo38c227d2013-01-29 03:18:40 +00002565 index = srcu_read_lock(&fs_info->subvol_srcu);
2566
2567 root = btrfs_read_fs_root_no_name(fs_info, &key);
2568 if (IS_ERR(root)) {
2569 srcu_read_unlock(&fs_info->subvol_srcu, index);
2570 if (PTR_ERR(root) == -ENOENT)
2571 return 0;
2572 return PTR_ERR(root);
2573 }
Liu Bo38c227d2013-01-29 03:18:40 +00002574
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002575 if (btrfs_root_readonly(root)) {
2576 srcu_read_unlock(&fs_info->subvol_srcu, index);
2577 return 0;
2578 }
2579
Liu Bo38c227d2013-01-29 03:18:40 +00002580 /* step 2: get inode */
2581 key.objectid = backref->inum;
2582 key.type = BTRFS_INODE_ITEM_KEY;
2583 key.offset = 0;
2584
2585 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2586 if (IS_ERR(inode)) {
2587 srcu_read_unlock(&fs_info->subvol_srcu, index);
2588 return 0;
2589 }
2590
2591 srcu_read_unlock(&fs_info->subvol_srcu, index);
2592
2593 /* step 3: relink backref */
2594 lock_start = backref->file_pos;
2595 lock_end = backref->file_pos + backref->num_bytes - 1;
2596 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002597 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002598
2599 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2600 if (ordered) {
2601 btrfs_put_ordered_extent(ordered);
2602 goto out_unlock;
2603 }
2604
2605 trans = btrfs_join_transaction(root);
2606 if (IS_ERR(trans)) {
2607 ret = PTR_ERR(trans);
2608 goto out_unlock;
2609 }
2610
2611 key.objectid = backref->inum;
2612 key.type = BTRFS_EXTENT_DATA_KEY;
2613 key.offset = backref->file_pos;
2614
2615 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2616 if (ret < 0) {
2617 goto out_free_path;
2618 } else if (ret > 0) {
2619 ret = 0;
2620 goto out_free_path;
2621 }
2622
2623 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2624 struct btrfs_file_extent_item);
2625
2626 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2627 backref->generation)
2628 goto out_free_path;
2629
2630 btrfs_release_path(path);
2631
2632 start = backref->file_pos;
2633 if (backref->extent_offset < old->extent_offset + old->offset)
2634 start += old->extent_offset + old->offset -
2635 backref->extent_offset;
2636
2637 len = min(backref->extent_offset + backref->num_bytes,
2638 old->extent_offset + old->offset + old->len);
2639 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2640
2641 ret = btrfs_drop_extents(trans, root, inode, start,
2642 start + len, 1);
2643 if (ret)
2644 goto out_free_path;
2645again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002646 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002647 key.type = BTRFS_EXTENT_DATA_KEY;
2648 key.offset = start;
2649
Liu Boa09a0a72013-03-11 09:20:58 +00002650 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002651 if (merge) {
2652 struct btrfs_file_extent_item *fi;
2653 u64 extent_len;
2654 struct btrfs_key found_key;
2655
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002656 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002657 if (ret < 0)
2658 goto out_free_path;
2659
2660 path->slots[0]--;
2661 leaf = path->nodes[0];
2662 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2663
2664 fi = btrfs_item_ptr(leaf, path->slots[0],
2665 struct btrfs_file_extent_item);
2666 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2667
Liu Bo116e0022013-08-02 16:30:40 +08002668 if (extent_len + found_key.offset == start &&
2669 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002670 btrfs_set_file_extent_num_bytes(leaf, fi,
2671 extent_len + len);
2672 btrfs_mark_buffer_dirty(leaf);
2673 inode_add_bytes(inode, len);
2674
2675 ret = 1;
2676 goto out_free_path;
2677 } else {
2678 merge = false;
2679 btrfs_release_path(path);
2680 goto again;
2681 }
2682 }
2683
2684 ret = btrfs_insert_empty_item(trans, root, path, &key,
2685 sizeof(*extent));
2686 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002687 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002688 goto out_free_path;
2689 }
2690
2691 leaf = path->nodes[0];
2692 item = btrfs_item_ptr(leaf, path->slots[0],
2693 struct btrfs_file_extent_item);
2694 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2695 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2696 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2697 btrfs_set_file_extent_num_bytes(leaf, item, len);
2698 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2699 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2700 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2701 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2702 btrfs_set_file_extent_encryption(leaf, item, 0);
2703 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2704
2705 btrfs_mark_buffer_dirty(leaf);
2706 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002707 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002708
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002709 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002710 new->disk_len, 0,
2711 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002712 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002713 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002714 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002715 goto out_free_path;
2716 }
2717
2718 ret = 1;
2719out_free_path:
2720 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002721 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002722 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002723out_unlock:
2724 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002725 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002726 iput(inode);
2727 return ret;
2728}
2729
Liu Bo6f519562013-10-29 10:45:05 +08002730static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2731{
2732 struct old_sa_defrag_extent *old, *tmp;
2733
2734 if (!new)
2735 return;
2736
2737 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002738 kfree(old);
2739 }
2740 kfree(new);
2741}
2742
Liu Bo38c227d2013-01-29 03:18:40 +00002743static void relink_file_extents(struct new_sa_defrag_extent *new)
2744{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002745 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002746 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002747 struct sa_defrag_extent_backref *backref;
2748 struct sa_defrag_extent_backref *prev = NULL;
2749 struct inode *inode;
2750 struct btrfs_root *root;
2751 struct rb_node *node;
2752 int ret;
2753
2754 inode = new->inode;
2755 root = BTRFS_I(inode)->root;
2756
2757 path = btrfs_alloc_path();
2758 if (!path)
2759 return;
2760
2761 if (!record_extent_backrefs(path, new)) {
2762 btrfs_free_path(path);
2763 goto out;
2764 }
2765 btrfs_release_path(path);
2766
2767 while (1) {
2768 node = rb_first(&new->root);
2769 if (!node)
2770 break;
2771 rb_erase(node, &new->root);
2772
2773 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2774
2775 ret = relink_extent_backref(path, prev, backref);
2776 WARN_ON(ret < 0);
2777
2778 kfree(prev);
2779
2780 if (ret == 1)
2781 prev = backref;
2782 else
2783 prev = NULL;
2784 cond_resched();
2785 }
2786 kfree(prev);
2787
2788 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002789out:
Liu Bo6f519562013-10-29 10:45:05 +08002790 free_sa_defrag_extent(new);
2791
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002792 atomic_dec(&fs_info->defrag_running);
2793 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002794}
2795
2796static struct new_sa_defrag_extent *
2797record_old_file_extents(struct inode *inode,
2798 struct btrfs_ordered_extent *ordered)
2799{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002800 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002801 struct btrfs_root *root = BTRFS_I(inode)->root;
2802 struct btrfs_path *path;
2803 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002804 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002805 struct new_sa_defrag_extent *new;
2806 int ret;
2807
2808 new = kmalloc(sizeof(*new), GFP_NOFS);
2809 if (!new)
2810 return NULL;
2811
2812 new->inode = inode;
2813 new->file_pos = ordered->file_offset;
2814 new->len = ordered->len;
2815 new->bytenr = ordered->start;
2816 new->disk_len = ordered->disk_len;
2817 new->compress_type = ordered->compress_type;
2818 new->root = RB_ROOT;
2819 INIT_LIST_HEAD(&new->head);
2820
2821 path = btrfs_alloc_path();
2822 if (!path)
2823 goto out_kfree;
2824
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002825 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002826 key.type = BTRFS_EXTENT_DATA_KEY;
2827 key.offset = new->file_pos;
2828
2829 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2830 if (ret < 0)
2831 goto out_free_path;
2832 if (ret > 0 && path->slots[0] > 0)
2833 path->slots[0]--;
2834
2835 /* find out all the old extents for the file range */
2836 while (1) {
2837 struct btrfs_file_extent_item *extent;
2838 struct extent_buffer *l;
2839 int slot;
2840 u64 num_bytes;
2841 u64 offset;
2842 u64 end;
2843 u64 disk_bytenr;
2844 u64 extent_offset;
2845
2846 l = path->nodes[0];
2847 slot = path->slots[0];
2848
2849 if (slot >= btrfs_header_nritems(l)) {
2850 ret = btrfs_next_leaf(root, path);
2851 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002852 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002853 else if (ret > 0)
2854 break;
2855 continue;
2856 }
2857
2858 btrfs_item_key_to_cpu(l, &key, slot);
2859
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002860 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002861 break;
2862 if (key.type != BTRFS_EXTENT_DATA_KEY)
2863 break;
2864 if (key.offset >= new->file_pos + new->len)
2865 break;
2866
2867 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2868
2869 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2870 if (key.offset + num_bytes < new->file_pos)
2871 goto next;
2872
2873 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2874 if (!disk_bytenr)
2875 goto next;
2876
2877 extent_offset = btrfs_file_extent_offset(l, extent);
2878
2879 old = kmalloc(sizeof(*old), GFP_NOFS);
2880 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002881 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002882
2883 offset = max(new->file_pos, key.offset);
2884 end = min(new->file_pos + new->len, key.offset + num_bytes);
2885
2886 old->bytenr = disk_bytenr;
2887 old->extent_offset = extent_offset;
2888 old->offset = offset - key.offset;
2889 old->len = end - offset;
2890 old->new = new;
2891 old->count = 0;
2892 list_add_tail(&old->list, &new->head);
2893next:
2894 path->slots[0]++;
2895 cond_resched();
2896 }
2897
2898 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002899 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002900
2901 return new;
2902
Liu Bo38c227d2013-01-29 03:18:40 +00002903out_free_path:
2904 btrfs_free_path(path);
2905out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002906 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002907 return NULL;
2908}
2909
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002910static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002911 u64 start, u64 len)
2912{
2913 struct btrfs_block_group_cache *cache;
2914
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002915 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002916 ASSERT(cache);
2917
2918 spin_lock(&cache->lock);
2919 cache->delalloc_bytes -= len;
2920 spin_unlock(&cache->lock);
2921
2922 btrfs_put_block_group(cache);
2923}
2924
Chris Masond352ac62008-09-29 15:18:18 -04002925/* as ordered data IO finishes, this gets called so we can finish
2926 * an ordered extent if the range of bytes in the file it covers are
2927 * fully written.
2928 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002929static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002930{
Josef Bacik5fd02042012-05-02 14:00:54 -04002931 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002932 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002933 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002934 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002935 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002936 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002937 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002938 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002939 int ret = 0;
2940 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002941 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002942 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002943 bool range_locked = false;
2944 bool clear_new_delalloc_bytes = false;
2945
2946 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2947 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2948 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2949 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002950
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002951 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002952
Josef Bacik5fd02042012-05-02 14:00:54 -04002953 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2954 ret = -EIO;
2955 goto out;
2956 }
2957
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002958 btrfs_free_io_failure_record(BTRFS_I(inode),
2959 ordered_extent->file_offset,
2960 ordered_extent->file_offset +
2961 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002962
Josef Bacik77cef2e2013-08-29 13:57:21 -04002963 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2964 truncated = true;
2965 logical_len = ordered_extent->truncated_len;
2966 /* Truncated the entire extent, don't bother adding */
2967 if (!logical_len)
2968 goto out;
2969 }
2970
Yan, Zhengc2167752009-11-12 09:34:21 +00002971 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002972 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002973
2974 /*
2975 * For mwrite(mmap + memset to write) case, we still reserve
2976 * space for NOCOW range.
2977 * As NOCOW won't cause a new delayed ref, just free the space
2978 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002979 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002980 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002981 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2982 if (nolock)
2983 trans = btrfs_join_transaction_nolock(root);
2984 else
2985 trans = btrfs_join_transaction(root);
2986 if (IS_ERR(trans)) {
2987 ret = PTR_ERR(trans);
2988 trans = NULL;
2989 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002990 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002991 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002992 ret = btrfs_update_inode_fallback(trans, root, inode);
2993 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002994 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002995 goto out;
2996 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002997
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002998 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002999 lock_extent_bits(io_tree, ordered_extent->file_offset,
3000 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003001 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003002
Liu Bo38c227d2013-01-29 03:18:40 +00003003 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3004 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003005 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003006 if (ret) {
3007 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003008 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003009 /* the inode is shared */
3010 new = record_old_file_extents(inode, ordered_extent);
3011
3012 clear_extent_bit(io_tree, ordered_extent->file_offset,
3013 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003014 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003015 }
3016
Josef Bacik0cb59c92010-07-02 12:14:14 -04003017 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003018 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003019 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003020 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003021 if (IS_ERR(trans)) {
3022 ret = PTR_ERR(trans);
3023 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003024 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003025 }
Chris Masona79b7d42014-05-22 16:18:52 -07003026
Josef Bacik69fe2d72017-10-19 14:15:57 -04003027 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003028
Chris Masonc8b97812008-10-29 14:49:59 -04003029 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003030 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003031 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003032 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003033 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3034 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003035 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003036 ordered_extent->file_offset,
3037 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003038 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003039 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003040 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003041 ret = insert_reserved_file_extent(trans, inode,
3042 ordered_extent->file_offset,
3043 ordered_extent->start,
3044 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003045 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003046 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003047 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003048 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003049 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003050 ordered_extent->start,
3051 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003052 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003053 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3054 ordered_extent->file_offset, ordered_extent->len,
3055 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003056 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003057 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003058 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003059 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003060
David Sterbadf9f6282017-02-10 19:35:37 +01003061 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003062
Josef Bacik6c760c02012-11-09 10:53:21 -05003063 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3064 ret = btrfs_update_inode_fallback(trans, root, inode);
3065 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003066 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003067 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003068 }
3069 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003070out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003071 if (range_locked || clear_new_delalloc_bytes) {
3072 unsigned int clear_bits = 0;
3073
3074 if (range_locked)
3075 clear_bits |= EXTENT_LOCKED;
3076 if (clear_new_delalloc_bytes)
3077 clear_bits |= EXTENT_DELALLOC_NEW;
3078 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3079 ordered_extent->file_offset,
3080 ordered_extent->file_offset +
3081 ordered_extent->len - 1,
3082 clear_bits,
3083 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003084 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003085 }
3086
Miao Xiea698d0752012-09-20 01:51:59 -06003087 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003088 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003089
Josef Bacik77cef2e2013-08-29 13:57:21 -04003090 if (ret || truncated) {
3091 u64 start, end;
3092
3093 if (truncated)
3094 start = ordered_extent->file_offset + logical_len;
3095 else
3096 start = ordered_extent->file_offset;
3097 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003098 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003099
3100 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003101 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003102
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003103 /*
3104 * If the ordered extent had an IOERR or something else went
3105 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003106 * back to the allocator. We only free the extent in the
3107 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003108 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003109 if ((ret || !logical_len) &&
3110 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003111 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003112 btrfs_free_reserved_extent(fs_info,
3113 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003114 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003115 }
3116
3117
Josef Bacik5fd02042012-05-02 14:00:54 -04003118 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003119 * This needs to be done to make sure anybody waiting knows we are done
3120 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003121 */
3122 btrfs_remove_ordered_extent(inode, ordered_extent);
3123
Liu Bo38c227d2013-01-29 03:18:40 +00003124 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003125 if (new) {
3126 if (ret) {
3127 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003128 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003129 } else {
3130 relink_file_extents(new);
3131 }
3132 }
Liu Bo38c227d2013-01-29 03:18:40 +00003133
Chris Masone6dcd2d2008-07-17 12:53:50 -04003134 /* once for us */
3135 btrfs_put_ordered_extent(ordered_extent);
3136 /* once for the tree */
3137 btrfs_put_ordered_extent(ordered_extent);
3138
Josef Bacik5fd02042012-05-02 14:00:54 -04003139 return ret;
3140}
3141
3142static void finish_ordered_fn(struct btrfs_work *work)
3143{
3144 struct btrfs_ordered_extent *ordered_extent;
3145 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3146 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003147}
3148
David Sterbac3988d62017-02-17 15:18:32 +01003149static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003150 struct extent_state *state, int uptodate)
3151{
Josef Bacik5fd02042012-05-02 14:00:54 -04003152 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003153 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003154 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003155 struct btrfs_workqueue *wq;
3156 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003157
liubo1abe9b82011-03-24 11:18:59 +00003158 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3159
Chris Mason8b62b722009-09-02 16:53:46 -04003160 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003161 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3162 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003163 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003164
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003165 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003166 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003167 func = btrfs_freespace_write_helper;
3168 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003169 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003170 func = btrfs_endio_write_helper;
3171 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003172
Liu Bo9e0af232014-08-15 23:36:53 +08003173 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3174 NULL);
3175 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003176}
3177
Miao Xiedc380ae2014-09-12 18:43:55 +08003178static int __readpage_endio_check(struct inode *inode,
3179 struct btrfs_io_bio *io_bio,
3180 int icsum, struct page *page,
3181 int pgoff, u64 start, size_t len)
3182{
3183 char *kaddr;
3184 u32 csum_expected;
3185 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003186
3187 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3188
3189 kaddr = kmap_atomic(page);
3190 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003191 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003192 if (csum != csum_expected)
3193 goto zeroit;
3194
3195 kunmap_atomic(kaddr);
3196 return 0;
3197zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003198 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003199 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003200 memset(kaddr + pgoff, 1, len);
3201 flush_dcache_page(page);
3202 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003203 return -EIO;
3204}
3205
Chris Masond352ac62008-09-29 15:18:18 -04003206/*
Chris Masond352ac62008-09-29 15:18:18 -04003207 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003208 * if there's a match, we allow the bio to finish. If not, the code in
3209 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003210 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003211static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3212 u64 phy_offset, struct page *page,
3213 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003214{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003215 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003216 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003217 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003218 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003219
Chris Masond20f7042008-12-08 16:58:54 -05003220 if (PageChecked(page)) {
3221 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003222 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003223 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003224
3225 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003226 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003227
Yan Zheng17d217f2008-12-12 10:03:38 -05003228 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003229 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003230 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003231 return 0;
3232 }
3233
Miao Xiefacc8a222013-07-25 19:22:34 +08003234 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003235 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3236 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003237}
Chris Masonb888db22007-08-27 16:49:44 -04003238
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003239void btrfs_add_delayed_iput(struct inode *inode)
3240{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003241 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003242 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003243
3244 if (atomic_add_unless(&inode->i_count, -1, 1))
3245 return;
3246
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003247 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003248 if (binode->delayed_iput_count == 0) {
3249 ASSERT(list_empty(&binode->delayed_iput));
3250 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3251 } else {
3252 binode->delayed_iput_count++;
3253 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003254 spin_unlock(&fs_info->delayed_iput_lock);
3255}
3256
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003257void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003258{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003259
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003260 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003261 while (!list_empty(&fs_info->delayed_iputs)) {
3262 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003263
David Sterba8089fe62015-11-19 14:15:51 +01003264 inode = list_first_entry(&fs_info->delayed_iputs,
3265 struct btrfs_inode, delayed_iput);
3266 if (inode->delayed_iput_count) {
3267 inode->delayed_iput_count--;
3268 list_move_tail(&inode->delayed_iput,
3269 &fs_info->delayed_iputs);
3270 } else {
3271 list_del_init(&inode->delayed_iput);
3272 }
3273 spin_unlock(&fs_info->delayed_iput_lock);
3274 iput(&inode->vfs_inode);
3275 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003276 }
David Sterba8089fe62015-11-19 14:15:51 +01003277 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003278}
3279
Yan, Zhengd68fc572010-05-16 10:49:58 -04003280/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003281 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003282 * files in the subvolume, it removes orphan item and frees block_rsv
3283 * structure.
3284 */
3285void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3286 struct btrfs_root *root)
3287{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003288 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003289 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003290 int ret;
3291
Josef Bacik8a35d952012-05-23 14:26:42 -04003292 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003293 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3294 return;
3295
Josef Bacik90290e12011-12-02 15:44:12 -05003296 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003297 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003298 spin_unlock(&root->orphan_lock);
3299 return;
3300 }
3301
3302 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3303 spin_unlock(&root->orphan_lock);
3304 return;
3305 }
3306
3307 block_rsv = root->orphan_block_rsv;
3308 root->orphan_block_rsv = NULL;
3309 spin_unlock(&root->orphan_lock);
3310
Miao Xie27cdeb72014-04-02 19:51:05 +08003311 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003312 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003313 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003314 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003315 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003316 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003317 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003318 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3319 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003320 }
3321
Josef Bacik90290e12011-12-02 15:44:12 -05003322 if (block_rsv) {
3323 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003324 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003325 }
3326}
3327
3328/*
Josef Bacik7b128762008-07-24 12:17:14 -04003329 * This creates an orphan entry for the given inode in case something goes
3330 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003331 *
3332 * NOTE: caller of this function should reserve 5 units of metadata for
3333 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003334 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003335int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3336 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003337{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003338 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3339 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003340 struct btrfs_block_rsv *block_rsv = NULL;
3341 int reserve = 0;
3342 int insert = 0;
3343 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003344
Yan, Zhengd68fc572010-05-16 10:49:58 -04003345 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003346 block_rsv = btrfs_alloc_block_rsv(fs_info,
3347 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003348 if (!block_rsv)
3349 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003350 }
3351
Yan, Zhengd68fc572010-05-16 10:49:58 -04003352 spin_lock(&root->orphan_lock);
3353 if (!root->orphan_block_rsv) {
3354 root->orphan_block_rsv = block_rsv;
3355 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003356 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003357 block_rsv = NULL;
3358 }
Josef Bacik7b128762008-07-24 12:17:14 -04003359
Josef Bacik8a35d952012-05-23 14:26:42 -04003360 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003361 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003362#if 0
3363 /*
3364 * For proper ENOSPC handling, we should do orphan
3365 * cleanup when mounting. But this introduces backward
3366 * compatibility issue.
3367 */
3368 if (!xchg(&root->orphan_item_inserted, 1))
3369 insert = 2;
3370 else
3371 insert = 1;
3372#endif
3373 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003374 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003375 }
Josef Bacik7b128762008-07-24 12:17:14 -04003376
Josef Bacik72ac3c02012-05-23 14:13:11 -04003377 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003378 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003379 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003380 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003381
Yan, Zhengd68fc572010-05-16 10:49:58 -04003382 /* grab metadata reservation from transaction handle */
3383 if (reserve) {
3384 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003385 ASSERT(!ret);
3386 if (ret) {
3387 atomic_dec(&root->orphan_inodes);
3388 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003389 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003390 if (insert)
3391 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003392 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003393 return ret;
3394 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003395 }
3396
3397 /* insert an orphan item to track this unlinked/truncated file */
3398 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003399 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003400 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003401 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003402 if (reserve) {
3403 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003404 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003405 btrfs_orphan_release_metadata(inode);
3406 }
3407 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003408 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003409 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003410 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003411 return ret;
3412 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003413 }
3414 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003415 }
3416
3417 /* insert an orphan item to track subvolume contains orphan files */
3418 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003419 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003420 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003421 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003422 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003423 return ret;
3424 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003425 }
3426 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003427}
3428
3429/*
3430 * We have done the truncate/delete so we can go ahead and remove the orphan
3431 * item for this particular inode.
3432 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003433static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003434 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003435{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003436 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003437 int delete_item = 0;
3438 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003439 int ret = 0;
3440
Yan, Zhengd68fc572010-05-16 10:49:58 -04003441 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003442 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003443 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003444 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003445
Josef Bacik72ac3c02012-05-23 14:13:11 -04003446 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003447 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003448 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003449 spin_unlock(&root->orphan_lock);
3450
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003451 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003452 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003453 if (trans)
3454 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003455 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003456 }
Josef Bacik7b128762008-07-24 12:17:14 -04003457
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003458 if (release_rsv)
3459 btrfs_orphan_release_metadata(inode);
3460
Josef Bacik4ef31a42013-08-13 14:10:08 -04003461 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003462}
3463
3464/*
3465 * this cleans up any orphans that may be left on the list from the last use
3466 * of this root.
3467 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003468int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003469{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003470 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003471 struct btrfs_path *path;
3472 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003473 struct btrfs_key key, found_key;
3474 struct btrfs_trans_handle *trans;
3475 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003476 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003477 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3478
Yan, Zhengd68fc572010-05-16 10:49:58 -04003479 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003480 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003481
3482 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003483 if (!path) {
3484 ret = -ENOMEM;
3485 goto out;
3486 }
David Sterbae4058b52015-11-27 16:31:35 +01003487 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003488
3489 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003490 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003491 key.offset = (u64)-1;
3492
Josef Bacik7b128762008-07-24 12:17:14 -04003493 while (1) {
3494 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003495 if (ret < 0)
3496 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003497
3498 /*
3499 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003500 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003501 * find the key and see if we have stuff that matches
3502 */
3503 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003504 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003505 if (path->slots[0] == 0)
3506 break;
3507 path->slots[0]--;
3508 }
3509
3510 /* pull out the item */
3511 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003512 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3513
3514 /* make sure the item matches what we want */
3515 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3516 break;
David Sterba962a2982014-06-04 18:41:45 +02003517 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003518 break;
3519
3520 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003521 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003522
3523 /*
3524 * this is where we are basically btrfs_lookup, without the
3525 * crossing root thing. we store the inode number in the
3526 * offset of the orphan item.
3527 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003528
3529 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003530 btrfs_err(fs_info,
3531 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003532 ret = -EINVAL;
3533 goto out;
3534 }
3535
3536 last_objectid = found_key.offset;
3537
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003538 found_key.objectid = found_key.offset;
3539 found_key.type = BTRFS_INODE_ITEM_KEY;
3540 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003541 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303542 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003543 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003544 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003545
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003546 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003547 struct btrfs_root *dead_root;
3548 struct btrfs_fs_info *fs_info = root->fs_info;
3549 int is_dead_root = 0;
3550
3551 /*
3552 * this is an orphan in the tree root. Currently these
3553 * could come from 2 sources:
3554 * a) a snapshot deletion in progress
3555 * b) a free space cache inode
3556 * We need to distinguish those two, as the snapshot
3557 * orphan must not get deleted.
3558 * find_dead_roots already ran before us, so if this
3559 * is a snapshot deletion, we should find the root
3560 * in the dead_roots list
3561 */
3562 spin_lock(&fs_info->trans_lock);
3563 list_for_each_entry(dead_root, &fs_info->dead_roots,
3564 root_list) {
3565 if (dead_root->root_key.objectid ==
3566 found_key.objectid) {
3567 is_dead_root = 1;
3568 break;
3569 }
3570 }
3571 spin_unlock(&fs_info->trans_lock);
3572 if (is_dead_root) {
3573 /* prevent this orphan from being found again */
3574 key.offset = found_key.objectid - 1;
3575 continue;
3576 }
3577 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003578 /*
3579 * Inode is already gone but the orphan item is still there,
3580 * kill the orphan item.
3581 */
Filipe Manana67710892016-06-06 11:51:25 +01003582 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003583 trans = btrfs_start_transaction(root, 1);
3584 if (IS_ERR(trans)) {
3585 ret = PTR_ERR(trans);
3586 goto out;
3587 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003588 btrfs_debug(fs_info, "auto deleting %Lu",
3589 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003590 ret = btrfs_del_orphan_item(trans, root,
3591 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003592 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003593 if (ret)
3594 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003595 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003596 }
Josef Bacik7b128762008-07-24 12:17:14 -04003597
Josef Bacik7b128762008-07-24 12:17:14 -04003598 /*
3599 * add this inode to the orphan list so btrfs_orphan_del does
3600 * the proper thing when we hit it
3601 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003602 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3603 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003604 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003605
Josef Bacik7b128762008-07-24 12:17:14 -04003606 /* if we have links, this was a truncate, lets do that */
3607 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303608 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003609 iput(inode);
3610 continue;
3611 }
Josef Bacik7b128762008-07-24 12:17:14 -04003612 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003613
3614 /* 1 for the orphan item deletion. */
3615 trans = btrfs_start_transaction(root, 1);
3616 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003617 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003618 ret = PTR_ERR(trans);
3619 goto out;
3620 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003621 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003622 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003623 if (ret) {
3624 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003625 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003626 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003627
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003628 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003629 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003630 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003631 } else {
3632 nr_unlink++;
3633 }
3634
3635 /* this will do delete_inode and everything for us */
3636 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003637 if (ret)
3638 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003639 }
Miao Xie3254c872011-11-10 20:45:05 -05003640 /* release the path since we're done with it */
3641 btrfs_release_path(path);
3642
Yan, Zhengd68fc572010-05-16 10:49:58 -04003643 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3644
3645 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003646 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003647 (u64)-1);
3648
Miao Xie27cdeb72014-04-02 19:51:05 +08003649 if (root->orphan_block_rsv ||
3650 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003651 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003652 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003653 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003654 }
Josef Bacik7b128762008-07-24 12:17:14 -04003655
3656 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003657 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003658 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003659 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003660
3661out:
3662 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003663 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003664 btrfs_free_path(path);
3665 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003666}
3667
Chris Masond352ac62008-09-29 15:18:18 -04003668/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003669 * very simple check to peek ahead in the leaf looking for xattrs. If we
3670 * don't find any xattrs, we know there can't be any acls.
3671 *
3672 * slot is the slot the inode is in, objectid is the objectid of the inode
3673 */
3674static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003675 int slot, u64 objectid,
3676 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003677{
3678 u32 nritems = btrfs_header_nritems(leaf);
3679 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003680 static u64 xattr_access = 0;
3681 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003682 int scanned = 0;
3683
Josef Bacikf23b5a52013-06-19 10:16:26 -04003684 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003685 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3686 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3687 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3688 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003689 }
3690
Chris Mason46a53cc2009-04-27 11:47:50 -04003691 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003692 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003693 while (slot < nritems) {
3694 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3695
3696 /* we found a different objectid, there must not be acls */
3697 if (found_key.objectid != objectid)
3698 return 0;
3699
3700 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003701 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003702 if (*first_xattr_slot == -1)
3703 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003704 if (found_key.offset == xattr_access ||
3705 found_key.offset == xattr_default)
3706 return 1;
3707 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003708
3709 /*
3710 * we found a key greater than an xattr key, there can't
3711 * be any acls later on
3712 */
3713 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3714 return 0;
3715
3716 slot++;
3717 scanned++;
3718
3719 /*
3720 * it goes inode, inode backrefs, xattrs, extents,
3721 * so if there are a ton of hard links to an inode there can
3722 * be a lot of backrefs. Don't waste time searching too hard,
3723 * this is just an optimization
3724 */
3725 if (scanned >= 8)
3726 break;
3727 }
3728 /* we hit the end of the leaf before we found an xattr or
3729 * something larger than an xattr. We have to assume the inode
3730 * has acls
3731 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003732 if (*first_xattr_slot == -1)
3733 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003734 return 1;
3735}
3736
3737/*
Chris Masond352ac62008-09-29 15:18:18 -04003738 * read an inode from the btree into the in-memory inode
3739 */
Filipe Manana67710892016-06-06 11:51:25 +01003740static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003741{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003742 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003743 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003744 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003745 struct btrfs_inode_item *inode_item;
3746 struct btrfs_root *root = BTRFS_I(inode)->root;
3747 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003748 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003749 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003750 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003751 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003752 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003753 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003754
3755 ret = btrfs_fill_inode(inode, &rdev);
3756 if (!ret)
3757 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003758
3759 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003760 if (!path) {
3761 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003762 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003763 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003764
Chris Mason39279cc2007-06-12 06:35:45 -04003765 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003766
Chris Mason39279cc2007-06-12 06:35:45 -04003767 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003768 if (ret) {
3769 if (ret > 0)
3770 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003771 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003772 }
Chris Mason39279cc2007-06-12 06:35:45 -04003773
Chris Mason5f39d392007-10-15 16:14:19 -04003774 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003775
3776 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003777 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003778
Chris Mason5f39d392007-10-15 16:14:19 -04003779 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3780 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003781 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003782 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003783 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3784 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003785 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003786
David Sterbaa937b972014-12-12 17:39:12 +01003787 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3788 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003789
David Sterbaa937b972014-12-12 17:39:12 +01003790 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3791 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003792
David Sterbaa937b972014-12-12 17:39:12 +01003793 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3794 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003795
chandan r9cc97d62012-07-04 12:48:07 +05303796 BTRFS_I(inode)->i_otime.tv_sec =
3797 btrfs_timespec_sec(leaf, &inode_item->otime);
3798 BTRFS_I(inode)->i_otime.tv_nsec =
3799 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003800
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003801 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003802 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003803 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3804
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003805 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003806 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003807 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003808 rdev = btrfs_inode_rdev(leaf, inode_item);
3809
Josef Bacikaec74772008-07-24 12:12:38 -04003810 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003811 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003812
3813cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003814 /*
3815 * If we were modified in the current generation and evicted from memory
3816 * and then re-read we need to do a full sync since we don't have any
3817 * idea about which extents were modified before we were evicted from
3818 * cache.
3819 *
3820 * This is required for both inode re-read from disk and delayed inode
3821 * in delayed_nodes_tree.
3822 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003823 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003824 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3825 &BTRFS_I(inode)->runtime_flags);
3826
Filipe Mananabde6c242015-07-24 00:00:19 +01003827 /*
3828 * We don't persist the id of the transaction where an unlink operation
3829 * against the inode was last made. So here we assume the inode might
3830 * have been evicted, and therefore the exact value of last_unlink_trans
3831 * lost, and set it to last_trans to avoid metadata inconsistencies
3832 * between the inode and its parent if the inode is fsync'ed and the log
3833 * replayed. For example, in the scenario:
3834 *
3835 * touch mydir/foo
3836 * ln mydir/foo mydir/bar
3837 * sync
3838 * unlink mydir/bar
3839 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3840 * xfs_io -c fsync mydir/foo
3841 * <power failure>
3842 * mount fs, triggers fsync log replay
3843 *
3844 * We must make sure that when we fsync our inode foo we also log its
3845 * parent inode, otherwise after log replay the parent still has the
3846 * dentry with the "bar" name but our inode foo has a link count of 1
3847 * and doesn't have an inode ref with the name "bar" anymore.
3848 *
3849 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003850 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003851 * transaction commits on fsync if our inode is a directory, or if our
3852 * inode is not a directory, logging its parent unnecessarily.
3853 */
3854 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3855
Miao Xie67de1172013-12-26 13:07:06 +08003856 path->slots[0]++;
3857 if (inode->i_nlink != 1 ||
3858 path->slots[0] >= btrfs_header_nritems(leaf))
3859 goto cache_acl;
3860
3861 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003862 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003863 goto cache_acl;
3864
3865 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3866 if (location.type == BTRFS_INODE_REF_KEY) {
3867 struct btrfs_inode_ref *ref;
3868
3869 ref = (struct btrfs_inode_ref *)ptr;
3870 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3871 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3872 struct btrfs_inode_extref *extref;
3873
3874 extref = (struct btrfs_inode_extref *)ptr;
3875 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3876 extref);
3877 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003878cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003879 /*
3880 * try to precache a NULL acl entry for files that don't have
3881 * any xattrs or acls
3882 */
Li Zefan33345d012011-04-20 10:31:50 +08003883 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003884 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003885 if (first_xattr_slot != -1) {
3886 path->slots[0] = first_xattr_slot;
3887 ret = btrfs_load_inode_props(inode, path);
3888 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003889 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003890 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003891 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003892 root->root_key.objectid, ret);
3893 }
3894 btrfs_free_path(path);
3895
Al Viro72c04902009-06-24 16:58:48 -04003896 if (!maybe_acls)
3897 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003898
Chris Mason39279cc2007-06-12 06:35:45 -04003899 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003900 case S_IFREG:
3901 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003902 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003903 inode->i_fop = &btrfs_file_operations;
3904 inode->i_op = &btrfs_file_inode_operations;
3905 break;
3906 case S_IFDIR:
3907 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003908 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003909 break;
3910 case S_IFLNK:
3911 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003912 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003913 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3914 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003915 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003916 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003917 init_special_inode(inode, inode->i_mode, rdev);
3918 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003919 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003920
3921 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003922 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003923
3924make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003925 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003926 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003927 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003928}
3929
Chris Masond352ac62008-09-29 15:18:18 -04003930/*
3931 * given a leaf and an inode, copy the inode fields into the leaf
3932 */
Chris Masone02119d2008-09-05 16:13:11 -04003933static void fill_inode_item(struct btrfs_trans_handle *trans,
3934 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003935 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003936 struct inode *inode)
3937{
Liu Bo51fab692012-12-27 09:01:21 +00003938 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003939
Liu Bo51fab692012-12-27 09:01:21 +00003940 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003941
Liu Bo51fab692012-12-27 09:01:21 +00003942 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3943 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3944 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3945 &token);
3946 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3947 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003948
David Sterbaa937b972014-12-12 17:39:12 +01003949 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003950 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003951 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003952 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003953
David Sterbaa937b972014-12-12 17:39:12 +01003954 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003955 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003956 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003957 inode->i_mtime.tv_nsec, &token);
3958
David Sterbaa937b972014-12-12 17:39:12 +01003959 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003960 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003961 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003962 inode->i_ctime.tv_nsec, &token);
3963
chandan r9cc97d62012-07-04 12:48:07 +05303964 btrfs_set_token_timespec_sec(leaf, &item->otime,
3965 BTRFS_I(inode)->i_otime.tv_sec, &token);
3966 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3967 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3968
Liu Bo51fab692012-12-27 09:01:21 +00003969 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3970 &token);
3971 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3972 &token);
3973 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3974 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3975 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3976 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3977 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003978}
3979
Chris Masond352ac62008-09-29 15:18:18 -04003980/*
3981 * copy everything in the in-memory inode into the btree.
3982 */
Chris Mason21151332011-11-10 20:39:08 -05003983static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003984 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003985{
3986 struct btrfs_inode_item *inode_item;
3987 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003988 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003989 int ret;
3990
3991 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003992 if (!path)
3993 return -ENOMEM;
3994
Chris Masonb9473432009-03-13 11:00:37 -04003995 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003996 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3997 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003998 if (ret) {
3999 if (ret > 0)
4000 ret = -ENOENT;
4001 goto failed;
4002 }
4003
Chris Mason5f39d392007-10-15 16:14:19 -04004004 leaf = path->nodes[0];
4005 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004006 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004007
Chris Masone02119d2008-09-05 16:13:11 -04004008 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004009 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004010 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004011 ret = 0;
4012failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004013 btrfs_free_path(path);
4014 return ret;
4015}
4016
Chris Masond352ac62008-09-29 15:18:18 -04004017/*
Chris Mason21151332011-11-10 20:39:08 -05004018 * copy everything in the in-memory inode into the btree.
4019 */
4020noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4021 struct btrfs_root *root, struct inode *inode)
4022{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004023 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004024 int ret;
4025
4026 /*
4027 * If the inode is a free space inode, we can deadlock during commit
4028 * if we put it into the delayed code.
4029 *
4030 * The data relocation inode should also be directly updated
4031 * without delay
4032 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004033 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004034 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004035 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004036 btrfs_update_root_times(trans, root);
4037
Chris Mason21151332011-11-10 20:39:08 -05004038 ret = btrfs_delayed_update_inode(trans, root, inode);
4039 if (!ret)
4040 btrfs_set_inode_last_trans(trans, inode);
4041 return ret;
4042 }
4043
4044 return btrfs_update_inode_item(trans, root, inode);
4045}
4046
Josef Bacikbe6aef62012-10-22 15:43:12 -04004047noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4048 struct btrfs_root *root,
4049 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004050{
4051 int ret;
4052
4053 ret = btrfs_update_inode(trans, root, inode);
4054 if (ret == -ENOSPC)
4055 return btrfs_update_inode_item(trans, root, inode);
4056 return ret;
4057}
4058
4059/*
Chris Masond352ac62008-09-29 15:18:18 -04004060 * unlink helper that gets used here in inode.c and in the tree logging
4061 * recovery code. It remove a link in a directory with a given name, and
4062 * also drops the back refs in the inode to the directory
4063 */
Al Viro92986792011-03-04 17:14:37 +00004064static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4065 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004066 struct btrfs_inode *dir,
4067 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004068 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004069{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004070 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004071 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004072 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004073 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004074 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004075 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004076 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004077 u64 ino = btrfs_ino(inode);
4078 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004079
4080 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004081 if (!path) {
4082 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004083 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004084 }
4085
Chris Masonb9473432009-03-13 11:00:37 -04004086 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004087 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004088 name, name_len, -1);
4089 if (IS_ERR(di)) {
4090 ret = PTR_ERR(di);
4091 goto err;
4092 }
4093 if (!di) {
4094 ret = -ENOENT;
4095 goto err;
4096 }
Chris Mason5f39d392007-10-15 16:14:19 -04004097 leaf = path->nodes[0];
4098 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004099 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004100 if (ret)
4101 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004102 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004103
Miao Xie67de1172013-12-26 13:07:06 +08004104 /*
4105 * If we don't have dir index, we have to get it by looking up
4106 * the inode ref, since we get the inode ref, remove it directly,
4107 * it is unnecessary to do delayed deletion.
4108 *
4109 * But if we have dir index, needn't search inode ref to get it.
4110 * Since the inode ref is close to the inode item, it is better
4111 * that we delay to delete it, and just do this deletion when
4112 * we update the inode item.
4113 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004114 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004115 ret = btrfs_delayed_delete_inode_ref(inode);
4116 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004117 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004118 goto skip_backref;
4119 }
4120 }
4121
Li Zefan33345d012011-04-20 10:31:50 +08004122 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4123 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004124 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004125 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004126 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004127 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004128 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004129 goto err;
4130 }
Miao Xie67de1172013-12-26 13:07:06 +08004131skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004132 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004133 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004134 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004135 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004136 }
Chris Mason39279cc2007-06-12 06:35:45 -04004137
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004138 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4139 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004140 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004141 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004142 goto err;
4143 }
Chris Masone02119d2008-09-05 16:13:11 -04004144
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004145 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4146 index);
Chris Mason6418c962010-10-30 07:34:24 -04004147 if (ret == -ENOENT)
4148 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004149 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004150 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004151err:
4152 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004153 if (ret)
4154 goto out;
4155
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004156 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004157 inode_inc_iversion(&inode->vfs_inode);
4158 inode_inc_iversion(&dir->vfs_inode);
4159 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4160 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4161 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004162out:
Chris Mason39279cc2007-06-12 06:35:45 -04004163 return ret;
4164}
4165
Al Viro92986792011-03-04 17:14:37 +00004166int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4167 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004168 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004169 const char *name, int name_len)
4170{
4171 int ret;
4172 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4173 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004174 drop_nlink(&inode->vfs_inode);
4175 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004176 }
4177 return ret;
4178}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004179
4180/*
4181 * helper to start transaction for unlink and rmdir.
4182 *
Josef Bacikd52be812013-05-29 14:54:47 -04004183 * unlink and rmdir are special in btrfs, they do not always free space, so
4184 * if we cannot make our reservations the normal way try and see if there is
4185 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4186 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004187 */
Josef Bacikd52be812013-05-29 14:54:47 -04004188static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004189{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004190 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004191
Josef Bacike70bea52011-10-11 14:18:24 -04004192 /*
4193 * 1 for the possible orphan item
4194 * 1 for the dir item
4195 * 1 for the dir index
4196 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004197 * 1 for the inode
4198 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004199 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004200}
4201
Chris Mason39279cc2007-06-12 06:35:45 -04004202static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4203{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004204 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004205 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004206 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004207 int ret;
4208
Josef Bacikd52be812013-05-29 14:54:47 -04004209 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004210 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004211 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004212
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004213 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4214 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004215
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004216 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4217 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4218 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004219 if (ret)
4220 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004221
Yan, Zhenga22285a2010-05-16 10:48:46 -04004222 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004223 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004224 if (ret)
4225 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004226 }
Josef Bacik7b128762008-07-24 12:17:14 -04004227
Tsutomu Itohb5324022011-07-19 07:27:20 +00004228out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004229 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004230 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004231 return ret;
4232}
4233
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004234int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4235 struct btrfs_root *root,
4236 struct inode *dir, u64 objectid,
4237 const char *name, int name_len)
4238{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004239 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004240 struct btrfs_path *path;
4241 struct extent_buffer *leaf;
4242 struct btrfs_dir_item *di;
4243 struct btrfs_key key;
4244 u64 index;
4245 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004246 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004247
4248 path = btrfs_alloc_path();
4249 if (!path)
4250 return -ENOMEM;
4251
Li Zefan33345d012011-04-20 10:31:50 +08004252 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004253 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004254 if (IS_ERR_OR_NULL(di)) {
4255 if (!di)
4256 ret = -ENOENT;
4257 else
4258 ret = PTR_ERR(di);
4259 goto out;
4260 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004261
4262 leaf = path->nodes[0];
4263 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4264 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4265 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004266 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004267 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004268 goto out;
4269 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004270 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004271
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004272 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4273 root->root_key.objectid, dir_ino,
4274 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004275 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004276 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004277 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004278 goto out;
4279 }
Li Zefan33345d012011-04-20 10:31:50 +08004280 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004281 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004282 if (IS_ERR_OR_NULL(di)) {
4283 if (!di)
4284 ret = -ENOENT;
4285 else
4286 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004287 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004288 goto out;
4289 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004290
4291 leaf = path->nodes[0];
4292 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004293 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004294 index = key.offset;
4295 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004296 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004297
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004298 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004299 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004300 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004301 goto out;
4302 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004303
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004304 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004305 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004306 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004307 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004308 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004309 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004310out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004311 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004312 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004313}
4314
Chris Mason39279cc2007-06-12 06:35:45 -04004315static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4316{
David Howells2b0143b2015-03-17 22:25:59 +00004317 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004318 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004319 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004320 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004321 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004322
David Sterbab3ae2442012-09-13 16:04:34 -06004323 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004324 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004325 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004326 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004327
Josef Bacikd52be812013-05-29 14:54:47 -04004328 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004329 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004330 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004331
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004332 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004333 err = btrfs_unlink_subvol(trans, root, dir,
4334 BTRFS_I(inode)->location.objectid,
4335 dentry->d_name.name,
4336 dentry->d_name.len);
4337 goto out;
4338 }
4339
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004340 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004341 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004342 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004343
Filipe Manana44f714d2016-06-06 16:11:13 +01004344 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4345
Chris Mason39279cc2007-06-12 06:35:45 -04004346 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004347 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4348 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4349 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004350 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004351 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004352 /*
4353 * Propagate the last_unlink_trans value of the deleted dir to
4354 * its parent directory. This is to prevent an unrecoverable
4355 * log tree in the case we do something like this:
4356 * 1) create dir foo
4357 * 2) create snapshot under dir foo
4358 * 3) delete the snapshot
4359 * 4) rmdir foo
4360 * 5) mkdir foo
4361 * 6) fsync foo or some file inside foo
4362 */
4363 if (last_unlink_trans >= trans->transid)
4364 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4365 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004366out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004367 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004368 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004369
Chris Mason39279cc2007-06-12 06:35:45 -04004370 return err;
4371}
4372
Chris Mason28f75a02015-02-04 06:59:29 -08004373static int truncate_space_check(struct btrfs_trans_handle *trans,
4374 struct btrfs_root *root,
4375 u64 bytes_deleted)
4376{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004377 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004378 int ret;
4379
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004380 /*
4381 * This is only used to apply pressure to the enospc system, we don't
4382 * intend to use this reservation at all.
4383 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004384 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004385 bytes_deleted *= fs_info->nodesize;
4386 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004387 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004388 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004389 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004390 trans->transid,
4391 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004392 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004393 }
Chris Mason28f75a02015-02-04 06:59:29 -08004394 return ret;
4395
4396}
4397
Josef Bacikddfae632017-10-19 14:16:02 -04004398/*
4399 * Return this if we need to call truncate_block for the last bit of the
4400 * truncate.
4401 */
4402#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004403
Chris Mason323ac952008-10-01 19:05:46 -04004404/*
Chris Mason39279cc2007-06-12 06:35:45 -04004405 * this can truncate away extent items, csum items and directory items.
4406 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004407 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004408 *
4409 * csum items that cross the new i_size are truncated to the new size
4410 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004411 *
4412 * min_type is the minimum key type to truncate down to. If set to 0, this
4413 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004414 */
Yan, Zheng80825102009-11-12 09:35:36 +00004415int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4416 struct btrfs_root *root,
4417 struct inode *inode,
4418 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004419{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004420 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004421 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004422 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004423 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004424 struct btrfs_key key;
4425 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004426 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004427 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004428 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004429 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004430 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004431 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004432 int found_extent;
4433 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004434 int pending_del_nr = 0;
4435 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004436 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004437 int ret;
4438 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004439 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004440 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004441 bool be_nice = false;
4442 bool should_throttle = false;
4443 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004444
4445 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004446
Chris Mason28ed1342014-12-17 09:41:04 -08004447 /*
4448 * for non-free space inodes and ref cows, we want to back off from
4449 * time to time
4450 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004451 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004452 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004453 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004454
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004455 path = btrfs_alloc_path();
4456 if (!path)
4457 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004458 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004459
Josef Bacik5dc562c2012-08-17 13:14:17 -04004460 /*
4461 * We want to drop from the next block forward in case this new size is
4462 * not block aligned since we will be keeping the last block of the
4463 * extent just the way it is.
4464 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004465 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004466 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004467 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004468 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004469 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004470
Miao Xie16cdcec2011-04-22 18:12:22 +08004471 /*
4472 * This function is also used to drop the items in the log tree before
4473 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4474 * it is used to drop the loged items. So we shouldn't kill the delayed
4475 * items.
4476 */
4477 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004478 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004479
Li Zefan33345d012011-04-20 10:31:50 +08004480 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004481 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004482 key.type = (u8)-1;
4483
Chris Mason85e21ba2008-01-29 15:11:36 -05004484search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004485 /*
4486 * with a 16K leaf size and 128MB extents, you can actually queue
4487 * up a huge file in a single leaf. Most of the time that
4488 * bytes_deleted is > 0, it will be huge by the time we get here
4489 */
Byongho Leeee221842015-12-15 01:42:10 +09004490 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004491 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004492 err = -EAGAIN;
4493 goto error;
4494 }
4495 }
4496
4497
Chris Masonb9473432009-03-13 11:00:37 -04004498 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004499 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004500 if (ret < 0) {
4501 err = ret;
4502 goto out;
4503 }
Chris Masond3977122009-01-05 21:25:51 -05004504
Chris Mason85e21ba2008-01-29 15:11:36 -05004505 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004506 /* there are no items in the tree for us to truncate, we're
4507 * done
4508 */
Yan, Zheng80825102009-11-12 09:35:36 +00004509 if (path->slots[0] == 0)
4510 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004511 path->slots[0]--;
4512 }
4513
Chris Masond3977122009-01-05 21:25:51 -05004514 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004515 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004516 leaf = path->nodes[0];
4517 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004518 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004519
Li Zefan33345d012011-04-20 10:31:50 +08004520 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004521 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004522
Chris Mason85e21ba2008-01-29 15:11:36 -05004523 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004524 break;
4525
Chris Mason5f39d392007-10-15 16:14:19 -04004526 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004527 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004528 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004529 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004530 extent_type = btrfs_file_extent_type(leaf, fi);
4531 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004532 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004533 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004534
4535 trace_btrfs_truncate_show_fi_regular(
4536 BTRFS_I(inode), leaf, fi,
4537 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004538 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004539 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004540 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004541
4542 trace_btrfs_truncate_show_fi_inline(
4543 BTRFS_I(inode), leaf, fi, path->slots[0],
4544 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004545 }
Yan008630c2007-11-07 13:31:09 -05004546 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004547 }
Yan, Zheng80825102009-11-12 09:35:36 +00004548 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004549 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004550 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004551 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004552 break;
4553 if (found_key.offset >= new_size)
4554 del_item = 1;
4555 else
4556 del_item = 0;
4557 }
Chris Mason39279cc2007-06-12 06:35:45 -04004558 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004559 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004560 if (found_type != BTRFS_EXTENT_DATA_KEY)
4561 goto delete;
4562
4563 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004564 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004565 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004566 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004567 u64 orig_num_bytes =
4568 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004569 extent_num_bytes = ALIGN(new_size -
4570 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004571 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004572 btrfs_set_file_extent_num_bytes(leaf, fi,
4573 extent_num_bytes);
4574 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004575 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004576 if (test_bit(BTRFS_ROOT_REF_COWS,
4577 &root->state) &&
4578 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004579 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004580 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004581 } else {
Chris Masondb945352007-10-15 16:15:53 -04004582 extent_num_bytes =
4583 btrfs_file_extent_disk_num_bytes(leaf,
4584 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004585 extent_offset = found_key.offset -
4586 btrfs_file_extent_offset(leaf, fi);
4587
Chris Mason39279cc2007-06-12 06:35:45 -04004588 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004589 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004590 if (extent_start != 0) {
4591 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004592 if (test_bit(BTRFS_ROOT_REF_COWS,
4593 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004594 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004595 }
4596 }
Chris Mason90692182008-02-08 13:49:28 -05004597 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004598 /*
4599 * we can't truncate inline items that have had
4600 * special encodings
4601 */
4602 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004603 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004604 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4605 btrfs_file_extent_compression(leaf, fi) == 0) {
4606 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004607
Josef Bacikddfae632017-10-19 14:16:02 -04004608 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4609 size = btrfs_file_extent_calc_inline_size(size);
4610 btrfs_truncate_item(root->fs_info, path, size, 1);
4611 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004612 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004613 * We have to bail so the last_size is set to
4614 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004615 */
Josef Bacikddfae632017-10-19 14:16:02 -04004616 err = NEED_TRUNCATE_BLOCK;
4617 break;
Chris Mason90692182008-02-08 13:49:28 -05004618 }
Josef Bacikddfae632017-10-19 14:16:02 -04004619
4620 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4621 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004622 }
Chris Mason179e29e2007-11-01 11:28:41 -04004623delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004624 if (del_item)
4625 last_size = found_key.offset;
4626 else
4627 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004628 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004629 if (!pending_del_nr) {
4630 /* no pending yet, add ourselves */
4631 pending_del_slot = path->slots[0];
4632 pending_del_nr = 1;
4633 } else if (pending_del_nr &&
4634 path->slots[0] + 1 == pending_del_slot) {
4635 /* hop on the pending chunk */
4636 pending_del_nr++;
4637 pending_del_slot = path->slots[0];
4638 } else {
Chris Masond3977122009-01-05 21:25:51 -05004639 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004640 }
Chris Mason39279cc2007-06-12 06:35:45 -04004641 } else {
4642 break;
4643 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004644 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004645
Miao Xie27cdeb72014-04-02 19:51:05 +08004646 if (found_extent &&
4647 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004648 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004649 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004650 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004651 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004652 extent_num_bytes, 0,
4653 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004654 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004655 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004656 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4657 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004658 trans->delayed_ref_updates * 2,
4659 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004660 if (be_nice) {
4661 if (truncate_space_check(trans, root,
4662 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004663 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004664 }
4665 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004666 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004667 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004668 }
Chris Mason39279cc2007-06-12 06:35:45 -04004669 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004670
Yan, Zheng80825102009-11-12 09:35:36 +00004671 if (found_type == BTRFS_INODE_ITEM_KEY)
4672 break;
4673
4674 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004675 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004676 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004677 if (pending_del_nr) {
4678 ret = btrfs_del_items(trans, root, path,
4679 pending_del_slot,
4680 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004681 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004682 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004683 goto error;
4684 }
Yan, Zheng80825102009-11-12 09:35:36 +00004685 pending_del_nr = 0;
4686 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004687 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004688 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004689 unsigned long updates = trans->delayed_ref_updates;
4690 if (updates) {
4691 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004692 ret = btrfs_run_delayed_refs(trans,
4693 fs_info,
4694 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004695 if (ret && !err)
4696 err = ret;
4697 }
4698 }
Chris Mason28f75a02015-02-04 06:59:29 -08004699 /*
4700 * if we failed to refill our space rsv, bail out
4701 * and let the transaction restart
4702 */
4703 if (should_end) {
4704 err = -EAGAIN;
4705 goto error;
4706 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004707 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004708 } else {
4709 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004710 }
Chris Mason39279cc2007-06-12 06:35:45 -04004711 }
Yan, Zheng80825102009-11-12 09:35:36 +00004712out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004713 if (pending_del_nr) {
4714 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4715 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004716 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004717 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004718 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004719error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004720 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4721 ASSERT(last_size >= new_size);
4722 if (!err && last_size > new_size)
4723 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004724 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004725 }
Chris Mason28ed1342014-12-17 09:41:04 -08004726
Chris Mason39279cc2007-06-12 06:35:45 -04004727 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004728
Byongho Leeee221842015-12-15 01:42:10 +09004729 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004730 unsigned long updates = trans->delayed_ref_updates;
4731 if (updates) {
4732 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004733 ret = btrfs_run_delayed_refs(trans, fs_info,
4734 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004735 if (ret && !err)
4736 err = ret;
4737 }
4738 }
Yan, Zheng80825102009-11-12 09:35:36 +00004739 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004740}
4741
Chris Masona52d9a82007-08-27 16:49:44 -04004742/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304743 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004744 * @inode - inode that we're zeroing
4745 * @from - the offset to start zeroing
4746 * @len - the length to zero, 0 to zero the entire range respective to the
4747 * offset
4748 * @front - zero up to the offset instead of from the offset on
4749 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304750 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004751 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004752 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304753int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004754 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004755{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004756 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004757 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004758 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4759 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004760 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004761 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004762 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004763 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004764 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304765 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004766 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004767 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004768 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304769 u64 block_start;
4770 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004771
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004772 if (IS_ALIGNED(offset, blocksize) &&
4773 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004774 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304775
Josef Bacik8b62f872017-10-19 14:15:55 -04004776 block_start = round_down(from, blocksize);
4777 block_end = block_start + blocksize - 1;
4778
Qu Wenruo364ecf32017-02-27 15:10:38 +08004779 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004780 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004781 if (ret)
4782 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004783
Chris Mason211c17f2008-05-15 09:13:45 -04004784again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004785 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004786 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004787 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004788 block_start, blocksize);
4789 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004790 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004791 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004792 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004793
Chris Masona52d9a82007-08-27 16:49:44 -04004794 if (!PageUptodate(page)) {
4795 ret = btrfs_readpage(NULL, page);
4796 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004797 if (page->mapping != mapping) {
4798 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004799 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004800 goto again;
4801 }
Chris Masona52d9a82007-08-27 16:49:44 -04004802 if (!PageUptodate(page)) {
4803 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004804 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004805 }
4806 }
Chris Mason211c17f2008-05-15 09:13:45 -04004807 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004808
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304809 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004810 set_page_extent_mapped(page);
4811
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304812 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004813 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304814 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004815 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004816 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004817 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004818 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004819 btrfs_put_ordered_extent(ordered);
4820 goto again;
4821 }
4822
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304823 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004824 EXTENT_DIRTY | EXTENT_DELALLOC |
4825 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004826 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004827
Filipe Mananae3b8a482017-11-04 00:16:59 +00004828 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004829 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004830 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304831 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004832 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004833 goto out_unlock;
4834 }
4835
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304836 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004837 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304838 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004839 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004840 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304841 memset(kaddr + (block_start - page_offset(page)),
4842 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004843 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304844 memset(kaddr + (block_start - page_offset(page)) + offset,
4845 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004846 flush_dcache_page(page);
4847 kunmap(page);
4848 }
Chris Mason247e7432008-07-17 12:53:51 -04004849 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004850 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004851 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004852
Chris Mason89642222008-07-24 09:41:53 -04004853out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004854 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004855 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304856 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004857 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004858 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004859 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004860out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004861 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004862 return ret;
4863}
4864
Josef Bacik16e75492013-10-22 12:18:51 -04004865static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4866 u64 offset, u64 len)
4867{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004868 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004869 struct btrfs_trans_handle *trans;
4870 int ret;
4871
4872 /*
4873 * Still need to make sure the inode looks like it's been updated so
4874 * that any holes get logged if we fsync.
4875 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004876 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4877 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004878 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4879 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4880 return 0;
4881 }
4882
4883 /*
4884 * 1 - for the one we're dropping
4885 * 1 - for the one we're adding
4886 * 1 - for updating the inode.
4887 */
4888 trans = btrfs_start_transaction(root, 3);
4889 if (IS_ERR(trans))
4890 return PTR_ERR(trans);
4891
4892 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4893 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004894 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004895 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004896 return ret;
4897 }
4898
David Sterbaf85b7372017-01-20 14:54:07 +01004899 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4900 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004901 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004902 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004903 else
4904 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004905 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004906 return ret;
4907}
4908
Josef Bacik695a0d02011-03-04 15:46:53 -05004909/*
4910 * This function puts in dummy file extents for the area we're creating a hole
4911 * for. So if we are truncating this file to a larger size we need to insert
4912 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4913 * the range between oldsize and size
4914 */
Josef Bacika41ad392011-01-31 15:30:16 -05004915int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004916{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004917 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004918 struct btrfs_root *root = BTRFS_I(inode)->root;
4919 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004920 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004921 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004922 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004923 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4924 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004925 u64 last_byte;
4926 u64 cur_offset;
4927 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004928 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004929
Josef Bacika71754f2013-06-17 17:14:39 -04004930 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304931 * If our size started in the middle of a block we need to zero out the
4932 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004933 * expose stale data.
4934 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304935 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004936 if (err)
4937 return err;
4938
Yan Zheng9036c102008-10-30 14:19:41 -04004939 if (size <= hole_start)
4940 return 0;
4941
Yan Zheng9036c102008-10-30 14:19:41 -04004942 while (1) {
4943 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004944
David Sterbaff13db42015-12-03 14:30:40 +01004945 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004946 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004947 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004948 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004949 if (!ordered)
4950 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004951 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004952 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004953 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004954 btrfs_put_ordered_extent(ordered);
4955 }
4956
Yan Zheng9036c102008-10-30 14:19:41 -04004957 cur_offset = hole_start;
4958 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004959 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004960 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004961 if (IS_ERR(em)) {
4962 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004963 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004964 break;
4965 }
Yan Zheng9036c102008-10-30 14:19:41 -04004966 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004967 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004968 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004969 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004970 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004971
Josef Bacik16e75492013-10-22 12:18:51 -04004972 err = maybe_insert_hole(root, inode, cur_offset,
4973 hole_size);
4974 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004975 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004976 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004977 cur_offset + hole_size - 1, 0);
4978 hole_em = alloc_extent_map();
4979 if (!hole_em) {
4980 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4981 &BTRFS_I(inode)->runtime_flags);
4982 goto next;
4983 }
4984 hole_em->start = cur_offset;
4985 hole_em->len = hole_size;
4986 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004987
Josef Bacik5dc562c2012-08-17 13:14:17 -04004988 hole_em->block_start = EXTENT_MAP_HOLE;
4989 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004990 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004991 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004992 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004993 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004994 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004995
4996 while (1) {
4997 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004998 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004999 write_unlock(&em_tree->lock);
5000 if (err != -EEXIST)
5001 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005002 btrfs_drop_extent_cache(BTRFS_I(inode),
5003 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005004 cur_offset +
5005 hole_size - 1, 0);
5006 }
5007 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005008 }
Josef Bacik16e75492013-10-22 12:18:51 -04005009next:
Yan Zheng9036c102008-10-30 14:19:41 -04005010 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005011 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005012 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005013 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005014 break;
5015 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005016 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005017 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005018 return err;
5019}
5020
Eric Sandeen3972f262013-01-12 02:57:22 +00005021static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005022{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005023 struct btrfs_root *root = BTRFS_I(inode)->root;
5024 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005025 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005026 loff_t newsize = attr->ia_size;
5027 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005028 int ret;
5029
Eric Sandeen3972f262013-01-12 02:57:22 +00005030 /*
5031 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5032 * special case where we need to update the times despite not having
5033 * these flags set. For all other operations the VFS set these flags
5034 * explicitly if it wants a timestamp update.
5035 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005036 if (newsize != oldsize) {
5037 inode_inc_iversion(inode);
5038 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5039 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005040 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005041 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005042
Josef Bacika41ad392011-01-31 15:30:16 -05005043 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005044 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005045 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005046 * This is to ensure the snapshot captures a fully consistent
5047 * state of this file - if the snapshot captures this expanding
5048 * truncation, it must capture all writes that happened before
5049 * this truncation.
5050 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005051 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005052 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005053 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005054 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005055 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005056 }
Yan, Zheng80825102009-11-12 09:35:36 +00005057
Miao Xief4a2f4c2011-12-14 20:12:01 -05005058 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005059 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005060 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005061 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005062 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005063
5064 i_size_write(inode, newsize);
5065 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305066 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005067 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005068 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005069 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005070 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005071
Josef Bacika41ad392011-01-31 15:30:16 -05005072 /*
5073 * We're truncating a file that used to have good data down to
5074 * zero. Make sure it gets into the ordered flush list so that
5075 * any new writes get down to disk quickly.
5076 */
5077 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005078 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5079 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005080
Josef Bacikf3fe8202013-01-07 17:03:21 -05005081 /*
5082 * 1 for the orphan item we're going to add
5083 * 1 for the orphan item deletion.
5084 */
5085 trans = btrfs_start_transaction(root, 2);
5086 if (IS_ERR(trans))
5087 return PTR_ERR(trans);
5088
5089 /*
5090 * We need to do this in case we fail at _any_ point during the
5091 * actual truncate. Once we do the truncate_setsize we could
5092 * invalidate pages which forces any outstanding ordered io to
5093 * be instantly completed which will give us extents that need
5094 * to be truncated. If we fail to get an orphan inode down we
5095 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005096 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005097 * will be consistent.
5098 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005099 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005100 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005101 if (ret)
5102 return ret;
5103
Josef Bacika41ad392011-01-31 15:30:16 -05005104 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5105 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005106
5107 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005108 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005109 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005110 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005111
Josef Bacika41ad392011-01-31 15:30:16 -05005112 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005113 if (ret && inode->i_nlink) {
5114 int err;
5115
Liu Bo19fd2df2016-12-01 13:01:02 -08005116 /* To get a stable disk_i_size */
5117 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5118 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005119 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005120 return err;
5121 }
5122
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005123 /*
5124 * failed to truncate, disk_i_size is only adjusted down
5125 * as we remove extents, so it should represent the true
5126 * size of the inode, so reset the in memory size and
5127 * delete our orphan entry.
5128 */
5129 trans = btrfs_join_transaction(root);
5130 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005131 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005132 return ret;
5133 }
5134 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005135 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005136 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005137 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005138 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005139 }
Yan, Zheng80825102009-11-12 09:35:36 +00005140 }
5141
Josef Bacika41ad392011-01-31 15:30:16 -05005142 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005143}
5144
Chris Mason39279cc2007-06-12 06:35:45 -04005145static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5146{
David Howells2b0143b2015-03-17 22:25:59 +00005147 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005148 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005149 int err;
5150
Li Zefanb83cc962010-12-20 16:04:08 +08005151 if (btrfs_root_readonly(root))
5152 return -EROFS;
5153
Jan Kara31051c82016-05-26 16:55:18 +02005154 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005155 if (err)
5156 return err;
5157
Chris Mason5a3f23d2009-03-31 13:27:11 -04005158 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005159 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005160 if (err)
5161 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005162 }
Yan Zheng9036c102008-10-30 14:19:41 -04005163
Christoph Hellwig10257742010-06-04 11:30:02 +02005164 if (attr->ia_valid) {
5165 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005166 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005167 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005168
Josef Bacik22c44fe2011-11-30 10:45:38 -05005169 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005170 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005171 }
5172
Chris Mason39279cc2007-06-12 06:35:45 -04005173 return err;
5174}
Chris Mason61295eb2008-01-14 16:24:38 -05005175
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005176/*
5177 * While truncating the inode pages during eviction, we get the VFS calling
5178 * btrfs_invalidatepage() against each page of the inode. This is slow because
5179 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5180 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5181 * extent_state structures over and over, wasting lots of time.
5182 *
5183 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5184 * those expensive operations on a per page basis and do only the ordered io
5185 * finishing, while we release here the extent_map and extent_state structures,
5186 * without the excessive merging and splitting.
5187 */
5188static void evict_inode_truncate_pages(struct inode *inode)
5189{
5190 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5191 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5192 struct rb_node *node;
5193
5194 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005195 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005196
5197 write_lock(&map_tree->lock);
5198 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5199 struct extent_map *em;
5200
5201 node = rb_first(&map_tree->map);
5202 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005203 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5204 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005205 remove_extent_mapping(map_tree, em);
5206 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005207 if (need_resched()) {
5208 write_unlock(&map_tree->lock);
5209 cond_resched();
5210 write_lock(&map_tree->lock);
5211 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005212 }
5213 write_unlock(&map_tree->lock);
5214
Filipe Manana6ca07092015-05-26 00:55:42 +01005215 /*
5216 * Keep looping until we have no more ranges in the io tree.
5217 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005218 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5219 * still in progress (unlocked the pages in the bio but did not yet
5220 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005221 * ranges can still be locked and eviction started because before
5222 * submitting those bios, which are executed by a separate task (work
5223 * queue kthread), inode references (inode->i_count) were not taken
5224 * (which would be dropped in the end io callback of each bio).
5225 * Therefore here we effectively end up waiting for those bios and
5226 * anyone else holding locked ranges without having bumped the inode's
5227 * reference count - if we don't do it, when they access the inode's
5228 * io_tree to unlock a range it may be too late, leading to an
5229 * use-after-free issue.
5230 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005231 spin_lock(&io_tree->lock);
5232 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5233 struct extent_state *state;
5234 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005235 u64 start;
5236 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005237
5238 node = rb_first(&io_tree->state);
5239 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005240 start = state->start;
5241 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005242 spin_unlock(&io_tree->lock);
5243
David Sterbaff13db42015-12-03 14:30:40 +01005244 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005245
5246 /*
5247 * If still has DELALLOC flag, the extent didn't reach disk,
5248 * and its reserved space won't be freed by delayed_ref.
5249 * So we need to free its reserved space here.
5250 * (Refer to comment in btrfs_invalidatepage, case 2)
5251 *
5252 * Note, end is the bytenr of last byte, so we need + 1 here.
5253 */
5254 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005255 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005256
Filipe Manana6ca07092015-05-26 00:55:42 +01005257 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005258 EXTENT_LOCKED | EXTENT_DIRTY |
5259 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005260 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005261
Filipe Manana7064dd52014-08-08 02:47:05 +01005262 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005263 spin_lock(&io_tree->lock);
5264 }
5265 spin_unlock(&io_tree->lock);
5266}
5267
Al Virobd555972010-06-07 11:35:40 -04005268void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005269{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005270 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005271 struct btrfs_trans_handle *trans;
5272 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005273 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005274 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005275 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005276 int ret;
5277
liubo1abe9b82011-03-24 11:18:59 +00005278 trace_btrfs_inode_evict(inode);
5279
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005280 if (!root) {
5281 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5282 return;
5283 }
5284
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005285 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005286
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005287 evict_inode_truncate_pages(inode);
5288
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005289 if (inode->i_nlink &&
5290 ((btrfs_root_refs(&root->root_item) != 0 &&
5291 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005292 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005293 goto no_delete;
5294
Chris Mason39279cc2007-06-12 06:35:45 -04005295 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005296 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005297 goto no_delete;
5298 }
Al Virobd555972010-06-07 11:35:40 -04005299 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005300 if (!special_file(inode->i_mode))
5301 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005302
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005303 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005304
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005305 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005306 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005307 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005308 goto no_delete;
5309 }
5310
Yan, Zheng76dda932009-09-21 16:00:26 -04005311 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005312 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5313 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005314 goto no_delete;
5315 }
5316
Nikolay Borisovaa790212017-01-10 20:35:40 +02005317 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005318 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005319 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005320 goto no_delete;
5321 }
5322
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005323 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005324 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005325 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005326 goto no_delete;
5327 }
Josef Bacik4a338542011-08-29 11:01:31 -04005328 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005329 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005330 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005331
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005332 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005333
Josef Bacik4289a662011-08-05 13:22:24 -04005334 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005335 * This is a bit simpler than btrfs_truncate since we've already
5336 * reserved our space for our orphan item in the unlink, so we just
5337 * need to reserve some slack space in case we add bytes and update
5338 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005339 */
Yan, Zheng80825102009-11-12 09:35:36 +00005340 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005341 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5342 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005343
Josef Bacik726c35f2011-09-26 15:46:06 -04005344 /*
5345 * Try and steal from the global reserve since we will
5346 * likely not use this space anyway, we want to try as
5347 * hard as possible to get this to work.
5348 */
5349 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005350 steal_from_global++;
5351 else
5352 steal_from_global = 0;
5353 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005354
Josef Bacik3bce8762015-02-24 12:35:51 -08005355 /*
5356 * steal_from_global == 0: we reserved stuff, hooray!
5357 * steal_from_global == 1: we didn't reserve stuff, boo!
5358 * steal_from_global == 2: we've committed, still not a lot of
5359 * room but maybe we'll have room in the global reserve this
5360 * time.
5361 * steal_from_global == 3: abandon all hope!
5362 */
5363 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005364 btrfs_warn(fs_info,
5365 "Could not get space for a delete, will truncate on mount %d",
5366 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005367 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005368 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005369 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005370 }
5371
Miao Xie0e8c36a2012-12-19 06:59:51 +00005372 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005373 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005374 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005375 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005376 goto no_delete;
5377 }
5378
Josef Bacik3bce8762015-02-24 12:35:51 -08005379 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005380 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005381 * sure there is room to do it, if not we need to commit and try
5382 * again.
5383 */
5384 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005385 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005386 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005387 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005388 else
5389 ret = -ENOSPC;
5390 }
5391
5392 /*
5393 * Couldn't steal from the global reserve, we have too much
5394 * pending stuff built up, commit the transaction and try it
5395 * again.
5396 */
5397 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005398 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005399 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005400 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005401 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005402 goto no_delete;
5403 }
5404 continue;
5405 } else {
5406 steal_from_global = 0;
5407 }
5408
Josef Bacik4289a662011-08-05 13:22:24 -04005409 trans->block_rsv = rsv;
5410
Yan, Zhengd68fc572010-05-16 10:49:58 -04005411 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005412 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005413 break;
5414
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005415 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005416 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005417 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005418 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005419 }
5420
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005421 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005422
Josef Bacik4ef31a42013-08-13 14:10:08 -04005423 /*
5424 * Errors here aren't a big deal, it just means we leave orphan items
5425 * in the tree. They will be cleaned up on the next mount.
5426 */
Yan, Zheng80825102009-11-12 09:35:36 +00005427 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005428 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005429 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005430 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005431 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005432 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005433
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005434 trans->block_rsv = &fs_info->trans_block_rsv;
5435 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005436 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005437 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005438
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005439 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005440 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005441no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005442 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005443 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005444}
5445
5446/*
5447 * this returns the key found in the dir entry in the location pointer.
5448 * If no dir entries were found, location->objectid is 0.
5449 */
5450static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5451 struct btrfs_key *location)
5452{
5453 const char *name = dentry->d_name.name;
5454 int namelen = dentry->d_name.len;
5455 struct btrfs_dir_item *di;
5456 struct btrfs_path *path;
5457 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005458 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005459
5460 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005461 if (!path)
5462 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005463
David Sterbaf85b7372017-01-20 14:54:07 +01005464 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5465 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005466 if (IS_ERR(di))
5467 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005468
David Sterbac7040052011-04-19 18:00:01 +02005469 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005470 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005471
Chris Mason5f39d392007-10-15 16:14:19 -04005472 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005473 if (location->type != BTRFS_INODE_ITEM_KEY &&
5474 location->type != BTRFS_ROOT_ITEM_KEY) {
5475 btrfs_warn(root->fs_info,
5476"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5477 __func__, name, btrfs_ino(BTRFS_I(dir)),
5478 location->objectid, location->type, location->offset);
5479 goto out_err;
5480 }
Chris Mason39279cc2007-06-12 06:35:45 -04005481out:
Chris Mason39279cc2007-06-12 06:35:45 -04005482 btrfs_free_path(path);
5483 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005484out_err:
5485 location->objectid = 0;
5486 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005487}
5488
5489/*
5490 * when we hit a tree root in a directory, the btrfs part of the inode
5491 * needs to be changed to reflect the root directory of the tree root. This
5492 * is kind of like crossing a mount point.
5493 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005494static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005495 struct inode *dir,
5496 struct dentry *dentry,
5497 struct btrfs_key *location,
5498 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005499{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005500 struct btrfs_path *path;
5501 struct btrfs_root *new_root;
5502 struct btrfs_root_ref *ref;
5503 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005504 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005505 int ret;
5506 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005507
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005508 path = btrfs_alloc_path();
5509 if (!path) {
5510 err = -ENOMEM;
5511 goto out;
5512 }
Chris Mason39279cc2007-06-12 06:35:45 -04005513
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005515 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5516 key.type = BTRFS_ROOT_REF_KEY;
5517 key.offset = location->objectid;
5518
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005519 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005520 if (ret) {
5521 if (ret < 0)
5522 err = ret;
5523 goto out;
5524 }
Chris Mason39279cc2007-06-12 06:35:45 -04005525
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005526 leaf = path->nodes[0];
5527 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005528 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005529 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5530 goto out;
5531
5532 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5533 (unsigned long)(ref + 1),
5534 dentry->d_name.len);
5535 if (ret)
5536 goto out;
5537
David Sterbab3b4aa72011-04-21 01:20:15 +02005538 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005539
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005540 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005541 if (IS_ERR(new_root)) {
5542 err = PTR_ERR(new_root);
5543 goto out;
5544 }
5545
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005546 *sub_root = new_root;
5547 location->objectid = btrfs_root_dirid(&new_root->root_item);
5548 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005549 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005550 err = 0;
5551out:
5552 btrfs_free_path(path);
5553 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005554}
5555
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005556static void inode_tree_add(struct inode *inode)
5557{
5558 struct btrfs_root *root = BTRFS_I(inode)->root;
5559 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005560 struct rb_node **p;
5561 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005562 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005563 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005564
Al Viro1d3382cb2010-10-23 15:19:20 -04005565 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005566 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005567 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005568 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005569 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005570 while (*p) {
5571 parent = *p;
5572 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5573
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005574 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005575 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005576 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005577 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005578 else {
5579 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005580 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005581 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005582 RB_CLEAR_NODE(parent);
5583 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005584 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005585 }
5586 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005587 rb_link_node(new, parent, p);
5588 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005589 spin_unlock(&root->inode_lock);
5590}
5591
5592static void inode_tree_del(struct inode *inode)
5593{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005594 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005595 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005596 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005597
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005598 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005599 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005600 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005602 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005603 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005604 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005605
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005606 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005607 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005608 spin_lock(&root->inode_lock);
5609 empty = RB_EMPTY_ROOT(&root->inode_tree);
5610 spin_unlock(&root->inode_lock);
5611 if (empty)
5612 btrfs_add_dead_root(root);
5613 }
5614}
5615
Jeff Mahoney143bede2012-03-01 14:56:26 +01005616void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005617{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005618 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005619 struct rb_node *node;
5620 struct rb_node *prev;
5621 struct btrfs_inode *entry;
5622 struct inode *inode;
5623 u64 objectid = 0;
5624
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005625 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005626 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005627
5628 spin_lock(&root->inode_lock);
5629again:
5630 node = root->inode_tree.rb_node;
5631 prev = NULL;
5632 while (node) {
5633 prev = node;
5634 entry = rb_entry(node, struct btrfs_inode, rb_node);
5635
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005636 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005637 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005638 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005639 node = node->rb_right;
5640 else
5641 break;
5642 }
5643 if (!node) {
5644 while (prev) {
5645 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005646 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005647 node = prev;
5648 break;
5649 }
5650 prev = rb_next(prev);
5651 }
5652 }
5653 while (node) {
5654 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005655 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005656 inode = igrab(&entry->vfs_inode);
5657 if (inode) {
5658 spin_unlock(&root->inode_lock);
5659 if (atomic_read(&inode->i_count) > 1)
5660 d_prune_aliases(inode);
5661 /*
Al Viro45321ac2010-06-07 13:43:19 -04005662 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005663 * the inode cache when its usage count
5664 * hits zero.
5665 */
5666 iput(inode);
5667 cond_resched();
5668 spin_lock(&root->inode_lock);
5669 goto again;
5670 }
5671
5672 if (cond_resched_lock(&root->inode_lock))
5673 goto again;
5674
5675 node = rb_next(node);
5676 }
5677 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005678}
5679
Chris Masone02119d2008-09-05 16:13:11 -04005680static int btrfs_init_locked_inode(struct inode *inode, void *p)
5681{
5682 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005683 inode->i_ino = args->location->objectid;
5684 memcpy(&BTRFS_I(inode)->location, args->location,
5685 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005686 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005687 return 0;
5688}
5689
5690static int btrfs_find_actor(struct inode *inode, void *opaque)
5691{
5692 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005693 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005694 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005695}
5696
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005697static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005698 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005699 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005700{
5701 struct inode *inode;
5702 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005703 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005704
Chris Mason90d3e592014-01-09 17:28:00 -08005705 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005706 args.root = root;
5707
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005708 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005709 btrfs_init_locked_inode,
5710 (void *)&args);
5711 return inode;
5712}
5713
Balaji Rao1a54ef82008-07-21 02:01:04 +05305714/* Get an inode object given its location and corresponding root.
5715 * Returns in *is_new if the inode was read from disk
5716 */
5717struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005718 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305719{
5720 struct inode *inode;
5721
Chris Mason90d3e592014-01-09 17:28:00 -08005722 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305723 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005724 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305725
5726 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005727 int ret;
5728
5729 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005730 if (!is_bad_inode(inode)) {
5731 inode_tree_add(inode);
5732 unlock_new_inode(inode);
5733 if (new)
5734 *new = 1;
5735 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005736 unlock_new_inode(inode);
5737 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005738 ASSERT(ret < 0);
5739 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005740 }
5741 }
5742
Balaji Rao1a54ef82008-07-21 02:01:04 +05305743 return inode;
5744}
5745
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005746static struct inode *new_simple_dir(struct super_block *s,
5747 struct btrfs_key *key,
5748 struct btrfs_root *root)
5749{
5750 struct inode *inode = new_inode(s);
5751
5752 if (!inode)
5753 return ERR_PTR(-ENOMEM);
5754
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005755 BTRFS_I(inode)->root = root;
5756 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005757 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005758
5759 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005760 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005761 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005762 inode->i_fop = &simple_dir_operations;
5763 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005764 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305765 inode->i_atime = inode->i_mtime;
5766 inode->i_ctime = inode->i_mtime;
5767 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005768
5769 return inode;
5770}
5771
Chris Mason3de45862008-11-17 21:02:50 -05005772struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005773{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005774 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005775 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005776 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005777 struct btrfs_root *sub_root = root;
5778 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005779 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005780 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005781
5782 if (dentry->d_name.len > BTRFS_NAME_LEN)
5783 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005784
Jeff Layton39e3c952012-11-28 11:30:53 -05005785 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005786 if (ret < 0)
5787 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005788
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005789 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005790 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005791
5792 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005793 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005794 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005795 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005796
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005797 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005798 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005799 &location, &sub_root);
5800 if (ret < 0) {
5801 if (ret != -ENOENT)
5802 inode = ERR_PTR(ret);
5803 else
5804 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5805 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005806 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005807 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005808 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005809
Julia Lawall34d19ba2011-01-24 19:55:19 +00005810 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005811 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005812 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005813 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005814 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005815 if (ret) {
5816 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005817 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005818 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005819 }
5820
Chris Mason3de45862008-11-17 21:02:50 -05005821 return inode;
5822}
5823
Nick Pigginfe15ce42011-01-07 17:49:23 +11005824static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005825{
5826 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005827 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005828
Li Zefan848cce02012-02-21 17:04:28 +08005829 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005830 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005831
Li Zefan848cce02012-02-21 17:04:28 +08005832 if (inode) {
5833 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005834 if (btrfs_root_refs(&root->root_item) == 0)
5835 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005836
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005837 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005838 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005839 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005840 return 0;
5841}
5842
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005843static void btrfs_dentry_release(struct dentry *dentry)
5844{
Daeseok Youn944a4512014-04-14 15:37:02 +09005845 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005846}
5847
Chris Mason3de45862008-11-17 21:02:50 -05005848static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005849 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005850{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005851 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005852
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005853 inode = btrfs_lookup_dentry(dir, dentry);
5854 if (IS_ERR(inode)) {
5855 if (PTR_ERR(inode) == -ENOENT)
5856 inode = NULL;
5857 else
5858 return ERR_CAST(inode);
5859 }
5860
Al Viro41d28bc2014-10-12 22:24:21 -04005861 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005862}
5863
Miao Xie16cdcec2011-04-22 18:12:22 +08005864unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005865 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5866};
5867
Josef Bacik23b5ec72017-07-24 15:14:25 -04005868/*
5869 * All this infrastructure exists because dir_emit can fault, and we are holding
5870 * the tree lock when doing readdir. For now just allocate a buffer and copy
5871 * our information into that, and then dir_emit from the buffer. This is
5872 * similar to what NFS does, only we don't keep the buffer around in pagecache
5873 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5874 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5875 * tree lock.
5876 */
5877static int btrfs_opendir(struct inode *inode, struct file *file)
5878{
5879 struct btrfs_file_private *private;
5880
5881 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5882 if (!private)
5883 return -ENOMEM;
5884 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5885 if (!private->filldir_buf) {
5886 kfree(private);
5887 return -ENOMEM;
5888 }
5889 file->private_data = private;
5890 return 0;
5891}
5892
5893struct dir_entry {
5894 u64 ino;
5895 u64 offset;
5896 unsigned type;
5897 int name_len;
5898};
5899
5900static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5901{
5902 while (entries--) {
5903 struct dir_entry *entry = addr;
5904 char *name = (char *)(entry + 1);
5905
5906 ctx->pos = entry->offset;
5907 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5908 entry->type))
5909 return 1;
5910 addr += sizeof(struct dir_entry) + entry->name_len;
5911 ctx->pos++;
5912 }
5913 return 0;
5914}
5915
Al Viro9cdda8d2013-05-22 16:48:09 -04005916static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005917{
Al Viro9cdda8d2013-05-22 16:48:09 -04005918 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005919 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005920 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005921 struct btrfs_dir_item *di;
5922 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005923 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005924 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005925 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005926 struct list_head ins_list;
5927 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005928 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005929 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005930 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005931 char *name_ptr;
5932 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005933 int entries = 0;
5934 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005935 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005936 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005937
Al Viro9cdda8d2013-05-22 16:48:09 -04005938 if (!dir_emit_dots(file, ctx))
5939 return 0;
5940
David Woodhouse49593bf2008-08-17 17:08:36 +01005941 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005942 if (!path)
5943 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005944
Josef Bacik23b5ec72017-07-24 15:14:25 -04005945 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005946 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005947
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005948 INIT_LIST_HEAD(&ins_list);
5949 INIT_LIST_HEAD(&del_list);
5950 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005951
Josef Bacik23b5ec72017-07-24 15:14:25 -04005952again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005953 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005954 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005955 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005956
Chris Mason39279cc2007-06-12 06:35:45 -04005957 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5958 if (ret < 0)
5959 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005960
5961 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005962 struct dir_entry *entry;
5963
Chris Mason5f39d392007-10-15 16:14:19 -04005964 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005965 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005966 if (slot >= btrfs_header_nritems(leaf)) {
5967 ret = btrfs_next_leaf(root, path);
5968 if (ret < 0)
5969 goto err;
5970 else if (ret > 0)
5971 break;
5972 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005973 }
Chris Mason3de45862008-11-17 21:02:50 -05005974
Chris Mason5f39d392007-10-15 16:14:19 -04005975 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5976
5977 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005978 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005979 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005980 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005981 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005982 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005983 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005984 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005985 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005986 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005987 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5988 PAGE_SIZE) {
5989 btrfs_release_path(path);
5990 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5991 if (ret)
5992 goto nopos;
5993 addr = private->filldir_buf;
5994 entries = 0;
5995 total_len = 0;
5996 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005997 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005998
5999 entry = addr;
6000 entry->name_len = name_len;
6001 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006002 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6003 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006004 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006005 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006006 entry->ino = location.objectid;
6007 entry->offset = found_key.offset;
6008 entries++;
6009 addr += sizeof(struct dir_entry) + name_len;
6010 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006011next:
6012 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006013 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006014 btrfs_release_path(path);
6015
6016 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6017 if (ret)
6018 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006019
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006020 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006021 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006022 goto nopos;
6023
Zach Browndb62efb2013-07-11 16:19:42 -07006024 /*
6025 * Stop new entries from being returned after we return the last
6026 * entry.
6027 *
6028 * New directory entries are assigned a strictly increasing
6029 * offset. This means that new entries created during readdir
6030 * are *guaranteed* to be seen in the future by that readdir.
6031 * This has broken buggy programs which operate on names as
6032 * they're returned by readdir. Until we re-use freed offsets
6033 * we have this hack to stop new entries from being returned
6034 * under the assumption that they'll never reach this huge
6035 * offset.
6036 *
6037 * This is being careful not to overflow 32bit loff_t unless the
6038 * last entry requires it because doing so has broken 32bit apps
6039 * in the past.
6040 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006041 if (ctx->pos >= INT_MAX)
6042 ctx->pos = LLONG_MAX;
6043 else
6044 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006045nopos:
6046 ret = 0;
6047err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006048 if (put)
6049 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006050 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006051 return ret;
6052}
6053
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006054int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006055{
6056 struct btrfs_root *root = BTRFS_I(inode)->root;
6057 struct btrfs_trans_handle *trans;
6058 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006059 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006060
Josef Bacik72ac3c02012-05-23 14:13:11 -04006061 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006062 return 0;
6063
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006064 if (btrfs_fs_closing(root->fs_info) &&
6065 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006066 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006067
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006068 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006069 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006070 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006071 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006072 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006073 if (IS_ERR(trans))
6074 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006075 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006076 }
6077 return ret;
6078}
6079
6080/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006081 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006082 * inode changes. But, it is most likely to find the inode in cache.
6083 * FIXME, needs more benchmarking...there are no reasons other than performance
6084 * to keep or drop this code.
6085 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006086static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006087{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006088 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006089 struct btrfs_root *root = BTRFS_I(inode)->root;
6090 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006091 int ret;
6092
Josef Bacik72ac3c02012-05-23 14:13:11 -04006093 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006094 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006095
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006096 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006097 if (IS_ERR(trans))
6098 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006099
6100 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006101 if (ret && ret == -ENOSPC) {
6102 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006103 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006104 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006105 if (IS_ERR(trans))
6106 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006107
Chris Mason94b60442010-05-26 11:02:00 -04006108 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006109 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006110 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006111 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006112 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006113
6114 return ret;
6115}
6116
6117/*
6118 * This is a copy of file_update_time. We need this so we can return error on
6119 * ENOSPC for updating the inode in the case of file write and mmap writes.
6120 */
Josef Bacike41f9412012-03-26 09:46:47 -04006121static int btrfs_update_time(struct inode *inode, struct timespec *now,
6122 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006123{
Alexander Block2bc5565282012-06-15 09:49:33 +02006124 struct btrfs_root *root = BTRFS_I(inode)->root;
6125
6126 if (btrfs_root_readonly(root))
6127 return -EROFS;
6128
Josef Bacike41f9412012-03-26 09:46:47 -04006129 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006130 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006131 if (flags & S_CTIME)
6132 inode->i_ctime = *now;
6133 if (flags & S_MTIME)
6134 inode->i_mtime = *now;
6135 if (flags & S_ATIME)
6136 inode->i_atime = *now;
6137 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006138}
6139
Chris Masond352ac62008-09-29 15:18:18 -04006140/*
6141 * find the highest existing sequence number in a directory
6142 * and then set the in-memory index_cnt variable to reflect
6143 * free sequence numbers
6144 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006145static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006146{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006147 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006148 struct btrfs_key key, found_key;
6149 struct btrfs_path *path;
6150 struct extent_buffer *leaf;
6151 int ret;
6152
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006153 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006154 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006155 key.offset = (u64)-1;
6156
6157 path = btrfs_alloc_path();
6158 if (!path)
6159 return -ENOMEM;
6160
6161 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6162 if (ret < 0)
6163 goto out;
6164 /* FIXME: we should be able to handle this */
6165 if (ret == 0)
6166 goto out;
6167 ret = 0;
6168
6169 /*
6170 * MAGIC NUMBER EXPLANATION:
6171 * since we search a directory based on f_pos we have to start at 2
6172 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6173 * else has to start at 2
6174 */
6175 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006176 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006177 goto out;
6178 }
6179
6180 path->slots[0]--;
6181
6182 leaf = path->nodes[0];
6183 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6184
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006185 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006186 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006187 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006188 goto out;
6189 }
6190
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006191 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006192out:
6193 btrfs_free_path(path);
6194 return ret;
6195}
6196
Chris Masond352ac62008-09-29 15:18:18 -04006197/*
6198 * helper to find a free sequence number in a given directory. This current
6199 * code is very simple, later versions will do smarter things in the btree
6200 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006201int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006202{
6203 int ret = 0;
6204
Nikolay Borisov877574e2017-02-20 13:50:33 +02006205 if (dir->index_cnt == (u64)-1) {
6206 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006207 if (ret) {
6208 ret = btrfs_set_inode_index_count(dir);
6209 if (ret)
6210 return ret;
6211 }
Josef Bacikaec74772008-07-24 12:12:38 -04006212 }
6213
Nikolay Borisov877574e2017-02-20 13:50:33 +02006214 *index = dir->index_cnt;
6215 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006216
6217 return ret;
6218}
6219
Chris Masonb0d5d102014-09-08 13:08:51 -07006220static int btrfs_insert_inode_locked(struct inode *inode)
6221{
6222 struct btrfs_iget_args args;
6223 args.location = &BTRFS_I(inode)->location;
6224 args.root = BTRFS_I(inode)->root;
6225
6226 return insert_inode_locked4(inode,
6227 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6228 btrfs_find_actor, &args);
6229}
6230
Anand Jain19aee8d2017-07-18 17:37:05 +08006231/*
6232 * Inherit flags from the parent inode.
6233 *
6234 * Currently only the compression flags and the cow flags are inherited.
6235 */
6236static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6237{
6238 unsigned int flags;
6239
6240 if (!dir)
6241 return;
6242
6243 flags = BTRFS_I(dir)->flags;
6244
6245 if (flags & BTRFS_INODE_NOCOMPRESS) {
6246 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6247 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6248 } else if (flags & BTRFS_INODE_COMPRESS) {
6249 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6250 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6251 }
6252
6253 if (flags & BTRFS_INODE_NODATACOW) {
6254 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6255 if (S_ISREG(inode->i_mode))
6256 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6257 }
6258
6259 btrfs_update_iflags(inode);
6260}
6261
Chris Mason39279cc2007-06-12 06:35:45 -04006262static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6263 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006264 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006265 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006266 u64 ref_objectid, u64 objectid,
6267 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006268{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006269 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006270 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006271 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006272 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006273 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006274 struct btrfs_inode_ref *ref;
6275 struct btrfs_key key[2];
6276 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006277 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006278 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006279 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006280
Chris Mason5f39d392007-10-15 16:14:19 -04006281 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006282 if (!path)
6283 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006284
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006285 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006286 if (!inode) {
6287 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006288 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006289 }
Chris Mason39279cc2007-06-12 06:35:45 -04006290
Li Zefan581bb052011-04-20 10:06:11 +08006291 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006292 * O_TMPFILE, set link count to 0, so that after this point,
6293 * we fill in an inode item with the correct link count.
6294 */
6295 if (!name)
6296 set_nlink(inode, 0);
6297
6298 /*
Li Zefan581bb052011-04-20 10:06:11 +08006299 * we have to initialize this early, so we can reclaim the inode
6300 * number if we fail afterwards in this function.
6301 */
6302 inode->i_ino = objectid;
6303
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006304 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006305 trace_btrfs_inode_request(dir);
6306
Nikolay Borisov877574e2017-02-20 13:50:33 +02006307 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006308 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006309 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006310 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006311 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006312 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006313 } else if (dir) {
6314 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006315 }
6316 /*
6317 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006318 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006319 * number
6320 */
6321 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006322 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006323 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006324 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006325 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006326
Josef Bacik5dc562c2012-08-17 13:14:17 -04006327 /*
6328 * We could have gotten an inode number from somebody who was fsynced
6329 * and then removed in this same transaction, so let's just set full
6330 * sync since it will be a full sync anyway and this will blow away the
6331 * old info in the log.
6332 */
6333 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6334
Chris Mason9c583092008-01-29 15:15:18 -05006335 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006336 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006337 key[0].offset = 0;
6338
Chris Mason9c583092008-01-29 15:15:18 -05006339 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006340
6341 if (name) {
6342 /*
6343 * Start new inodes with an inode_ref. This is slightly more
6344 * efficient for small numbers of hard links since they will
6345 * be packed into one item. Extended refs will kick in if we
6346 * add more hard links than can fit in the ref item.
6347 */
6348 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006349 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006350 key[1].offset = ref_objectid;
6351
6352 sizes[1] = name_len + sizeof(*ref);
6353 }
Chris Mason9c583092008-01-29 15:15:18 -05006354
Chris Masonb0d5d102014-09-08 13:08:51 -07006355 location = &BTRFS_I(inode)->location;
6356 location->objectid = objectid;
6357 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006358 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006359
6360 ret = btrfs_insert_inode_locked(inode);
6361 if (ret < 0)
6362 goto fail;
6363
Chris Masonb9473432009-03-13 11:00:37 -04006364 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006365 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006366 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006367 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006368
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006369 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006370 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306371
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006372 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306373 inode->i_atime = inode->i_mtime;
6374 inode->i_ctime = inode->i_mtime;
6375 BTRFS_I(inode)->i_otime = inode->i_mtime;
6376
Chris Mason5f39d392007-10-15 16:14:19 -04006377 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6378 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006379 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006380 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006381 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006382
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006383 if (name) {
6384 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6385 struct btrfs_inode_ref);
6386 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6387 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6388 ptr = (unsigned long)(ref + 1);
6389 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6390 }
Chris Mason9c583092008-01-29 15:15:18 -05006391
Chris Mason5f39d392007-10-15 16:14:19 -04006392 btrfs_mark_buffer_dirty(path->nodes[0]);
6393 btrfs_free_path(path);
6394
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006395 btrfs_inherit_iflags(inode, dir);
6396
Al Viro569254b2011-07-24 17:08:40 -04006397 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006398 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006399 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006400 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006401 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6402 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006403 }
6404
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006405 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006406
6407 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006408 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006409
Alexander Block8ea05e32012-07-25 17:35:53 +02006410 btrfs_update_root_times(trans, root);
6411
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006412 ret = btrfs_inode_inherit_props(trans, inode, dir);
6413 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006414 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006415 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006416 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006417
Chris Mason39279cc2007-06-12 06:35:45 -04006418 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006419
6420fail_unlock:
6421 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006422fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006423 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006424 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006425 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006426 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006427 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006428}
6429
6430static inline u8 btrfs_inode_type(struct inode *inode)
6431{
6432 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6433}
6434
Chris Masond352ac62008-09-29 15:18:18 -04006435/*
6436 * utility function to add 'inode' into 'parent_inode' with
6437 * a give name and a given sequence number.
6438 * if 'add_backref' is true, also insert a backref from the
6439 * inode to the parent directory.
6440 */
Chris Masone02119d2008-09-05 16:13:11 -04006441int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006442 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006443 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006444{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006445 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006446 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006447 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006448 struct btrfs_root *root = parent_inode->root;
6449 u64 ino = btrfs_ino(inode);
6450 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006451
Li Zefan33345d012011-04-20 10:31:50 +08006452 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006453 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006454 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006455 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006456 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006457 key.offset = 0;
6458 }
Chris Mason39279cc2007-06-12 06:35:45 -04006459
Li Zefan33345d012011-04-20 10:31:50 +08006460 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006461 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6462 root->root_key.objectid, parent_ino,
6463 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006464 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006465 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6466 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006467 }
6468
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006469 /* Nothing to clean up yet */
6470 if (ret)
6471 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006472
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006473 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6474 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006475 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006476 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006477 goto fail_dir_item;
6478 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006479 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006480 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006481 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006482
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006483 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006484 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006485 inode_inc_iversion(&parent_inode->vfs_inode);
6486 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6487 current_time(&parent_inode->vfs_inode);
6488 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006489 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006490 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006491 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006492
6493fail_dir_item:
6494 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6495 u64 local_index;
6496 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006497 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6498 root->root_key.objectid, parent_ino,
6499 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006500
6501 } else if (add_backref) {
6502 u64 local_index;
6503 int err;
6504
6505 err = btrfs_del_inode_ref(trans, root, name, name_len,
6506 ino, parent_ino, &local_index);
6507 }
6508 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006509}
6510
6511static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006512 struct btrfs_inode *dir, struct dentry *dentry,
6513 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006514{
Josef Bacika1b075d2010-11-19 20:36:11 +00006515 int err = btrfs_add_link(trans, dir, inode,
6516 dentry->d_name.name, dentry->d_name.len,
6517 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006518 if (err > 0)
6519 err = -EEXIST;
6520 return err;
6521}
6522
Josef Bacik618e21d2007-07-11 10:18:17 -04006523static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006524 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006525{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006526 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006527 struct btrfs_trans_handle *trans;
6528 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006529 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006530 int err;
6531 int drop_inode = 0;
6532 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006533 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006534
Josef Bacik9ed74f22009-09-11 16:12:44 -04006535 /*
6536 * 2 for inode item and ref
6537 * 2 for dir items
6538 * 1 for xattr if selinux is on
6539 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006540 trans = btrfs_start_transaction(root, 5);
6541 if (IS_ERR(trans))
6542 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006543
Li Zefan581bb052011-04-20 10:06:11 +08006544 err = btrfs_find_free_ino(root, &objectid);
6545 if (err)
6546 goto out_unlock;
6547
Josef Bacikaec74772008-07-24 12:12:38 -04006548 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006549 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6550 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006551 if (IS_ERR(inode)) {
6552 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006553 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006554 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006555
Casey Schauflerad19db72011-12-15 10:09:07 -05006556 /*
6557 * If the active LSM wants to access the inode during
6558 * d_instantiate it needs these. Smack checks to see
6559 * if the filesystem supports xattrs by looking at the
6560 * ops vector.
6561 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006562 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006563 init_special_inode(inode, inode->i_mode, rdev);
6564
6565 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006566 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006567 goto out_unlock_inode;
6568
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006569 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6570 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006571 if (err) {
6572 goto out_unlock_inode;
6573 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006574 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006575 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006576 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006577 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006578
Josef Bacik618e21d2007-07-11 10:18:17 -04006579out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006580 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006581 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006582 if (drop_inode) {
6583 inode_dec_link_count(inode);
6584 iput(inode);
6585 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006586 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006587
6588out_unlock_inode:
6589 drop_inode = 1;
6590 unlock_new_inode(inode);
6591 goto out_unlock;
6592
Josef Bacik618e21d2007-07-11 10:18:17 -04006593}
6594
Chris Mason39279cc2007-06-12 06:35:45 -04006595static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006596 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006597{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006598 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006599 struct btrfs_trans_handle *trans;
6600 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006601 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006602 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006603 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006604 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006605 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006606
Josef Bacik9ed74f22009-09-11 16:12:44 -04006607 /*
6608 * 2 for inode item and ref
6609 * 2 for dir items
6610 * 1 for xattr if selinux is on
6611 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006612 trans = btrfs_start_transaction(root, 5);
6613 if (IS_ERR(trans))
6614 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006615
Li Zefan581bb052011-04-20 10:06:11 +08006616 err = btrfs_find_free_ino(root, &objectid);
6617 if (err)
6618 goto out_unlock;
6619
Josef Bacikaec74772008-07-24 12:12:38 -04006620 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006621 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6622 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006623 if (IS_ERR(inode)) {
6624 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006625 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006626 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006627 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006628 /*
6629 * If the active LSM wants to access the inode during
6630 * d_instantiate it needs these. Smack checks to see
6631 * if the filesystem supports xattrs by looking at the
6632 * ops vector.
6633 */
6634 inode->i_fop = &btrfs_file_operations;
6635 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006636 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006637
6638 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6639 if (err)
6640 goto out_unlock_inode;
6641
6642 err = btrfs_update_inode(trans, root, inode);
6643 if (err)
6644 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006645
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006646 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6647 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006648 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006649 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006650
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006651 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006652 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006653 d_instantiate(dentry, inode);
6654
Chris Mason39279cc2007-06-12 06:35:45 -04006655out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006656 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006657 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006658 inode_dec_link_count(inode);
6659 iput(inode);
6660 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006661 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006662 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006663
6664out_unlock_inode:
6665 unlock_new_inode(inode);
6666 goto out_unlock;
6667
Chris Mason39279cc2007-06-12 06:35:45 -04006668}
6669
6670static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6671 struct dentry *dentry)
6672{
Filipe Manana271dba452016-01-05 16:24:05 +00006673 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006674 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006675 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006676 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006677 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006678 int err;
6679 int drop_inode = 0;
6680
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006681 /* do not allow sys_link's with other subvols of the same device */
6682 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006683 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006684
Mark Fashehf1863732012-08-08 11:32:27 -07006685 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006686 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006687
Nikolay Borisov877574e2017-02-20 13:50:33 +02006688 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006689 if (err)
6690 goto fail;
6691
Yan, Zhenga22285a2010-05-16 10:48:46 -04006692 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006693 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006694 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006695 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006696 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006697 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006698 if (IS_ERR(trans)) {
6699 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006700 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006701 goto fail;
6702 }
Chris Mason5f39d392007-10-15 16:14:19 -04006703
Miao Xie67de1172013-12-26 13:07:06 +08006704 /* There are several dir indexes for this inode, clear the cache. */
6705 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006706 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006707 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006708 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006709 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006710 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006711
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006712 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6713 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006714
Yan, Zhenga5719522009-09-24 09:17:31 -04006715 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006716 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006717 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006718 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006719 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006720 if (err)
6721 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006722 if (inode->i_nlink == 1) {
6723 /*
6724 * If new hard link count is 1, it's a file created
6725 * with open(2) O_TMPFILE flag.
6726 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006727 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006728 if (err)
6729 goto fail;
6730 }
Al Viro08c422c2011-12-23 07:58:13 -05006731 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006732 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006733 }
Chris Mason39279cc2007-06-12 06:35:45 -04006734
Chris Mason1832a6d2007-12-21 16:27:21 -05006735fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006736 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006737 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006738 if (drop_inode) {
6739 inode_dec_link_count(inode);
6740 iput(inode);
6741 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006742 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006743 return err;
6744}
6745
Al Viro18bb1db2011-07-26 01:41:39 -04006746static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006747{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006748 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006749 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006750 struct btrfs_trans_handle *trans;
6751 struct btrfs_root *root = BTRFS_I(dir)->root;
6752 int err = 0;
6753 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006754 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006755 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006756
Josef Bacik9ed74f22009-09-11 16:12:44 -04006757 /*
6758 * 2 items for inode and ref
6759 * 2 items for dir items
6760 * 1 for xattr if selinux is on
6761 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006762 trans = btrfs_start_transaction(root, 5);
6763 if (IS_ERR(trans))
6764 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006765
Li Zefan581bb052011-04-20 10:06:11 +08006766 err = btrfs_find_free_ino(root, &objectid);
6767 if (err)
6768 goto out_fail;
6769
Josef Bacikaec74772008-07-24 12:12:38 -04006770 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006771 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6772 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006773 if (IS_ERR(inode)) {
6774 err = PTR_ERR(inode);
6775 goto out_fail;
6776 }
Chris Mason5f39d392007-10-15 16:14:19 -04006777
Chris Mason39279cc2007-06-12 06:35:45 -04006778 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006779 /* these must be set before we unlock the inode */
6780 inode->i_op = &btrfs_dir_inode_operations;
6781 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006782
Eric Paris2a7dba32011-02-01 11:05:39 -05006783 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006784 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006785 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006786
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006787 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006788 err = btrfs_update_inode(trans, root, inode);
6789 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006790 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006791
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006792 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6793 dentry->d_name.name,
6794 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006795 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006796 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006797
Chris Mason39279cc2007-06-12 06:35:45 -04006798 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006799 /*
6800 * mkdir is special. We're unlocking after we call d_instantiate
6801 * to avoid a race with nfsd calling d_instantiate.
6802 */
6803 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006804 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006805
6806out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006807 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006808 if (drop_on_err) {
6809 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006810 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006811 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006812 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006813 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006814
6815out_fail_inode:
6816 unlock_new_inode(inode);
6817 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006818}
6819
Chris Masonc8b97812008-10-29 14:49:59 -04006820static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006821 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006822 size_t pg_offset, u64 extent_offset,
6823 struct btrfs_file_extent_item *item)
6824{
6825 int ret;
6826 struct extent_buffer *leaf = path->nodes[0];
6827 char *tmp;
6828 size_t max_size;
6829 unsigned long inline_size;
6830 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006831 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006832
6833 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006834 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006835 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6836 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006837 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006838 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006839 if (!tmp)
6840 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006841 ptr = btrfs_file_extent_inline_start(item);
6842
6843 read_extent_buffer(leaf, tmp, ptr, inline_size);
6844
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006845 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006846 ret = btrfs_decompress(compress_type, tmp, page,
6847 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006848
6849 /*
6850 * decompression code contains a memset to fill in any space between the end
6851 * of the uncompressed data and the end of max_size in case the decompressed
6852 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6853 * the end of an inline extent and the beginning of the next block, so we
6854 * cover that region here.
6855 */
6856
6857 if (max_size + pg_offset < PAGE_SIZE) {
6858 char *map = kmap(page);
6859 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6860 kunmap(page);
6861 }
Chris Masonc8b97812008-10-29 14:49:59 -04006862 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006863 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006864}
6865
Chris Masond352ac62008-09-29 15:18:18 -04006866/*
6867 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006868 * the ugly parts come from merging extents from the disk with the in-ram
6869 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006870 * where the in-ram extents might be locked pending data=ordered completion.
6871 *
6872 * This also copies inline extents directly into the page.
6873 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006874struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6875 struct page *page,
6876 size_t pg_offset, u64 start, u64 len,
6877 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006878{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006879 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006880 int ret;
6881 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006882 u64 extent_start = 0;
6883 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006884 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006885 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006886 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006887 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006888 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006889 struct extent_buffer *leaf;
6890 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006891 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006892 struct extent_map_tree *em_tree = &inode->extent_tree;
6893 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006894 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006895
Chris Mason890871b2009-09-02 16:24:52 -04006896 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006897 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006898 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006899 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006900 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006901
Chris Masona52d9a82007-08-27 16:49:44 -04006902 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006903 if (em->start > start || em->start + em->len <= start)
6904 free_extent_map(em);
6905 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006906 free_extent_map(em);
6907 else
6908 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006909 }
David Sterba172ddd62011-04-21 00:48:27 +02006910 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006911 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006912 err = -ENOMEM;
6913 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006914 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006915 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006916 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006917 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006918 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006919 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006920
6921 if (!path) {
6922 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006923 if (!path) {
6924 err = -ENOMEM;
6925 goto out;
6926 }
6927 /*
6928 * Chances are we'll be called again, so go ahead and do
6929 * readahead
6930 */
David Sterbae4058b52015-11-27 16:31:35 +01006931 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006932 }
6933
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006934 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006935 if (ret < 0) {
6936 err = ret;
6937 goto out;
6938 }
6939
6940 if (ret != 0) {
6941 if (path->slots[0] == 0)
6942 goto not_found;
6943 path->slots[0]--;
6944 }
6945
Chris Mason5f39d392007-10-15 16:14:19 -04006946 leaf = path->nodes[0];
6947 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006948 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006949 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006950 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006951 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006952 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006953 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006954 /*
6955 * If we backup past the first extent we want to move forward
6956 * and see if there is an extent in front of us, otherwise we'll
6957 * say there is a hole for our whole search range which can
6958 * cause problems.
6959 */
6960 extent_end = start;
6961 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006962 }
6963
Chris Mason5f39d392007-10-15 16:14:19 -04006964 found_type = btrfs_file_extent_type(leaf, item);
6965 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006966 if (found_type == BTRFS_FILE_EXTENT_REG ||
6967 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006968 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006969 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006970
6971 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6972 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006973 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6974 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006975 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006976 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006977 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006978
6979 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6980 path->slots[0],
6981 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006982 }
Josef Bacik25a50342013-10-14 12:08:38 -04006983next:
Yan Zheng9036c102008-10-30 14:19:41 -04006984 if (start >= extent_end) {
6985 path->slots[0]++;
6986 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6987 ret = btrfs_next_leaf(root, path);
6988 if (ret < 0) {
6989 err = ret;
6990 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006991 }
Yan Zheng9036c102008-10-30 14:19:41 -04006992 if (ret > 0)
6993 goto not_found;
6994 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006995 }
Yan Zheng9036c102008-10-30 14:19:41 -04006996 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6997 if (found_key.objectid != objectid ||
6998 found_key.type != BTRFS_EXTENT_DATA_KEY)
6999 goto not_found;
7000 if (start + len <= found_key.offset)
7001 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007002 if (start > found_key.offset)
7003 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007004 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007005 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007006 em->len = found_key.offset - start;
7007 goto not_found_em;
7008 }
7009
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007010 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007011 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007012
Yan Zhengd899e052008-10-30 14:25:28 -04007013 if (found_type == BTRFS_FILE_EXTENT_REG ||
7014 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007015 goto insert;
7016 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007017 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007018 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007019 size_t size;
7020 size_t extent_offset;
7021 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007022
Filipe Manana7ffbb592014-06-09 03:48:05 +01007023 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007024 goto out;
Yan689f9342007-10-29 11:41:07 -04007025
Chris Mason514ac8a2014-01-03 21:07:00 -08007026 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007027 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007028 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7029 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007030 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007031 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007032 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007033 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007034 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007035 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007036 if (btrfs_file_extent_compression(leaf, item) !=
7037 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007038 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007039 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007040 if (ret) {
7041 err = ret;
7042 goto out;
7043 }
Chris Masonc8b97812008-10-29 14:49:59 -04007044 } else {
7045 map = kmap(page);
7046 read_extent_buffer(leaf, map + pg_offset, ptr,
7047 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007048 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007049 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007050 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007051 copy_size);
7052 }
Chris Masonc8b97812008-10-29 14:49:59 -04007053 kunmap(page);
7054 }
Chris Mason179e29e2007-11-01 11:28:41 -04007055 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007056 }
Chris Masond1310b22008-01-24 16:13:08 -05007057 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007058 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007059 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007060 }
7061not_found:
7062 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007063 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007064 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007065not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007066 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007067insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007068 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007069 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007070 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007071 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7072 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007073 err = -EIO;
7074 goto out;
7075 }
Chris Masond1310b22008-01-24 16:13:08 -05007076
7077 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007078 write_lock(&em_tree->lock);
Liu Bo7b4df0582018-01-05 12:51:10 -07007079 err = btrfs_add_extent_mapping(em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007080 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007081out:
liubo1abe9b82011-03-24 11:18:59 +00007082
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007083 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007084
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007085 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007086 if (err) {
7087 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007088 return ERR_PTR(err);
7089 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007090 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007091 return em;
7092}
7093
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007094struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7095 struct page *page,
7096 size_t pg_offset, u64 start, u64 len,
7097 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007098{
7099 struct extent_map *em;
7100 struct extent_map *hole_em = NULL;
7101 u64 range_start = start;
7102 u64 end;
7103 u64 found;
7104 u64 found_end;
7105 int err = 0;
7106
7107 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7108 if (IS_ERR(em))
7109 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007110 /*
7111 * If our em maps to:
7112 * - a hole or
7113 * - a pre-alloc extent,
7114 * there might actually be delalloc bytes behind it.
7115 */
7116 if (em->block_start != EXTENT_MAP_HOLE &&
7117 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7118 return em;
7119 else
7120 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007121
7122 /* check to see if we've wrapped (len == -1 or similar) */
7123 end = start + len;
7124 if (end < start)
7125 end = (u64)-1;
7126 else
7127 end -= 1;
7128
7129 em = NULL;
7130
7131 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007132 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007133 end, len, EXTENT_DELALLOC, 1);
7134 found_end = range_start + found;
7135 if (found_end < range_start)
7136 found_end = (u64)-1;
7137
7138 /*
7139 * we didn't find anything useful, return
7140 * the original results from get_extent()
7141 */
7142 if (range_start > end || found_end <= start) {
7143 em = hole_em;
7144 hole_em = NULL;
7145 goto out;
7146 }
7147
7148 /* adjust the range_start to make sure it doesn't
7149 * go backwards from the start they passed in
7150 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307151 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007152 found = found_end - range_start;
7153
7154 if (found > 0) {
7155 u64 hole_start = start;
7156 u64 hole_len = len;
7157
David Sterba172ddd62011-04-21 00:48:27 +02007158 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007159 if (!em) {
7160 err = -ENOMEM;
7161 goto out;
7162 }
7163 /*
7164 * when btrfs_get_extent can't find anything it
7165 * returns one huge hole
7166 *
7167 * make sure what it found really fits our range, and
7168 * adjust to make sure it is based on the start from
7169 * the caller
7170 */
7171 if (hole_em) {
7172 u64 calc_end = extent_map_end(hole_em);
7173
7174 if (calc_end <= start || (hole_em->start > end)) {
7175 free_extent_map(hole_em);
7176 hole_em = NULL;
7177 } else {
7178 hole_start = max(hole_em->start, start);
7179 hole_len = calc_end - hole_start;
7180 }
7181 }
7182 em->bdev = NULL;
7183 if (hole_em && range_start > hole_start) {
7184 /* our hole starts before our delalloc, so we
7185 * have to return just the parts of the hole
7186 * that go until the delalloc starts
7187 */
7188 em->len = min(hole_len,
7189 range_start - hole_start);
7190 em->start = hole_start;
7191 em->orig_start = hole_start;
7192 /*
7193 * don't adjust block start at all,
7194 * it is fixed at EXTENT_MAP_HOLE
7195 */
7196 em->block_start = hole_em->block_start;
7197 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007198 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7199 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007200 } else {
7201 em->start = range_start;
7202 em->len = found;
7203 em->orig_start = range_start;
7204 em->block_start = EXTENT_MAP_DELALLOC;
7205 em->block_len = found;
7206 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007207 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007208 return hole_em;
7209 }
7210out:
7211
7212 free_extent_map(hole_em);
7213 if (err) {
7214 free_extent_map(em);
7215 return ERR_PTR(err);
7216 }
7217 return em;
7218}
7219
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007220static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7221 const u64 start,
7222 const u64 len,
7223 const u64 orig_start,
7224 const u64 block_start,
7225 const u64 block_len,
7226 const u64 orig_block_len,
7227 const u64 ram_bytes,
7228 const int type)
7229{
7230 struct extent_map *em = NULL;
7231 int ret;
7232
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007233 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007234 em = create_io_em(inode, start, len, orig_start,
7235 block_start, block_len, orig_block_len,
7236 ram_bytes,
7237 BTRFS_COMPRESS_NONE, /* compress_type */
7238 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007239 if (IS_ERR(em))
7240 goto out;
7241 }
7242 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7243 len, block_len, type);
7244 if (ret) {
7245 if (em) {
7246 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007247 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007248 start + len - 1, 0);
7249 }
7250 em = ERR_PTR(ret);
7251 }
7252 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007253
7254 return em;
7255}
7256
Josef Bacik4b46fce2010-05-23 11:00:55 -04007257static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7258 u64 start, u64 len)
7259{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007260 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007261 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007262 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007263 struct btrfs_key ins;
7264 u64 alloc_hint;
7265 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007266
Josef Bacik4b46fce2010-05-23 11:00:55 -04007267 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007268 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007269 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007270 if (ret)
7271 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007272
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007273 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7274 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007275 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007276 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007277 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007278 btrfs_free_reserved_extent(fs_info, ins.objectid,
7279 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007280
Josef Bacik4b46fce2010-05-23 11:00:55 -04007281 return em;
7282}
7283
Chris Mason46bfbb52010-05-26 11:04:10 -04007284/*
7285 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7286 * block must be cow'd
7287 */
Josef Bacik00361582013-08-14 14:02:47 -04007288noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007289 u64 *orig_start, u64 *orig_block_len,
7290 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007291{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007292 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007293 struct btrfs_path *path;
7294 int ret;
7295 struct extent_buffer *leaf;
7296 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007297 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007298 struct btrfs_file_extent_item *fi;
7299 struct btrfs_key key;
7300 u64 disk_bytenr;
7301 u64 backref_offset;
7302 u64 extent_end;
7303 u64 num_bytes;
7304 int slot;
7305 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007306 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007307
Chris Mason46bfbb52010-05-26 11:04:10 -04007308 path = btrfs_alloc_path();
7309 if (!path)
7310 return -ENOMEM;
7311
David Sterbaf85b7372017-01-20 14:54:07 +01007312 ret = btrfs_lookup_file_extent(NULL, root, path,
7313 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007314 if (ret < 0)
7315 goto out;
7316
7317 slot = path->slots[0];
7318 if (ret == 1) {
7319 if (slot == 0) {
7320 /* can't find the item, must cow */
7321 ret = 0;
7322 goto out;
7323 }
7324 slot--;
7325 }
7326 ret = 0;
7327 leaf = path->nodes[0];
7328 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007329 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007330 key.type != BTRFS_EXTENT_DATA_KEY) {
7331 /* not our file or wrong item type, must cow */
7332 goto out;
7333 }
7334
7335 if (key.offset > offset) {
7336 /* Wrong offset, must cow */
7337 goto out;
7338 }
7339
7340 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7341 found_type = btrfs_file_extent_type(leaf, fi);
7342 if (found_type != BTRFS_FILE_EXTENT_REG &&
7343 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7344 /* not a regular extent, must cow */
7345 goto out;
7346 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007347
7348 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7349 goto out;
7350
Miao Xiee77751a2013-12-27 21:11:50 +08007351 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7352 if (extent_end <= offset)
7353 goto out;
7354
Chris Mason46bfbb52010-05-26 11:04:10 -04007355 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007356 if (disk_bytenr == 0)
7357 goto out;
7358
7359 if (btrfs_file_extent_compression(leaf, fi) ||
7360 btrfs_file_extent_encryption(leaf, fi) ||
7361 btrfs_file_extent_other_encoding(leaf, fi))
7362 goto out;
7363
Chris Mason46bfbb52010-05-26 11:04:10 -04007364 backref_offset = btrfs_file_extent_offset(leaf, fi);
7365
Josef Bacik7ee9e442013-06-21 16:37:03 -04007366 if (orig_start) {
7367 *orig_start = key.offset - backref_offset;
7368 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7369 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7370 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007371
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007372 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007373 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007374
7375 num_bytes = min(offset + *len, extent_end) - offset;
7376 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7377 u64 range_end;
7378
Jeff Mahoneyda170662016-06-15 09:22:56 -04007379 range_end = round_up(offset + num_bytes,
7380 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007381 ret = test_range_bit(io_tree, offset, range_end,
7382 EXTENT_DELALLOC, 0, NULL);
7383 if (ret) {
7384 ret = -EAGAIN;
7385 goto out;
7386 }
7387 }
7388
Josef Bacik1bda19e2013-10-18 12:10:36 -04007389 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007390
7391 /*
7392 * look for other files referencing this extent, if we
7393 * find any we must cow
7394 */
Josef Bacik00361582013-08-14 14:02:47 -04007395
Liu Boe4c3b2d2017-01-30 12:25:28 -08007396 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007397 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007398 if (ret) {
7399 ret = 0;
7400 goto out;
7401 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007402
7403 /*
7404 * adjust disk_bytenr and num_bytes to cover just the bytes
7405 * in this extent we are about to write. If there
7406 * are any csums in that range we have to cow in order
7407 * to keep the csums correct
7408 */
7409 disk_bytenr += backref_offset;
7410 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007411 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7412 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007413 /*
7414 * all of the above have passed, it is safe to overwrite this extent
7415 * without cow
7416 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007417 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007418 ret = 1;
7419out:
7420 btrfs_free_path(path);
7421 return ret;
7422}
7423
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007424bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7425{
7426 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007427 bool found = false;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007428 void **pagep = NULL;
7429 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007430 unsigned long start_idx;
7431 unsigned long end_idx;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007432
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007433 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007434
7435 /*
7436 * end is the last byte in the last page. end == start is legal
7437 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007438 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007439
7440 rcu_read_lock();
7441
7442 /* Most of the code in this while loop is lifted from
7443 * find_get_page. It's been modified to begin searching from a
7444 * page and return just the first page found in that range. If the
7445 * found idx is less than or equal to the end idx then we know that
7446 * a page exists. If no pages are found or if those pages are
7447 * outside of the range then we're fine (yay!) */
7448 while (page == NULL &&
7449 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7450 page = radix_tree_deref_slot(pagep);
7451 if (unlikely(!page))
7452 break;
7453
7454 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007455 if (radix_tree_deref_retry(page)) {
7456 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007457 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007458 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007459 /*
7460 * Otherwise, shmem/tmpfs must be storing a swap entry
7461 * here as an exceptional entry: so return it without
7462 * attempting to raise page count.
7463 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007464 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007465 break; /* TODO: Is this relevant for this use case? */
7466 }
7467
Filipe Manana91405152014-06-05 13:22:24 +01007468 if (!page_cache_get_speculative(page)) {
7469 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007470 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007471 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007472
7473 /*
7474 * Has the page moved?
7475 * This is part of the lockless pagecache protocol. See
7476 * include/linux/pagemap.h for details.
7477 */
7478 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007479 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007480 page = NULL;
7481 }
7482 }
7483
7484 if (page) {
7485 if (page->index <= end_idx)
7486 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007487 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007488 }
7489
7490 rcu_read_unlock();
7491 return found;
7492}
7493
Josef Bacikeb838e72012-07-31 16:28:48 -04007494static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7495 struct extent_state **cached_state, int writing)
7496{
7497 struct btrfs_ordered_extent *ordered;
7498 int ret = 0;
7499
7500 while (1) {
7501 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007502 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007503 /*
7504 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007505 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007506 * extents in this range.
7507 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007508 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007509 lockend - lockstart + 1);
7510
7511 /*
7512 * We need to make sure there are no buffered pages in this
7513 * range either, we could have raced between the invalidate in
7514 * generic_file_direct_write and locking the extent. The
7515 * invalidate needs to happen so that reads after a write do not
7516 * get stale data.
7517 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007518 if (!ordered &&
7519 (!writing ||
7520 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007521 break;
7522
7523 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007524 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007525
7526 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007527 /*
7528 * If we are doing a DIO read and the ordered extent we
7529 * found is for a buffered write, we can not wait for it
7530 * to complete and retry, because if we do so we can
7531 * deadlock with concurrent buffered writes on page
7532 * locks. This happens only if our DIO read covers more
7533 * than one extent map, if at this point has already
7534 * created an ordered extent for a previous extent map
7535 * and locked its range in the inode's io tree, and a
7536 * concurrent write against that previous extent map's
7537 * range and this range started (we unlock the ranges
7538 * in the io tree only when the bios complete and
7539 * buffered writes always lock pages before attempting
7540 * to lock range in the io tree).
7541 */
7542 if (writing ||
7543 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7544 btrfs_start_ordered_extent(inode, ordered, 1);
7545 else
7546 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007547 btrfs_put_ordered_extent(ordered);
7548 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007549 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007550 * We could trigger writeback for this range (and wait
7551 * for it to complete) and then invalidate the pages for
7552 * this range (through invalidate_inode_pages2_range()),
7553 * but that can lead us to a deadlock with a concurrent
7554 * call to readpages() (a buffered read or a defrag call
7555 * triggered a readahead) on a page lock due to an
7556 * ordered dio extent we created before but did not have
7557 * yet a corresponding bio submitted (whence it can not
7558 * complete), which makes readpages() wait for that
7559 * ordered extent to complete while holding a lock on
7560 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007561 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007562 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007563 }
7564
Filipe Mananaade77022016-02-18 14:28:55 +00007565 if (ret)
7566 break;
7567
Josef Bacikeb838e72012-07-31 16:28:48 -04007568 cond_resched();
7569 }
7570
7571 return ret;
7572}
7573
Liu Bo6f9994d2017-01-31 07:50:22 -08007574/* The callers of this must take lock_extent() */
7575static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7576 u64 orig_start, u64 block_start,
7577 u64 block_len, u64 orig_block_len,
7578 u64 ram_bytes, int compress_type,
7579 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007580{
7581 struct extent_map_tree *em_tree;
7582 struct extent_map *em;
7583 struct btrfs_root *root = BTRFS_I(inode)->root;
7584 int ret;
7585
Liu Bo6f9994d2017-01-31 07:50:22 -08007586 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7587 type == BTRFS_ORDERED_COMPRESSED ||
7588 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007589 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007590
Josef Bacik69ffb542012-09-11 15:40:07 -04007591 em_tree = &BTRFS_I(inode)->extent_tree;
7592 em = alloc_extent_map();
7593 if (!em)
7594 return ERR_PTR(-ENOMEM);
7595
7596 em->start = start;
7597 em->orig_start = orig_start;
7598 em->len = len;
7599 em->block_len = block_len;
7600 em->block_start = block_start;
7601 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007602 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007603 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007604 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007605 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007606 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007607 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007608 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007609 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7610 em->compress_type = compress_type;
7611 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007612
7613 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007614 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007615 em->start + em->len - 1, 0);
7616 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007617 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007618 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007619 /*
7620 * The caller has taken lock_extent(), who could race with us
7621 * to add em?
7622 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007623 } while (ret == -EEXIST);
7624
7625 if (ret) {
7626 free_extent_map(em);
7627 return ERR_PTR(ret);
7628 }
7629
Liu Bo6f9994d2017-01-31 07:50:22 -08007630 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007631 return em;
7632}
7633
Josef Bacik4b46fce2010-05-23 11:00:55 -04007634static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7635 struct buffer_head *bh_result, int create)
7636{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007637 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007638 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007639 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307640 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007641 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007642 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007643 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007644 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007645 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007646
Miao Xie172a5042013-02-21 02:48:22 -07007647 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007648 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007649 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007650 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007651
Josef Bacikc3298612012-08-03 16:49:19 -04007652 lockstart = start;
7653 lockend = start + len - 1;
7654
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007655 if (current->journal_info) {
7656 /*
7657 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007658 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007659 * confused.
7660 */
chandan50745b02015-08-28 21:10:13 +05307661 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007662 current->journal_info = NULL;
7663 }
7664
Josef Bacikeb838e72012-07-31 16:28:48 -04007665 /*
7666 * If this errors out it's because we couldn't invalidate pagecache for
7667 * this range and we need to fallback to buffered.
7668 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007669 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7670 create)) {
7671 ret = -ENOTBLK;
7672 goto err;
7673 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007674
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007675 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007676 if (IS_ERR(em)) {
7677 ret = PTR_ERR(em);
7678 goto unlock_err;
7679 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007680
7681 /*
7682 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7683 * io. INLINE is special, and we could probably kludge it in here, but
7684 * it's still buffered so for safety lets just fall back to the generic
7685 * buffered path.
7686 *
7687 * For COMPRESSED we _have_ to read the entire extent in so we can
7688 * decompress it, so there will be buffering required no matter what we
7689 * do, so go ahead and fallback to buffered.
7690 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007691 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007692 * to buffered IO. Don't blame me, this is the price we pay for using
7693 * the generic code.
7694 */
7695 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7696 em->block_start == EXTENT_MAP_INLINE) {
7697 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007698 ret = -ENOTBLK;
7699 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007700 }
7701
7702 /* Just a good old fashioned hole, return */
7703 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7704 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7705 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007706 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007707 }
7708
7709 /*
7710 * We don't allocate a new extent in the following cases
7711 *
7712 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7713 * existing extent.
7714 * 2) The extent is marked as PREALLOC. We're good to go here and can
7715 * just use the extent.
7716 *
7717 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007718 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007719 len = min(len, em->len - (start - em->start));
7720 lockstart = start + len;
7721 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007722 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007723
7724 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7725 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7726 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007727 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007728 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007729
7730 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7731 type = BTRFS_ORDERED_PREALLOC;
7732 else
7733 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007734 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007735 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007736
Josef Bacik00361582013-08-14 14:02:47 -04007737 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007738 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007739 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007740 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007741
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007742 em2 = btrfs_create_dio_extent(inode, start, len,
7743 orig_start, block_start,
7744 len, orig_block_len,
7745 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007746 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007747 if (type == BTRFS_ORDERED_PREALLOC) {
7748 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007749 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007750 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007751 if (em2 && IS_ERR(em2)) {
7752 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007753 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007754 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007755 /*
7756 * For inode marked NODATACOW or extent marked PREALLOC,
7757 * use the existing or preallocated extent, so does not
7758 * need to adjust btrfs_space_info's bytes_may_use.
7759 */
7760 btrfs_free_reserved_data_space_noquota(inode,
7761 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007762 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007763 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007764 }
Josef Bacik00361582013-08-14 14:02:47 -04007765
Chris Mason46bfbb52010-05-26 11:04:10 -04007766 /*
7767 * this will cow the extent, reset the len in case we changed
7768 * it above
7769 */
7770 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007771 free_extent_map(em);
7772 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007773 if (IS_ERR(em)) {
7774 ret = PTR_ERR(em);
7775 goto unlock_err;
7776 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007777 len = min(len, em->len - (start - em->start));
7778unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007779 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7780 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007781 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007782 bh_result->b_bdev = em->bdev;
7783 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007784 if (create) {
7785 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7786 set_buffer_new(bh_result);
7787
7788 /*
7789 * Need to update the i_size under the extent lock so buffered
7790 * readers will get the updated i_size when we unlock.
7791 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007792 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007793 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007794
chandan50745b02015-08-28 21:10:13 +05307795 WARN_ON(dio_data->reserve < len);
7796 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007797 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307798 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007799 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007800
Josef Bacikeb838e72012-07-31 16:28:48 -04007801 /*
7802 * In the case of write we need to clear and unlock the entire range,
7803 * in the case of read we need to unlock only the end area that we
7804 * aren't using if there is any left over space.
7805 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007806 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007807 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7808 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007809 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007810 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007811 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007812 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007813
Josef Bacik4b46fce2010-05-23 11:00:55 -04007814 free_extent_map(em);
7815
7816 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007817
7818unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007819 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007820 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007821err:
chandan50745b02015-08-28 21:10:13 +05307822 if (dio_data)
7823 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007824 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007825}
7826
Omar Sandoval58efbc92017-08-22 23:45:59 -07007827static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7828 struct bio *bio,
7829 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007830{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007831 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007832 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007833
Mike Christie37226b22016-06-05 14:31:52 -05007834 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007835
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007836 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007837 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007838 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007839
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007840 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007841
Miao Xie8b110e32014-09-12 18:44:03 +08007842 return ret;
7843}
7844
7845static int btrfs_check_dio_repairable(struct inode *inode,
7846 struct bio *failed_bio,
7847 struct io_failure_record *failrec,
7848 int failed_mirror)
7849{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007850 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007851 int num_copies;
7852
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007853 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007854 if (num_copies == 1) {
7855 /*
7856 * we only have a single copy of the data, so don't bother with
7857 * all the retry and error correction code that follows. no
7858 * matter what the error is, it is very likely to persist.
7859 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007860 btrfs_debug(fs_info,
7861 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7862 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007863 return 0;
7864 }
7865
7866 failrec->failed_mirror = failed_mirror;
7867 failrec->this_mirror++;
7868 if (failrec->this_mirror == failed_mirror)
7869 failrec->this_mirror++;
7870
7871 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007872 btrfs_debug(fs_info,
7873 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7874 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007875 return 0;
7876 }
7877
7878 return 1;
7879}
7880
Omar Sandoval58efbc92017-08-22 23:45:59 -07007881static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7882 struct page *page, unsigned int pgoff,
7883 u64 start, u64 end, int failed_mirror,
7884 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007885{
7886 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007887 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7888 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007889 struct bio *bio;
7890 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007891 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007892 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007893 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007894 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007895
Mike Christie37226b22016-06-05 14:31:52 -05007896 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007897
7898 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7899 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007900 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007901
7902 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7903 failed_mirror);
7904 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007905 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007906 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007907 }
7908
Liu Bo17347ce2017-05-15 15:33:27 -07007909 segs = bio_segments(failed_bio);
7910 if (segs > 1 ||
7911 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007912 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007913
7914 isector = start - btrfs_io_bio(failed_bio)->logical;
7915 isector >>= inode->i_sb->s_blocksize_bits;
7916 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307917 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05007918 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007919
7920 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007921 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007922 read_mode, failrec->this_mirror, failrec->in_validation);
7923
Omar Sandoval58efbc92017-08-22 23:45:59 -07007924 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7925 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007926 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007927 bio_put(bio);
7928 }
7929
Omar Sandoval58efbc92017-08-22 23:45:59 -07007930 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007931}
7932
7933struct btrfs_retry_complete {
7934 struct completion done;
7935 struct inode *inode;
7936 u64 start;
7937 int uptodate;
7938};
7939
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007940static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007941{
7942 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007943 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007944 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007945 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007946 int i;
7947
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007948 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007949 goto end;
7950
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307951 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007952 io_tree = &BTRFS_I(inode)->io_tree;
7953 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7954 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307955
Miao Xie8b110e32014-09-12 18:44:03 +08007956 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007957 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007958 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007959 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7960 io_tree, done->start, bvec->bv_page,
7961 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007962end:
7963 complete(&done->done);
7964 bio_put(bio);
7965}
7966
Omar Sandoval58efbc92017-08-22 23:45:59 -07007967static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7968 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007969{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307970 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007971 struct bio_vec bvec;
7972 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007973 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007974 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307975 unsigned int pgoff;
7976 u32 sectorsize;
7977 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007978 blk_status_t ret;
7979 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007980
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307981 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007982 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307983
Miao Xiec1dc0892014-09-12 18:43:56 +08007984 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007985 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007986 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007987
Liu Bo17347ce2017-05-15 15:33:27 -07007988 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7989 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7990 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307991
7992next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007993 done.uptodate = 0;
7994 done.start = start;
7995 init_completion(&done.done);
7996
Liu Bo17347ce2017-05-15 15:33:27 -07007997 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307998 pgoff, start, start + sectorsize - 1,
7999 io_bio->mirror_num,
8000 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008001 if (ret) {
8002 err = ret;
8003 goto next;
8004 }
Miao Xie8b110e32014-09-12 18:44:03 +08008005
David Sterba9c17f6c2017-07-19 19:26:45 +02008006 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008007
8008 if (!done.uptodate) {
8009 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308010 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008011 }
8012
Liu Bo629ebf42017-05-15 17:20:07 -07008013next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308014 start += sectorsize;
8015
Liu Bo97bf5a52017-04-07 13:11:10 -07008016 nr_sectors--;
8017 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308018 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008019 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308020 goto next_block_or_try_again;
8021 }
Miao Xie8b110e32014-09-12 18:44:03 +08008022 }
8023
Liu Bo629ebf42017-05-15 17:20:07 -07008024 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008025}
8026
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008027static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008028{
8029 struct btrfs_retry_complete *done = bio->bi_private;
8030 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008031 struct extent_io_tree *io_tree, *failure_tree;
8032 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008033 struct bio_vec *bvec;
8034 int uptodate;
8035 int ret;
8036 int i;
8037
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008038 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008039 goto end;
8040
8041 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308042
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308043 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008044 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308045
Josef Bacik7870d082017-05-05 11:57:15 -04008046 io_tree = &BTRFS_I(inode)->io_tree;
8047 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8048
David Sterbac09abff2017-07-13 18:10:07 +02008049 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008050 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008051 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8052 bvec->bv_offset, done->start,
8053 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008054 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008055 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8056 failure_tree, io_tree, done->start,
8057 bvec->bv_page,
8058 btrfs_ino(BTRFS_I(inode)),
8059 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008060 else
8061 uptodate = 0;
8062 }
8063
8064 done->uptodate = uptodate;
8065end:
8066 complete(&done->done);
8067 bio_put(bio);
8068}
8069
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008070static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8071 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008072{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308073 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008074 struct bio_vec bvec;
8075 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008076 struct btrfs_retry_complete done;
8077 u64 start;
8078 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308079 u32 sectorsize;
8080 int nr_sectors;
8081 unsigned int pgoff;
8082 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008083 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008084 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008085 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008086
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308087 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008088 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308089
Omar Sandoval58efbc92017-08-22 23:45:59 -07008090 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008091 start = io_bio->logical;
8092 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008093 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008094
Liu Bo17347ce2017-05-15 15:33:27 -07008095 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8096 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308097
Liu Bo17347ce2017-05-15 15:33:27 -07008098 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308099next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008100 if (uptodate) {
8101 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8102 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8103 bvec.bv_page, pgoff, start, sectorsize);
8104 if (likely(!ret))
8105 goto next;
8106 }
Miao Xie8b110e32014-09-12 18:44:03 +08008107try_again:
8108 done.uptodate = 0;
8109 done.start = start;
8110 init_completion(&done.done);
8111
Omar Sandoval58efbc92017-08-22 23:45:59 -07008112 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8113 pgoff, start, start + sectorsize - 1,
8114 io_bio->mirror_num, btrfs_retry_endio,
8115 &done);
8116 if (status) {
8117 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008118 goto next;
8119 }
8120
David Sterba9c17f6c2017-07-19 19:26:45 +02008121 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008122
8123 if (!done.uptodate) {
8124 /* We might have another mirror, so try again */
8125 goto try_again;
8126 }
8127next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308128 offset += sectorsize;
8129 start += sectorsize;
8130
8131 ASSERT(nr_sectors);
8132
Liu Bo97bf5a52017-04-07 13:11:10 -07008133 nr_sectors--;
8134 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308135 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008136 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308137 goto next_block;
8138 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008139 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008140
8141 return err;
8142}
8143
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008144static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8145 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008146{
8147 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8148
8149 if (skip_csum) {
8150 if (unlikely(err))
8151 return __btrfs_correct_data_nocsum(inode, io_bio);
8152 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008153 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008154 } else {
8155 return __btrfs_subio_endio_read(inode, io_bio, err);
8156 }
8157}
8158
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008159static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008160{
8161 struct btrfs_dio_private *dip = bio->bi_private;
8162 struct inode *inode = dip->inode;
8163 struct bio *dio_bio;
8164 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008165 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008166
Liu Bo99c4e3b92017-09-15 15:06:51 -06008167 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008168 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008169
Josef Bacik4b46fce2010-05-23 11:00:55 -04008170 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008171 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008172 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008173
Josef Bacik4b46fce2010-05-23 11:00:55 -04008174 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008175
Liu Bo99c4e3b92017-09-15 15:06:51 -06008176 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008177 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008178
8179 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008180 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008181 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008182}
8183
Qu Wenruo524272602017-03-08 10:25:52 +08008184static void __endio_write_update_ordered(struct inode *inode,
8185 const u64 offset, const u64 bytes,
8186 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008187{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008188 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008189 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008190 struct btrfs_workqueue *wq;
8191 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008192 u64 ordered_offset = offset;
8193 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008194 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008195 int ret;
8196
Qu Wenruo524272602017-03-08 10:25:52 +08008197 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8198 wq = fs_info->endio_freespace_worker;
8199 func = btrfs_freespace_write_helper;
8200 } else {
8201 wq = fs_info->endio_write_workers;
8202 func = btrfs_endio_write_helper;
8203 }
8204
Chris Mason163cf092010-11-28 19:56:33 -05008205again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008206 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008207 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8208 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008209 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008210 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008211 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008212 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008213
Qu Wenruo524272602017-03-08 10:25:52 +08008214 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8215 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008216out_test:
8217 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008218 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8219 * in the range, we can exit.
8220 */
8221 if (ordered_offset == last_offset)
8222 return;
8223 /*
Chris Mason163cf092010-11-28 19:56:33 -05008224 * our bio might span multiple ordered extents. If we haven't
8225 * completed the accounting for the whole dio, go back and try again
8226 */
Filipe Manana14543772015-11-24 16:23:54 +00008227 if (ordered_offset < offset + bytes) {
8228 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008229 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008230 goto again;
8231 }
Filipe Manana14543772015-11-24 16:23:54 +00008232}
8233
8234static void btrfs_endio_direct_write(struct bio *bio)
8235{
8236 struct btrfs_dio_private *dip = bio->bi_private;
8237 struct bio *dio_bio = dip->dio_bio;
8238
Qu Wenruo524272602017-03-08 10:25:52 +08008239 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008240 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008241
Josef Bacik4b46fce2010-05-23 11:00:55 -04008242 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008243
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008244 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008245 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008246 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008247}
8248
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008249static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008250 struct bio *bio, int mirror_num,
8251 unsigned long bio_flags, u64 offset)
8252{
Josef Bacikc6100a42017-05-05 11:57:13 -04008253 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008254 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008255 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008256 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008257 return 0;
8258}
8259
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008260static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008261{
8262 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008263 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008264
Miao Xie8b110e32014-09-12 18:44:03 +08008265 if (err)
8266 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008267 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008268 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8269 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008270 (unsigned long long)bio->bi_iter.bi_sector,
8271 bio->bi_iter.bi_size, err);
8272
8273 if (dip->subio_endio)
8274 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008275
8276 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008277 dip->errors = 1;
8278
8279 /*
8280 * before atomic variable goto zero, we must make sure
8281 * dip->errors is perceived to be set.
8282 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008283 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008284 }
8285
8286 /* if there are more bios still pending for this dio, just exit */
8287 if (!atomic_dec_and_test(&dip->pending_bios))
8288 goto out;
8289
Chris Mason9be33952013-05-17 18:30:14 -04008290 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008291 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008292 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008293 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008294 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008295 }
8296out:
8297 bio_put(bio);
8298}
8299
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008300static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008301 struct btrfs_dio_private *dip,
8302 struct bio *bio,
8303 u64 file_offset)
8304{
8305 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8306 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008307 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008308
8309 /*
8310 * We load all the csum data we need when we submit
8311 * the first bio to reduce the csum tree search and
8312 * contention.
8313 */
8314 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008315 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008316 file_offset);
8317 if (ret)
8318 return ret;
8319 }
8320
8321 if (bio == dip->orig_bio)
8322 return 0;
8323
8324 file_offset -= dip->logical_offset;
8325 file_offset >>= inode->i_sb->s_blocksize_bits;
8326 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8327
8328 return 0;
8329}
8330
Omar Sandoval58efbc92017-08-22 23:45:59 -07008331static inline blk_status_t
8332__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008333 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008334{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008335 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008336 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008337 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008338 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008339
Liu Bo4c274bc2017-11-01 17:19:27 -06008340 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008341 if (async_submit)
8342 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8343
Josef Bacik5fd02042012-05-02 14:00:54 -04008344 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008345 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008346 if (ret)
8347 goto err;
8348 }
Miao Xiee65e1532010-11-22 03:04:43 +00008349
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008350 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008351 goto map;
8352
8353 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008354 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8355 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008356 __btrfs_submit_bio_start_direct_io,
8357 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008358 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008359 } else if (write) {
8360 /*
8361 * If we aren't doing async submit, calculate the csum of the
8362 * bio now.
8363 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008364 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008365 if (ret)
8366 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008367 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008368 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008369 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008370 if (ret)
8371 goto err;
8372 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008373map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008374 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008375err:
Miao Xiee65e1532010-11-22 03:04:43 +00008376 return ret;
8377}
8378
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008379static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008380{
8381 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008382 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008383 struct bio *bio;
8384 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008385 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008386 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008387 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008388 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008389 u64 submit_len;
8390 int clone_offset = 0;
8391 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308392 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008393 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008394
Kent Overstreet4f024f32013-10-11 15:44:27 -07008395 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008396 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008397 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8398 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008399 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008400 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008401
Liu Bo725130b2017-05-16 09:51:39 -07008402 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008403 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008404 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008405 goto submit;
8406 }
8407
David Woodhouse53b381b2013-01-29 18:40:14 -05008408 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008409 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008410 async_submit = 0;
8411 else
8412 async_submit = 1;
8413
Liu Bo725130b2017-05-16 09:51:39 -07008414 /* bio split */
8415 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008416 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008417 do {
Liu Bo725130b2017-05-16 09:51:39 -07008418 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008419
Liu Bo725130b2017-05-16 09:51:39 -07008420 /*
8421 * This will never fail as it's passing GPF_NOFS and
8422 * the allocation is backed by btrfs_bioset.
8423 */
Liu Boe4770942017-05-16 10:57:14 -07008424 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008425 clone_len);
8426 bio->bi_private = dip;
8427 bio->bi_end_io = btrfs_end_dio_bio;
8428 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008429
Liu Bo725130b2017-05-16 09:51:39 -07008430 ASSERT(submit_len >= clone_len);
8431 submit_len -= clone_len;
8432 if (submit_len == 0)
8433 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008434
Liu Bo725130b2017-05-16 09:51:39 -07008435 /*
8436 * Increase the count before we submit the bio so we know
8437 * the end IO handler won't happen before we increase the
8438 * count. Otherwise, the dip might get freed before we're
8439 * done setting it up.
8440 */
8441 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008442
Linus Torvalds66ba7722017-09-09 13:27:51 -07008443 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008444 async_submit);
8445 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008446 bio_put(bio);
8447 atomic_dec(&dip->pending_bios);
8448 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008449 }
Liu Bo725130b2017-05-16 09:51:39 -07008450
8451 clone_offset += clone_len;
8452 start_sector += clone_len >> 9;
8453 file_offset += clone_len;
8454
8455 map_length = submit_len;
8456 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8457 start_sector << 9, &map_length, NULL, 0);
8458 if (ret)
8459 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008460 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008461
Josef Bacik02f57c72011-04-06 14:25:44 -04008462submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008463 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008464 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008465 return 0;
8466
8467 bio_put(bio);
8468out_err:
8469 dip->errors = 1;
8470 /*
8471 * before atomic variable goto zero, we must
8472 * make sure dip->errors is perceived to be set.
8473 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008474 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008475 if (atomic_dec_and_test(&dip->pending_bios))
8476 bio_io_error(dip->orig_bio);
8477
8478 /* bio_end_io() will handle error, so we needn't return it */
8479 return 0;
8480}
8481
Mike Christie8a4c1e42016-06-05 14:31:50 -05008482static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8483 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008484{
Filipe Manana61de7182015-07-01 12:13:10 +01008485 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008486 struct bio *bio = NULL;
8487 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008488 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008489 int ret = 0;
8490
David Sterba8b6c1d52017-06-02 17:48:13 +02008491 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008492
Miao Xiec1dc0892014-09-12 18:43:56 +08008493 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008494 if (!dip) {
8495 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008496 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008497 }
8498
8499 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008500 dip->inode = inode;
8501 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008502 dip->bytes = dio_bio->bi_iter.bi_size;
8503 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008504 bio->bi_private = dip;
8505 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008506 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008507 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008508 io_bio = btrfs_io_bio(bio);
8509 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008510
Miao Xiec1dc0892014-09-12 18:43:56 +08008511 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008512 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008513 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008514 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008515 dip->subio_endio = btrfs_subio_endio_read;
8516 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008517
Filipe Mananaf28a4922015-12-08 19:23:20 +00008518 /*
8519 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8520 * even if we fail to submit a bio, because in such case we do the
8521 * corresponding error handling below and it must not be done a second
8522 * time by btrfs_direct_IO().
8523 */
8524 if (write) {
8525 struct btrfs_dio_data *dio_data = current->journal_info;
8526
8527 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8528 dip->bytes;
8529 dio_data->unsubmitted_oe_range_start =
8530 dio_data->unsubmitted_oe_range_end;
8531 }
8532
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008533 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008534 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008535 return;
Chris Mason9be33952013-05-17 18:30:14 -04008536
Liu Bo3892ac92017-04-17 15:00:28 -07008537 if (io_bio->end_io)
8538 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008539
Josef Bacik4b46fce2010-05-23 11:00:55 -04008540free_ordered:
8541 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008542 * If we arrived here it means either we failed to submit the dip
8543 * or we either failed to clone the dio_bio or failed to allocate the
8544 * dip. If we cloned the dio_bio and allocated the dip, we can just
8545 * call bio_endio against our io_bio so that we get proper resource
8546 * cleanup if we fail to submit the dip, otherwise, we must do the
8547 * same as btrfs_endio_direct_[write|read] because we can't call these
8548 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008549 */
Liu Bo3892ac92017-04-17 15:00:28 -07008550 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008551 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008552 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008553 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008554 * and all the cleanup and final put for dio_bio (through
8555 * dio_end_io()).
8556 */
8557 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008558 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008559 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008560 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008561 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008562 file_offset,
8563 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008564 false);
Filipe Manana14543772015-11-24 16:23:54 +00008565 else
Filipe Manana61de7182015-07-01 12:13:10 +01008566 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8567 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008568
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008569 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008570 /*
8571 * Releases and cleans up our dio_bio, no need to bio_put()
8572 * nor bio_endio()/bio_io_error() against dio_bio.
8573 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008574 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008575 }
Liu Bo3892ac92017-04-17 15:00:28 -07008576 if (bio)
8577 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008578 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008579}
8580
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008581static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008582 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008583{
8584 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008585 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008586 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008587 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008588
8589 if (offset & blocksize_mask)
8590 goto out;
8591
Al Viro28060d52014-03-22 05:15:17 -04008592 if (iov_iter_alignment(iter) & blocksize_mask)
8593 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008594
Al Viro28060d52014-03-22 05:15:17 -04008595 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008596 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008597 return 0;
8598 /*
8599 * Check to make sure we don't have duplicate iov_base's in this
8600 * iovec, if so return EINVAL, otherwise we'll get csum errors
8601 * when reading back.
8602 */
8603 for (seg = 0; seg < iter->nr_segs; seg++) {
8604 for (i = seg + 1; i < iter->nr_segs; i++) {
8605 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008606 goto out;
8607 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008608 }
8609 retval = 0;
8610out:
8611 return retval;
8612}
Josef Bacikeb838e72012-07-31 16:28:48 -04008613
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008614static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008615{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008616 struct file *file = iocb->ki_filp;
8617 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008618 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308619 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008620 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008621 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008622 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008623 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008624 bool wakeup = true;
8625 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008626 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008627
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008628 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008629 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008630
Jens Axboefe0f07d2015-04-15 17:05:48 -06008631 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008632
Josef Bacik0e267c42013-07-02 10:38:02 -04008633 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008634 * The generic stuff only does filemap_write_and_wait_range, which
8635 * isn't enough if we've written compressed pages to this area, so
8636 * we need to flush the dirty pages again to make absolutely sure
8637 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008638 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008639 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008640 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8641 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008642 filemap_fdatawrite_range(inode->i_mapping, offset,
8643 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008644
Omar Sandoval6f673762015-03-16 04:33:52 -07008645 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008646 /*
8647 * If the write DIO is beyond the EOF, we need update
8648 * the isize, but it is protected by i_mutex. So we can
8649 * not unlock the i_mutex at this case.
8650 */
8651 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008652 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008653 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008654 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008655 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8656 ret = -EAGAIN;
8657 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008658 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008659 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8660 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008661 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008662 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008663
8664 /*
8665 * We need to know how many extents we reserved so that we can
8666 * do the accounting properly if we go over the number we
8667 * originally calculated. Abuse current->journal_info for this.
8668 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008669 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008670 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008671 dio_data.unsubmitted_oe_range_start = (u64)offset;
8672 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308673 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308674 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008675 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8676 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008677 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008678 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8679 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008680 }
8681
Omar Sandoval17f8c842015-03-16 04:33:50 -07008682 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008683 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008684 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008685 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008686 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308687 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008688 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008689 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308690 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008691 btrfs_delalloc_release_space(inode, data_reserved,
8692 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008693 /*
8694 * On error we might have left some ordered extents
8695 * without submitting corresponding bios for them, so
8696 * cleanup them up to avoid other tasks getting them
8697 * and waiting for them to complete forever.
8698 */
8699 if (dio_data.unsubmitted_oe_range_start <
8700 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008701 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008702 dio_data.unsubmitted_oe_range_start,
8703 dio_data.unsubmitted_oe_range_end -
8704 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008705 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008706 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008707 btrfs_delalloc_release_space(inode, data_reserved,
8708 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008709 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008710 }
Miao Xie38851cc2013-02-08 07:04:11 +00008711out:
Miao Xie2e60a512013-02-08 07:01:08 +00008712 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008713 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008714 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008715 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008716
Qu Wenruo364ecf32017-02-27 15:10:38 +08008717 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008718 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008719}
8720
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008721#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8722
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008723static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8724 __u64 start, __u64 len)
8725{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008726 int ret;
8727
8728 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8729 if (ret)
8730 return ret;
8731
David Sterba2135fb92017-06-23 04:09:57 +02008732 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008733}
8734
Chris Mason9ebefb182007-06-15 13:50:00 -04008735int btrfs_readpage(struct file *file, struct page *page)
8736{
Chris Masond1310b22008-01-24 16:13:08 -05008737 struct extent_io_tree *tree;
8738 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008739 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008740}
Chris Mason1832a6d2007-12-21 16:27:21 -05008741
Chris Mason39279cc2007-06-12 06:35:45 -04008742static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8743{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008744 struct inode *inode = page->mapping->host;
8745 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008746
8747 if (current->flags & PF_MEMALLOC) {
8748 redirty_page_for_writepage(wbc, page);
8749 unlock_page(page);
8750 return 0;
8751 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008752
8753 /*
8754 * If we are under memory pressure we will call this directly from the
8755 * VM, we need to make sure we have the inode referenced for the ordered
8756 * extent. If not just return like we didn't do anything.
8757 */
8758 if (!igrab(inode)) {
8759 redirty_page_for_writepage(wbc, page);
8760 return AOP_WRITEPAGE_ACTIVATE;
8761 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008762 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008763 btrfs_add_delayed_iput(inode);
8764 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008765}
Chris Mason39279cc2007-06-12 06:35:45 -04008766
Eric Sandeen48a3b632013-04-25 20:41:01 +00008767static int btrfs_writepages(struct address_space *mapping,
8768 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008769{
Chris Masond1310b22008-01-24 16:13:08 -05008770 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008771
Chris Masond1310b22008-01-24 16:13:08 -05008772 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba43317592017-06-23 03:46:07 +02008773 return extent_writepages(tree, mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008774}
8775
Chris Mason3ab2fb52007-11-08 10:59:22 -05008776static int
8777btrfs_readpages(struct file *file, struct address_space *mapping,
8778 struct list_head *pages, unsigned nr_pages)
8779{
Chris Masond1310b22008-01-24 16:13:08 -05008780 struct extent_io_tree *tree;
8781 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba09325842017-06-23 04:09:57 +02008782 return extent_readpages(tree, mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008783}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008784static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008785{
Chris Masond1310b22008-01-24 16:13:08 -05008786 struct extent_io_tree *tree;
8787 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008788 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008789
Chris Masond1310b22008-01-24 16:13:08 -05008790 tree = &BTRFS_I(page->mapping->host)->io_tree;
8791 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008792 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008793 if (ret == 1) {
8794 ClearPagePrivate(page);
8795 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008796 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008797 }
8798 return ret;
8799}
Chris Mason39279cc2007-06-12 06:35:45 -04008800
Chris Masone6dcd2d2008-07-17 12:53:50 -04008801static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8802{
Chris Mason98509cf2008-09-11 15:51:43 -04008803 if (PageWriteback(page) || PageDirty(page))
8804 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008805 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008806}
8807
Lukas Czernerd47992f2013-05-21 23:17:23 -04008808static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8809 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008810{
Josef Bacik5fd02042012-05-02 14:00:54 -04008811 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008812 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008813 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008814 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008815 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008816 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308817 u64 start;
8818 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008819 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008820
Chris Mason8b62b722009-09-02 16:53:46 -04008821 /*
8822 * we have the page locked, so new writeback can't start,
8823 * and the dirty bit won't be cleared while we are here.
8824 *
8825 * Wait for IO on this page so that we can safely clear
8826 * the PagePrivate2 bit and do ordered accounting
8827 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008828 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008829
Josef Bacik5fd02042012-05-02 14:00:54 -04008830 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008831 if (offset) {
8832 btrfs_releasepage(page, GFP_NOFS);
8833 return;
8834 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008835
8836 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008837 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308838again:
8839 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008840 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308841 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008842 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308843 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008844 /*
8845 * IO on this page will never be started, so we need
8846 * to account for any ordered extents now
8847 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008848 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308849 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008850 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008851 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008852 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008853 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008854 /*
8855 * whoever cleared the private bit is responsible
8856 * for the finish_ordered_io
8857 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008858 if (TestClearPagePrivate2(page)) {
8859 struct btrfs_ordered_inode_tree *tree;
8860 u64 new_len;
8861
8862 tree = &BTRFS_I(inode)->ordered_tree;
8863
8864 spin_lock_irq(&tree->lock);
8865 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308866 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008867 if (new_len < ordered->truncated_len)
8868 ordered->truncated_len = new_len;
8869 spin_unlock_irq(&tree->lock);
8870
8871 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308872 start,
8873 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008874 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008875 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008876 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008877 if (!inode_evicting) {
8878 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308879 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008880 &cached_state);
8881 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308882
8883 start = end + 1;
8884 if (start < page_end)
8885 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008886 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008887
Qu Wenruob9d0b382015-09-29 10:35:16 +08008888 /*
8889 * Qgroup reserved space handler
8890 * Page here will be either
8891 * 1) Already written to disk
8892 * In this case, its reserved space is released from data rsv map
8893 * and will be freed by delayed_ref handler finally.
8894 * So even we call qgroup_free_data(), it won't decrease reserved
8895 * space.
8896 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008897 * This means the reserved space should be freed here. However,
8898 * if a truncate invalidates the page (by clearing PageDirty)
8899 * and the page is accounted for while allocating extent
8900 * in btrfs_check_data_free_space() we let delayed_ref to
8901 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008902 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008903 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008904 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008905 if (!inode_evicting) {
8906 clear_extent_bit(tree, page_start, page_end,
8907 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008908 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8909 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008910 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008911
8912 __btrfs_releasepage(page, GFP_NOFS);
8913 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008914
Chris Mason4a096752008-07-21 10:29:44 -04008915 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008916 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008917 ClearPagePrivate(page);
8918 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008919 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008920 }
Chris Mason39279cc2007-06-12 06:35:45 -04008921}
8922
Chris Mason9ebefb182007-06-15 13:50:00 -04008923/*
8924 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8925 * called from a page fault handler when a page is first dirtied. Hence we must
8926 * be careful to check for EOF conditions here. We set the page up correctly
8927 * for a written page which means we get ENOSPC checking when writing into
8928 * holes and correct delalloc and unwritten extent mapping on filesystems that
8929 * support these features.
8930 *
8931 * We are not allowed to take the i_mutex here so we have to play games to
8932 * protect against truncate races as the page could now be beyond EOF. Because
8933 * vmtruncate() writes the inode size before removing pages, once we have the
8934 * page lock we can determine safely if the page is beyond EOF. If it is not
8935 * beyond EOF, then the page is guaranteed safe against truncation until we
8936 * unlock the page.
8937 */
Dave Jiang11bac802017-02-24 14:56:41 -08008938int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008939{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008940 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008941 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008942 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008943 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8944 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008945 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008946 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008947 char *kaddr;
8948 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008949 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008950 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008951 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308952 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008953 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008954 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308955 u64 end;
8956
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008957 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008958
Jan Karab2b5ef52012-06-12 16:20:45 +02008959 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008960 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008961 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308962 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008963
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308964 /*
8965 * Reserving delalloc space after obtaining the page lock can lead to
8966 * deadlock. For example, if a dirty page is locked by this function
8967 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8968 * dirty page write out, then the btrfs_writepage() function could
8969 * end up waiting indefinitely to get a lock on the page currently
8970 * being processed by btrfs_page_mkwrite() function.
8971 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08008972 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308973 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008974 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08008975 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008976 reserved = 1;
8977 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008978 if (ret) {
8979 if (ret == -ENOMEM)
8980 ret = VM_FAULT_OOM;
8981 else /* -ENOSPC, -EIO, etc */
8982 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008983 if (reserved)
8984 goto out;
8985 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008986 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008987
Nick Piggin56a76f82009-03-31 15:23:23 -07008988 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008989again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008990 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008991 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008992
Chris Mason9ebefb182007-06-15 13:50:00 -04008993 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008994 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008995 /* page got truncated out from underneath us */
8996 goto out_unlock;
8997 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008998 wait_on_page_writeback(page);
8999
David Sterbaff13db42015-12-03 14:30:40 +01009000 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009001 set_page_extent_mapped(page);
9002
Chris Masoneb84ae02008-07-17 13:53:27 -04009003 /*
9004 * we can't set the delalloc bits if there are pending ordered
9005 * extents. Drop our locks and wait for them to finish
9006 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009007 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9008 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009009 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009010 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009011 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009012 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009013 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009014 btrfs_put_ordered_extent(ordered);
9015 goto again;
9016 }
9017
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009018 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009019 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009020 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009021 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309022 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009023 btrfs_delalloc_release_space(inode, data_reserved,
9024 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309025 }
9026 }
9027
Josef Bacikfbf19082009-10-01 17:10:23 -04009028 /*
Liu Bo54160342016-12-13 12:15:19 -08009029 * page_mkwrite gets called when the page is firstly dirtied after it's
9030 * faulted in, but write(2) could also dirty a page and set delalloc
9031 * bits, thus in this case for space account reason, we still need to
9032 * clear any delalloc bits within this page range since we have to
9033 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009034 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309035 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009036 EXTENT_DIRTY | EXTENT_DELALLOC |
9037 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009038 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009039
Filipe Mananae3b8a482017-11-04 00:16:59 +00009040 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009041 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009042 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009043 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009044 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009045 ret = VM_FAULT_SIGBUS;
9046 goto out_unlock;
9047 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009048 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009049
9050 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009051 if (page_start + PAGE_SIZE > size)
9052 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009053 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009054 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009055
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009056 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009057 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009058 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009059 flush_dcache_page(page);
9060 kunmap(page);
9061 }
Chris Mason247e7432008-07-17 12:53:51 -04009062 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009063 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009064 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009065
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009066 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009067 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009068 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009069
David Sterbae43bbe52017-12-12 21:43:52 +01009070 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009071
9072out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009073 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009074 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009075 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009076 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009077 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009078 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009079 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009080out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009081 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009082 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9083 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009084out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009085 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009086 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009087 return ret;
9088}
9089
Josef Bacika41ad392011-01-31 15:30:16 -05009090static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009091{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009092 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009093 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009094 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009095 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009096 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009097 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009098 u64 mask = fs_info->sectorsize - 1;
9099 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009100
Josef Bacik0ef8b722013-10-25 16:13:35 -04009101 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9102 (u64)-1);
9103 if (ret)
9104 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009105
Josef Bacikfcb80c22011-05-03 10:40:22 -04009106 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009107 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009108 * 3 things going on here
9109 *
9110 * 1) We need to reserve space for our orphan item and the space to
9111 * delete our orphan item. Lord knows we don't want to have a dangling
9112 * orphan item because we didn't reserve space to remove it.
9113 *
9114 * 2) We need to reserve space to update our inode.
9115 *
9116 * 3) We need to have something to cache all the space that is going to
9117 * be free'd up by the truncate operation, but also have some slack
9118 * space reserved in case it uses space during the truncate (thank you
9119 * very much snapshotting).
9120 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009121 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009122 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009123 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009124 * doesn't end up using space reserved for updating the inode or
9125 * removing the orphan item. We also need to be able to stop the
9126 * transaction and start a new one, which means we need to be able to
9127 * update the inode several times, and we have no idea of knowing how
9128 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009129 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009130 * Then there is the orphan item, which does indeed need to be held on
9131 * to for the whole operation, and we need nobody to touch this reserved
9132 * space except the orphan code.
9133 *
9134 * So that leaves us with
9135 *
9136 * 1) root->orphan_block_rsv - for the orphan deletion.
9137 * 2) rsv - for the truncate reservation, which we will steal from the
9138 * transaction reservation.
9139 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9140 * updating the inode.
9141 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009142 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009143 if (!rsv)
9144 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009145 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009146 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009147
Josef Bacik907cbce2011-08-08 13:46:15 -04009148 /*
Josef Bacik07127182011-08-19 10:29:59 -04009149 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009150 * 1 for updating the inode.
9151 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009152 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009153 if (IS_ERR(trans)) {
9154 err = PTR_ERR(trans);
9155 goto out;
9156 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009157
Josef Bacik907cbce2011-08-08 13:46:15 -04009158 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009159 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009160 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009161 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009162
Chris Mason5a3f23d2009-03-31 13:27:11 -04009163 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009164 * So if we truncate and then write and fsync we normally would just
9165 * write the extents that changed, which is a problem if we need to
9166 * first truncate that entire inode. So set this flag so we write out
9167 * all of the extents in the inode to the sync log so we're completely
9168 * safe.
9169 */
9170 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009171 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009172
Yan, Zheng80825102009-11-12 09:35:36 +00009173 while (1) {
9174 ret = btrfs_truncate_inode_items(trans, root, inode,
9175 inode->i_size,
9176 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009177 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009178 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009179 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009180 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009181 }
Chris Mason39279cc2007-06-12 06:35:45 -04009182
Yan, Zheng80825102009-11-12 09:35:36 +00009183 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009184 if (ret) {
9185 err = ret;
9186 break;
9187 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009188
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009189 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009190 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009191
9192 trans = btrfs_start_transaction(root, 2);
9193 if (IS_ERR(trans)) {
9194 ret = err = PTR_ERR(trans);
9195 trans = NULL;
9196 break;
9197 }
9198
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009199 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009200 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009201 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009202 BUG_ON(ret); /* shouldn't happen */
9203 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009204 }
9205
Josef Bacikddfae632017-10-19 14:16:02 -04009206 /*
9207 * We can't call btrfs_truncate_block inside a trans handle as we could
9208 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9209 * we've truncated everything except the last little bit, and can do
9210 * btrfs_truncate_block and then update the disk_i_size.
9211 */
9212 if (ret == NEED_TRUNCATE_BLOCK) {
9213 btrfs_end_transaction(trans);
9214 btrfs_btree_balance_dirty(fs_info);
9215
9216 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9217 if (ret)
9218 goto out;
9219 trans = btrfs_start_transaction(root, 1);
9220 if (IS_ERR(trans)) {
9221 ret = PTR_ERR(trans);
9222 goto out;
9223 }
9224 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9225 }
9226
Yan, Zheng80825102009-11-12 09:35:36 +00009227 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009228 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009229 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009230 if (ret)
9231 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009232 }
9233
Chris Mason917c16b2011-11-08 14:49:59 -05009234 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009235 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009236 ret = btrfs_update_inode(trans, root, inode);
9237 if (ret && !err)
9238 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009239
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009240 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009241 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009242 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009243out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009244 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009245
Josef Bacik3893e332011-01-31 16:03:11 -05009246 if (ret && !err)
9247 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009248
Josef Bacik3893e332011-01-31 16:03:11 -05009249 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009250}
9251
Sven Wegener3b963622008-06-09 21:57:42 -04009252/*
Chris Masond352ac62008-09-29 15:18:18 -04009253 * create a new subvolume directory/inode (helper for the ioctl).
9254 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009255int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009256 struct btrfs_root *new_root,
9257 struct btrfs_root *parent_root,
9258 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009259{
Chris Mason39279cc2007-06-12 06:35:45 -04009260 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009261 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009262 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009263
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009264 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9265 new_dirid, new_dirid,
9266 S_IFDIR | (~current_umask() & S_IRWXUGO),
9267 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009268 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009269 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009270 inode->i_op = &btrfs_dir_inode_operations;
9271 inode->i_fop = &btrfs_dir_file_operations;
9272
Miklos Szeredibfe86842011-10-28 14:13:29 +02009273 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009274 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009275 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009276
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009277 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9278 if (err)
9279 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009280 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009281 new_root->root_key.objectid, err);
9282
Yan, Zheng76dda932009-09-21 16:00:26 -04009283 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009284
Yan, Zheng76dda932009-09-21 16:00:26 -04009285 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009286 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009287}
9288
Chris Mason39279cc2007-06-12 06:35:45 -04009289struct inode *btrfs_alloc_inode(struct super_block *sb)
9290{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009291 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009292 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009293 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009294
David Sterba712e36c2017-10-31 17:08:27 +01009295 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009296 if (!ei)
9297 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009298
9299 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009300 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009301 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009302 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009303 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009304 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009305 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009306 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009307 ei->disk_i_size = 0;
9308 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009309 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009310 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009311 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009312 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009313 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009314 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009315
Josef Bacik9e0baf62011-07-15 15:16:44 +00009316 spin_lock_init(&ei->lock);
9317 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009318 if (sb->s_magic != BTRFS_TEST_MAGIC)
9319 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9320 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009321 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009322 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009323 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009324
Miao Xie16cdcec2011-04-22 18:12:22 +08009325 ei->delayed_node = NULL;
9326
chandan r9cc97d62012-07-04 12:48:07 +05309327 ei->i_otime.tv_sec = 0;
9328 ei->i_otime.tv_nsec = 0;
9329
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009330 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009331 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009332 extent_io_tree_init(&ei->io_tree, inode);
9333 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009334 ei->io_tree.track_uptodate = 1;
9335 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009336 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009337 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009338 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009339 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009340 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009341 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009342 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009343 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009344
9345 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009346}
9347
Josef Bacikaaedb552013-10-11 14:44:09 -04009348#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9349void btrfs_test_destroy_inode(struct inode *inode)
9350{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009351 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009352 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9353}
9354#endif
9355
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009356static void btrfs_i_callback(struct rcu_head *head)
9357{
9358 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009359 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9360}
9361
Chris Mason39279cc2007-06-12 06:35:45 -04009362void btrfs_destroy_inode(struct inode *inode)
9363{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009364 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009365 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009366 struct btrfs_root *root = BTRFS_I(inode)->root;
9367
Al Virob3d9b7a2012-06-09 13:51:19 -04009368 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009369 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009370 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9371 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009372 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009373 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009374 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009375 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009376 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009377
Chris Mason5a3f23d2009-03-31 13:27:11 -04009378 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009379 * This can happen where we create an inode, but somebody else also
9380 * created the same inode and we need to destroy the one we already
9381 * created.
9382 */
9383 if (!root)
9384 goto free;
9385
Josef Bacik8a35d952012-05-23 14:26:42 -04009386 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9387 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009388 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009389 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009390 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009391 }
Josef Bacik7b128762008-07-24 12:17:14 -04009392
Chris Masond3977122009-01-05 21:25:51 -05009393 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009394 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9395 if (!ordered)
9396 break;
9397 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009398 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009399 "found ordered extent %llu %llu on inode cleanup",
9400 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009401 btrfs_remove_ordered_extent(inode, ordered);
9402 btrfs_put_ordered_extent(ordered);
9403 btrfs_put_ordered_extent(ordered);
9404 }
9405 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009406 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009407 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009408 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009409free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009410 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009411}
9412
Al Viro45321ac2010-06-07 13:43:19 -04009413int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009414{
9415 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009416
Naohiro Aota6379ef92013-06-06 09:56:34 +00009417 if (root == NULL)
9418 return 1;
9419
Liu Bofa6ac872013-02-20 14:10:23 +00009420 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009421 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009422 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009423 else
Al Viro45321ac2010-06-07 13:43:19 -04009424 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009425}
9426
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009427static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009428{
9429 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9430
9431 inode_init_once(&ei->vfs_inode);
9432}
9433
9434void btrfs_destroy_cachep(void)
9435{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009436 /*
9437 * Make sure all delayed rcu free inodes are flushed before we
9438 * destroy cache.
9439 */
9440 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009441 kmem_cache_destroy(btrfs_inode_cachep);
9442 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009443 kmem_cache_destroy(btrfs_path_cachep);
9444 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009445}
9446
Liu Bof5c29bd2017-11-02 17:21:50 -06009447int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009448{
David Sterba837e1972012-09-07 03:00:48 -06009449 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009450 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009451 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9452 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009453 if (!btrfs_inode_cachep)
9454 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009455
David Sterba837e1972012-09-07 03:00:48 -06009456 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009457 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009458 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009459 if (!btrfs_trans_handle_cachep)
9460 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009461
David Sterba837e1972012-09-07 03:00:48 -06009462 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009463 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009464 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009465 if (!btrfs_path_cachep)
9466 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009467
David Sterba837e1972012-09-07 03:00:48 -06009468 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009469 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009470 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009471 if (!btrfs_free_space_cachep)
9472 goto fail;
9473
Chris Mason39279cc2007-06-12 06:35:45 -04009474 return 0;
9475fail:
9476 btrfs_destroy_cachep();
9477 return -ENOMEM;
9478}
9479
David Howellsa528d352017-01-31 16:46:22 +00009480static int btrfs_getattr(const struct path *path, struct kstat *stat,
9481 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009482{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009483 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009484 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009485 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009486 u32 bi_flags = BTRFS_I(inode)->flags;
9487
9488 stat->result_mask |= STATX_BTIME;
9489 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9490 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9491 if (bi_flags & BTRFS_INODE_APPEND)
9492 stat->attributes |= STATX_ATTR_APPEND;
9493 if (bi_flags & BTRFS_INODE_COMPRESS)
9494 stat->attributes |= STATX_ATTR_COMPRESSED;
9495 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9496 stat->attributes |= STATX_ATTR_IMMUTABLE;
9497 if (bi_flags & BTRFS_INODE_NODUMP)
9498 stat->attributes |= STATX_ATTR_NODUMP;
9499
9500 stat->attributes_mask |= (STATX_ATTR_APPEND |
9501 STATX_ATTR_COMPRESSED |
9502 STATX_ATTR_IMMUTABLE |
9503 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009504
Chris Mason39279cc2007-06-12 06:35:45 -04009505 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009506 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009507
9508 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009509 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009510 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009511 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009512 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009513 return 0;
9514}
9515
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009516static int btrfs_rename_exchange(struct inode *old_dir,
9517 struct dentry *old_dentry,
9518 struct inode *new_dir,
9519 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009520{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009521 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009522 struct btrfs_trans_handle *trans;
9523 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009524 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009525 struct inode *new_inode = new_dentry->d_inode;
9526 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009527 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009528 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009529 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9530 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009531 u64 old_idx = 0;
9532 u64 new_idx = 0;
9533 u64 root_objectid;
9534 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009535 bool root_log_pinned = false;
9536 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009537
9538 /* we only allow rename subvolume link between subvolumes */
9539 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9540 return -EXDEV;
9541
9542 /* close the race window with snapshot create/destroy ioctl */
9543 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009544 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009545 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009546 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009547
9548 /*
9549 * We want to reserve the absolute worst case amount of items. So if
9550 * both inodes are subvols and we need to unlink them then that would
9551 * require 4 item modifications, but if they are both normal inodes it
9552 * would require 5 item modifications, so we'll assume their normal
9553 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9554 * should cover the worst case number of items we'll modify.
9555 */
9556 trans = btrfs_start_transaction(root, 12);
9557 if (IS_ERR(trans)) {
9558 ret = PTR_ERR(trans);
9559 goto out_notrans;
9560 }
9561
9562 /*
9563 * We need to find a free sequence number both in the source and
9564 * in the destination directory for the exchange.
9565 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009566 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009567 if (ret)
9568 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009569 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009570 if (ret)
9571 goto out_fail;
9572
9573 BTRFS_I(old_inode)->dir_index = 0ULL;
9574 BTRFS_I(new_inode)->dir_index = 0ULL;
9575
9576 /* Reference for the source. */
9577 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9578 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009579 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009580 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009581 btrfs_pin_log_trans(root);
9582 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009583 ret = btrfs_insert_inode_ref(trans, dest,
9584 new_dentry->d_name.name,
9585 new_dentry->d_name.len,
9586 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009587 btrfs_ino(BTRFS_I(new_dir)),
9588 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009589 if (ret)
9590 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009591 }
9592
9593 /* And now for the dest. */
9594 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9595 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009596 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009597 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009598 btrfs_pin_log_trans(dest);
9599 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009600 ret = btrfs_insert_inode_ref(trans, root,
9601 old_dentry->d_name.name,
9602 old_dentry->d_name.len,
9603 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009604 btrfs_ino(BTRFS_I(old_dir)),
9605 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009606 if (ret)
9607 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009608 }
9609
9610 /* Update inode version and ctime/mtime. */
9611 inode_inc_iversion(old_dir);
9612 inode_inc_iversion(new_dir);
9613 inode_inc_iversion(old_inode);
9614 inode_inc_iversion(new_inode);
9615 old_dir->i_ctime = old_dir->i_mtime = ctime;
9616 new_dir->i_ctime = new_dir->i_mtime = ctime;
9617 old_inode->i_ctime = ctime;
9618 new_inode->i_ctime = ctime;
9619
9620 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009621 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9622 BTRFS_I(old_inode), 1);
9623 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9624 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009625 }
9626
9627 /* src is a subvolume */
9628 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9629 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9630 ret = btrfs_unlink_subvol(trans, root, old_dir,
9631 root_objectid,
9632 old_dentry->d_name.name,
9633 old_dentry->d_name.len);
9634 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009635 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9636 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009637 old_dentry->d_name.name,
9638 old_dentry->d_name.len);
9639 if (!ret)
9640 ret = btrfs_update_inode(trans, root, old_inode);
9641 }
9642 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009643 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009644 goto out_fail;
9645 }
9646
9647 /* dest is a subvolume */
9648 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9649 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9650 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9651 root_objectid,
9652 new_dentry->d_name.name,
9653 new_dentry->d_name.len);
9654 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009655 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9656 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009657 new_dentry->d_name.name,
9658 new_dentry->d_name.len);
9659 if (!ret)
9660 ret = btrfs_update_inode(trans, dest, new_inode);
9661 }
9662 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009663 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664 goto out_fail;
9665 }
9666
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009667 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009668 new_dentry->d_name.name,
9669 new_dentry->d_name.len, 0, old_idx);
9670 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009671 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009672 goto out_fail;
9673 }
9674
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009675 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009676 old_dentry->d_name.name,
9677 old_dentry->d_name.len, 0, new_idx);
9678 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009679 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009680 goto out_fail;
9681 }
9682
9683 if (old_inode->i_nlink == 1)
9684 BTRFS_I(old_inode)->dir_index = old_idx;
9685 if (new_inode->i_nlink == 1)
9686 BTRFS_I(new_inode)->dir_index = new_idx;
9687
Filipe Manana86e8aa02016-05-05 02:02:27 +01009688 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009689 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009690 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9691 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009692 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009693 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009694 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009695 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009696 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009697 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9698 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009699 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009700 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009701 }
9702out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009703 /*
9704 * If we have pinned a log and an error happened, we unpin tasks
9705 * trying to sync the log and force them to fallback to a transaction
9706 * commit if the log currently contains any of the inodes involved in
9707 * this rename operation (to ensure we do not persist a log with an
9708 * inconsistent state for any of these inodes or leading to any
9709 * inconsistencies when replayed). If the transaction was aborted, the
9710 * abortion reason is propagated to userspace when attempting to commit
9711 * the transaction. If the log does not contain any of these inodes, we
9712 * allow the tasks to sync it.
9713 */
9714 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009715 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9716 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9717 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009718 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009719 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009720 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009721
9722 if (root_log_pinned) {
9723 btrfs_end_log_trans(root);
9724 root_log_pinned = false;
9725 }
9726 if (dest_log_pinned) {
9727 btrfs_end_log_trans(dest);
9728 dest_log_pinned = false;
9729 }
9730 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009731 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009732out_notrans:
9733 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009734 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009735 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009736 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009737
9738 return ret;
9739}
9740
9741static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9742 struct btrfs_root *root,
9743 struct inode *dir,
9744 struct dentry *dentry)
9745{
9746 int ret;
9747 struct inode *inode;
9748 u64 objectid;
9749 u64 index;
9750
9751 ret = btrfs_find_free_ino(root, &objectid);
9752 if (ret)
9753 return ret;
9754
9755 inode = btrfs_new_inode(trans, root, dir,
9756 dentry->d_name.name,
9757 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009758 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009759 objectid,
9760 S_IFCHR | WHITEOUT_MODE,
9761 &index);
9762
9763 if (IS_ERR(inode)) {
9764 ret = PTR_ERR(inode);
9765 return ret;
9766 }
9767
9768 inode->i_op = &btrfs_special_inode_operations;
9769 init_special_inode(inode, inode->i_mode,
9770 WHITEOUT_DEV);
9771
9772 ret = btrfs_init_inode_security(trans, inode, dir,
9773 &dentry->d_name);
9774 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009775 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009776
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009777 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9778 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009779 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009780 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009781
9782 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009783out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009784 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009785 if (ret)
9786 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009787 iput(inode);
9788
Filipe Mananac9901612016-05-05 01:41:57 +01009789 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009790}
9791
Chris Mason39279cc2007-06-12 06:35:45 -04009792static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009793 struct inode *new_dir, struct dentry *new_dentry,
9794 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009795{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009796 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009797 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009798 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009799 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9800 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009801 struct inode *new_inode = d_inode(new_dentry);
9802 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009803 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009804 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009805 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009806 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009807 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009808
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009809 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009810 return -EPERM;
9811
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009812 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009813 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009814 return -EXDEV;
9815
Li Zefan33345d012011-04-20 10:31:50 +08009816 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009817 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009818 return -ENOTEMPTY;
9819
Chris Mason39279cc2007-06-12 06:35:45 -04009820 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009821 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009822 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009823
9824
9825 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009826 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009827 new_dentry->d_name.name,
9828 new_dentry->d_name.len);
9829
9830 if (ret) {
9831 if (ret == -EEXIST) {
9832 /* we shouldn't get
9833 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309834 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009835 return ret;
9836 }
9837 } else {
9838 /* maybe -EOVERFLOW */
9839 return ret;
9840 }
9841 }
9842 ret = 0;
9843
Chris Mason5a3f23d2009-03-31 13:27:11 -04009844 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009845 * we're using rename to replace one file with another. Start IO on it
9846 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009847 */
Chris Mason8d875f92014-08-12 10:47:42 -07009848 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009849 filemap_flush(old_inode->i_mapping);
9850
Yan, Zheng76dda932009-09-21 16:00:26 -04009851 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009852 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009853 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009854 /*
9855 * We want to reserve the absolute worst case amount of items. So if
9856 * both inodes are subvols and we need to unlink them then that would
9857 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009858 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009859 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9860 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009861 * If our rename has the whiteout flag, we need more 5 units for the
9862 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9863 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009864 */
Filipe Manana5062af32016-05-05 10:26:26 +01009865 trans_num_items = 11;
9866 if (flags & RENAME_WHITEOUT)
9867 trans_num_items += 5;
9868 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009869 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009870 ret = PTR_ERR(trans);
9871 goto out_notrans;
9872 }
Chris Mason5f39d392007-10-15 16:14:19 -04009873
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009874 if (dest != root)
9875 btrfs_record_root_in_trans(trans, dest);
9876
Nikolay Borisov877574e2017-02-20 13:50:33 +02009877 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009878 if (ret)
9879 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009880
Miao Xie67de1172013-12-26 13:07:06 +08009881 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009882 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009883 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009884 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009885 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009886 btrfs_pin_log_trans(root);
9887 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009888 ret = btrfs_insert_inode_ref(trans, dest,
9889 new_dentry->d_name.name,
9890 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009891 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009892 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009893 if (ret)
9894 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009895 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009896
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009897 inode_inc_iversion(old_dir);
9898 inode_inc_iversion(new_dir);
9899 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009900 old_dir->i_ctime = old_dir->i_mtime =
9901 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009902 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009903
Chris Mason12fcfd22009-03-24 10:24:20 -04009904 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009905 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9906 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009907
Li Zefan33345d012011-04-20 10:31:50 +08009908 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009909 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9910 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9911 old_dentry->d_name.name,
9912 old_dentry->d_name.len);
9913 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009914 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9915 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009916 old_dentry->d_name.name,
9917 old_dentry->d_name.len);
9918 if (!ret)
9919 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009920 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009921 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009922 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009923 goto out_fail;
9924 }
Chris Mason39279cc2007-06-12 06:35:45 -04009925
9926 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009927 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009928 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009929 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009930 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9931 root_objectid = BTRFS_I(new_inode)->location.objectid;
9932 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9933 root_objectid,
9934 new_dentry->d_name.name,
9935 new_dentry->d_name.len);
9936 BUG_ON(new_inode->i_nlink == 0);
9937 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009938 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9939 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009940 new_dentry->d_name.name,
9941 new_dentry->d_name.len);
9942 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009943 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009944 ret = btrfs_orphan_add(trans,
9945 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009946 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009947 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009948 goto out_fail;
9949 }
Chris Mason39279cc2007-06-12 06:35:45 -04009950 }
Josef Bacikaec74772008-07-24 12:12:38 -04009951
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009952 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009953 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009954 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009955 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009956 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009957 goto out_fail;
9958 }
Chris Mason39279cc2007-06-12 06:35:45 -04009959
Miao Xie67de1172013-12-26 13:07:06 +08009960 if (old_inode->i_nlink == 1)
9961 BTRFS_I(old_inode)->dir_index = index;
9962
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009963 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009964 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009965
David Sterbaf85b7372017-01-20 14:54:07 +01009966 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9967 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009968 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009969 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009970 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009971
9972 if (flags & RENAME_WHITEOUT) {
9973 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9974 old_dentry);
9975
9976 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009977 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009978 goto out_fail;
9979 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009980 }
Chris Mason39279cc2007-06-12 06:35:45 -04009981out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009982 /*
9983 * If we have pinned the log and an error happened, we unpin tasks
9984 * trying to sync the log and force them to fallback to a transaction
9985 * commit if the log currently contains any of the inodes involved in
9986 * this rename operation (to ensure we do not persist a log with an
9987 * inconsistent state for any of these inodes or leading to any
9988 * inconsistencies when replayed). If the transaction was aborted, the
9989 * abortion reason is propagated to userspace when attempting to commit
9990 * the transaction. If the log does not contain any of these inodes, we
9991 * allow the tasks to sync it.
9992 */
9993 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009994 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9995 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9996 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009997 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009998 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009999 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010000
10001 btrfs_end_log_trans(root);
10002 log_pinned = false;
10003 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010004 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010005out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010006 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010007 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010008
Chris Mason39279cc2007-06-12 06:35:45 -040010009 return ret;
10010}
10011
Miklos Szeredi80ace852014-07-23 15:15:32 +020010012static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10013 struct inode *new_dir, struct dentry *new_dentry,
10014 unsigned int flags)
10015{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010016 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010017 return -EINVAL;
10018
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010019 if (flags & RENAME_EXCHANGE)
10020 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10021 new_dentry);
10022
10023 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010024}
10025
Miao Xie8ccf6f192012-10-25 09:28:04 +000010026static void btrfs_run_delalloc_work(struct btrfs_work *work)
10027{
10028 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010029 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010030
10031 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10032 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010033 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010034 filemap_flush(inode->i_mapping);
10035 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10036 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010037 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010038
10039 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010040 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010041 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010042 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010043 complete(&delalloc_work->completion);
10044}
10045
10046struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010047 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010048{
10049 struct btrfs_delalloc_work *work;
10050
David Sterba100d5702015-12-08 14:39:32 +010010051 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010052 if (!work)
10053 return NULL;
10054
10055 init_completion(&work->completion);
10056 INIT_LIST_HEAD(&work->list);
10057 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010058 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010059 WARN_ON_ONCE(!inode);
10060 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10061 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010062
10063 return work;
10064}
10065
10066void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10067{
10068 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010069 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010070}
10071
Chris Masond352ac62008-09-29 15:18:18 -040010072/*
10073 * some fairly slow code that needs optimization. This walks the list
10074 * of all the inodes with pending delalloc and forces them to disk.
10075 */
Miao Xie6c255e62014-03-06 13:55:01 +080010076static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10077 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010078{
Chris Masonea8c2812008-08-04 23:17:27 -040010079 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010080 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010081 struct btrfs_delalloc_work *work, *next;
10082 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010083 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010084 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010085
Miao Xie8ccf6f192012-10-25 09:28:04 +000010086 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010087 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010088
Miao Xie573bfb72014-03-06 13:55:03 +080010089 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010090 spin_lock(&root->delalloc_lock);
10091 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010092 while (!list_empty(&splice)) {
10093 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010094 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010095
Miao Xieeb73c1b2013-05-15 07:48:22 +000010096 list_move_tail(&binode->delalloc_inodes,
10097 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010098 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010099 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010100 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010101 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010102 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010103 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010104
David Sterba651d4942015-11-27 19:24:16 +010010105 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010106 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010107 if (delay_iput)
10108 btrfs_add_delayed_iput(inode);
10109 else
10110 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010111 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010112 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010113 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010114 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010115 btrfs_queue_work(root->fs_info->flush_workers,
10116 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010117 ret++;
10118 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010119 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010120 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010121 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010122 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010123 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010124
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010125out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010126 list_for_each_entry_safe(work, next, &works, list) {
10127 list_del_init(&work->list);
10128 btrfs_wait_and_free_delalloc_work(work);
10129 }
10130
Miao Xieeb73c1b2013-05-15 07:48:22 +000010131 if (!list_empty_careful(&splice)) {
10132 spin_lock(&root->delalloc_lock);
10133 list_splice_tail(&splice, &root->delalloc_inodes);
10134 spin_unlock(&root->delalloc_lock);
10135 }
Miao Xie573bfb72014-03-06 13:55:03 +080010136 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010137 return ret;
10138}
10139
10140int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10141{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010142 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010143 int ret;
10144
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010145 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010146 return -EROFS;
10147
Miao Xie6c255e62014-03-06 13:55:01 +080010148 ret = __start_delalloc_inodes(root, delay_iput, -1);
10149 if (ret > 0)
10150 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010151 return ret;
10152}
10153
Miao Xie6c255e62014-03-06 13:55:01 +080010154int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10155 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010156{
10157 struct btrfs_root *root;
10158 struct list_head splice;
10159 int ret;
10160
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010161 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010162 return -EROFS;
10163
10164 INIT_LIST_HEAD(&splice);
10165
Miao Xie573bfb72014-03-06 13:55:03 +080010166 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010167 spin_lock(&fs_info->delalloc_root_lock);
10168 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010169 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010170 root = list_first_entry(&splice, struct btrfs_root,
10171 delalloc_root);
10172 root = btrfs_grab_fs_root(root);
10173 BUG_ON(!root);
10174 list_move_tail(&root->delalloc_root,
10175 &fs_info->delalloc_roots);
10176 spin_unlock(&fs_info->delalloc_root_lock);
10177
Miao Xie6c255e62014-03-06 13:55:01 +080010178 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010179 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010180 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010181 goto out;
10182
Miao Xie6c255e62014-03-06 13:55:01 +080010183 if (nr != -1) {
10184 nr -= ret;
10185 WARN_ON(nr < 0);
10186 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010187 spin_lock(&fs_info->delalloc_root_lock);
10188 }
10189 spin_unlock(&fs_info->delalloc_root_lock);
10190
Miao Xie6c255e62014-03-06 13:55:01 +080010191 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010192out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010193 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010194 spin_lock(&fs_info->delalloc_root_lock);
10195 list_splice_tail(&splice, &fs_info->delalloc_roots);
10196 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010197 }
Miao Xie573bfb72014-03-06 13:55:03 +080010198 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010199 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010200}
10201
Chris Mason39279cc2007-06-12 06:35:45 -040010202static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10203 const char *symname)
10204{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010205 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010206 struct btrfs_trans_handle *trans;
10207 struct btrfs_root *root = BTRFS_I(dir)->root;
10208 struct btrfs_path *path;
10209 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010210 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010211 int err;
10212 int drop_inode = 0;
10213 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010214 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010215 int name_len;
10216 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010217 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010218 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010219 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010220
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010221 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010222 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010223 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010224
Josef Bacik9ed74f22009-09-11 16:12:44 -040010225 /*
10226 * 2 items for inode item and ref
10227 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010228 * 1 item for updating parent inode item
10229 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010230 * 1 item for xattr if selinux is on
10231 */
Filipe Manana9269d122015-12-31 18:16:29 +000010232 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010233 if (IS_ERR(trans))
10234 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010235
Li Zefan581bb052011-04-20 10:06:11 +080010236 err = btrfs_find_free_ino(root, &objectid);
10237 if (err)
10238 goto out_unlock;
10239
Josef Bacikaec74772008-07-24 12:12:38 -040010240 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010241 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10242 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010243 if (IS_ERR(inode)) {
10244 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010245 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010246 }
Chris Mason39279cc2007-06-12 06:35:45 -040010247
Casey Schauflerad19db72011-12-15 10:09:07 -050010248 /*
10249 * If the active LSM wants to access the inode during
10250 * d_instantiate it needs these. Smack checks to see
10251 * if the filesystem supports xattrs by looking at the
10252 * ops vector.
10253 */
10254 inode->i_fop = &btrfs_file_operations;
10255 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010256 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010257 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10258
10259 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10260 if (err)
10261 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010262
Chris Mason39279cc2007-06-12 06:35:45 -040010263 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010264 if (!path) {
10265 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010266 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010267 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010268 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010269 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010270 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010271 datasize = btrfs_file_extent_calc_inline_size(name_len);
10272 err = btrfs_insert_empty_item(trans, root, path, &key,
10273 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010274 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010275 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010276 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010277 }
Chris Mason5f39d392007-10-15 16:14:19 -040010278 leaf = path->nodes[0];
10279 ei = btrfs_item_ptr(leaf, path->slots[0],
10280 struct btrfs_file_extent_item);
10281 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10282 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010283 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010284 btrfs_set_file_extent_encryption(leaf, ei, 0);
10285 btrfs_set_file_extent_compression(leaf, ei, 0);
10286 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10287 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10288
Chris Mason39279cc2007-06-12 06:35:45 -040010289 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010290 write_extent_buffer(leaf, symname, ptr, name_len);
10291 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010292 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010293
Chris Mason39279cc2007-06-12 06:35:45 -040010294 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010295 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010296 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010297 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010298 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010299 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010300 /*
10301 * Last step, add directory indexes for our symlink inode. This is the
10302 * last step to avoid extra cleanup of these indexes if an error happens
10303 * elsewhere above.
10304 */
10305 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010306 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10307 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010308 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010309 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010310 goto out_unlock_inode;
10311 }
10312
10313 unlock_new_inode(inode);
10314 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010315
10316out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010317 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010318 if (drop_inode) {
10319 inode_dec_link_count(inode);
10320 iput(inode);
10321 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010322 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010323 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010324
10325out_unlock_inode:
10326 drop_inode = 1;
10327 unlock_new_inode(inode);
10328 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010329}
Chris Mason16432982008-04-10 10:23:21 -040010330
Josef Bacik0af3d002010-06-21 14:48:16 -040010331static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10332 u64 start, u64 num_bytes, u64 min_size,
10333 loff_t actual_len, u64 *alloc_hint,
10334 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010335{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010336 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010337 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10338 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010339 struct btrfs_root *root = BTRFS_I(inode)->root;
10340 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010341 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010342 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010343 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010344 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010345 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010346 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010347 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010348
Josef Bacik0af3d002010-06-21 14:48:16 -040010349 if (trans)
10350 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010351 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010352 if (own_trans) {
10353 trans = btrfs_start_transaction(root, 3);
10354 if (IS_ERR(trans)) {
10355 ret = PTR_ERR(trans);
10356 break;
10357 }
Yan Zhengd899e052008-10-30 14:25:28 -040010358 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010359
Byongho Leeee221842015-12-15 01:42:10 +090010360 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010361 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010362 /*
10363 * If we are severely fragmented we could end up with really
10364 * small allocations, so if the allocator is returning small
10365 * chunks lets make its job easier by only searching for those
10366 * sized chunks.
10367 */
10368 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010369 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10370 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010371 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010372 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010373 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010374 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010375 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010376 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010377
Josef Bacik0b670dc2015-09-23 17:11:16 -040010378 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010379 ret = insert_reserved_file_extent(trans, inode,
10380 cur_offset, ins.objectid,
10381 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010382 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010383 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010384 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010385 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010386 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010387 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010388 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010389 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010390 break;
10391 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010392
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010393 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010394 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010395
Josef Bacik5dc562c2012-08-17 13:14:17 -040010396 em = alloc_extent_map();
10397 if (!em) {
10398 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10399 &BTRFS_I(inode)->runtime_flags);
10400 goto next;
10401 }
10402
10403 em->start = cur_offset;
10404 em->orig_start = cur_offset;
10405 em->len = ins.offset;
10406 em->block_start = ins.objectid;
10407 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010408 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010409 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010410 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010411 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10412 em->generation = trans->transid;
10413
10414 while (1) {
10415 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010416 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010417 write_unlock(&em_tree->lock);
10418 if (ret != -EEXIST)
10419 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010420 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010421 cur_offset + ins.offset - 1,
10422 0);
10423 }
10424 free_extent_map(em);
10425next:
Yan Zhengd899e052008-10-30 14:25:28 -040010426 num_bytes -= ins.offset;
10427 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010428 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010429
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010430 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010431 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010432 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010433 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010434 (actual_len > inode->i_size) &&
10435 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010436 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010437 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010438 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010439 i_size = cur_offset;
10440 i_size_write(inode, i_size);
10441 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010442 }
10443
Yan Zhengd899e052008-10-30 14:25:28 -040010444 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010445
10446 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010447 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010448 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010449 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010450 break;
10451 }
Yan Zhengd899e052008-10-30 14:25:28 -040010452
Josef Bacik0af3d002010-06-21 14:48:16 -040010453 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010454 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010455 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010456 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010457 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010458 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010459 return ret;
10460}
10461
Josef Bacik0af3d002010-06-21 14:48:16 -040010462int btrfs_prealloc_file_range(struct inode *inode, int mode,
10463 u64 start, u64 num_bytes, u64 min_size,
10464 loff_t actual_len, u64 *alloc_hint)
10465{
10466 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10467 min_size, actual_len, alloc_hint,
10468 NULL);
10469}
10470
10471int btrfs_prealloc_file_range_trans(struct inode *inode,
10472 struct btrfs_trans_handle *trans, int mode,
10473 u64 start, u64 num_bytes, u64 min_size,
10474 loff_t actual_len, u64 *alloc_hint)
10475{
10476 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10477 min_size, actual_len, alloc_hint, trans);
10478}
10479
Chris Masone6dcd2d2008-07-17 12:53:50 -040010480static int btrfs_set_page_dirty(struct page *page)
10481{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010482 return __set_page_dirty_nobuffers(page);
10483}
10484
Al Viro10556cb22011-06-20 19:28:19 -040010485static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010486{
Li Zefanb83cc962010-12-20 16:04:08 +080010487 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010488 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010489
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010490 if (mask & MAY_WRITE &&
10491 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10492 if (btrfs_root_readonly(root))
10493 return -EROFS;
10494 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10495 return -EACCES;
10496 }
Al Viro2830ba72011-06-20 19:16:29 -040010497 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010498}
Chris Mason39279cc2007-06-12 06:35:45 -040010499
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010500static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10501{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010502 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010503 struct btrfs_trans_handle *trans;
10504 struct btrfs_root *root = BTRFS_I(dir)->root;
10505 struct inode *inode = NULL;
10506 u64 objectid;
10507 u64 index;
10508 int ret = 0;
10509
10510 /*
10511 * 5 units required for adding orphan entry
10512 */
10513 trans = btrfs_start_transaction(root, 5);
10514 if (IS_ERR(trans))
10515 return PTR_ERR(trans);
10516
10517 ret = btrfs_find_free_ino(root, &objectid);
10518 if (ret)
10519 goto out;
10520
10521 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010522 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010523 if (IS_ERR(inode)) {
10524 ret = PTR_ERR(inode);
10525 inode = NULL;
10526 goto out;
10527 }
10528
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010529 inode->i_fop = &btrfs_file_operations;
10530 inode->i_op = &btrfs_file_inode_operations;
10531
10532 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010533 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10534
Chris Masonb0d5d102014-09-08 13:08:51 -070010535 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10536 if (ret)
10537 goto out_inode;
10538
10539 ret = btrfs_update_inode(trans, root, inode);
10540 if (ret)
10541 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010542 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010543 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010544 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010545
Filipe Manana5762b5c2014-08-01 00:10:32 +010010546 /*
10547 * We set number of links to 0 in btrfs_new_inode(), and here we set
10548 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10549 * through:
10550 *
10551 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10552 */
10553 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010554 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010555 d_tmpfile(dentry, inode);
10556 mark_inode_dirty(inode);
10557
10558out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010559 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010560 if (ret)
10561 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010562 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010563 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010564
10565out_inode:
10566 unlock_new_inode(inode);
10567 goto out;
10568
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010569}
10570
David Sterba20a7db82017-02-17 16:24:29 +010010571__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010572static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010573{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010574 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010575}
10576
Josef Bacikc6100a42017-05-05 11:57:13 -040010577static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10578{
10579 struct inode *inode = private_data;
10580 return btrfs_sb(inode->i_sb);
10581}
10582
10583static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10584 u64 start, u64 end)
10585{
10586 struct inode *inode = private_data;
10587 u64 isize;
10588
10589 isize = i_size_read(inode);
10590 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10591 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10592 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10593 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10594 }
10595}
10596
10597void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10598{
10599 struct inode *inode = private_data;
10600 unsigned long index = start >> PAGE_SHIFT;
10601 unsigned long end_index = end >> PAGE_SHIFT;
10602 struct page *page;
10603
10604 while (index <= end_index) {
10605 page = find_get_page(inode->i_mapping, index);
10606 ASSERT(page); /* Pages should be in the extent_io_tree */
10607 set_page_writeback(page);
10608 put_page(page);
10609 index++;
10610 }
10611}
10612
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010613static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010614 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010615 .lookup = btrfs_lookup,
10616 .create = btrfs_create,
10617 .unlink = btrfs_unlink,
10618 .link = btrfs_link,
10619 .mkdir = btrfs_mkdir,
10620 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010621 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010622 .symlink = btrfs_symlink,
10623 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010624 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010625 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010626 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010627 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010628 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010629 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010630 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010631};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010632static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010633 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010634 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010635 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010636};
Yan, Zheng76dda932009-09-21 16:00:26 -040010637
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010638static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010639 .llseek = generic_file_llseek,
10640 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010641 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010642 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010643 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010644#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010645 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010646#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010647 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010648 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010649};
10650
David Sterba20e55062015-11-19 11:42:28 +010010651static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010652 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010653 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010654 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010655 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010656 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010657 .tree_fs_info = iotree_fs_info,
10658 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010659
10660 /* optional callbacks */
10661 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010662 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010663 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010664 .set_bit_hook = btrfs_set_bit_hook,
10665 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010666 .merge_extent_hook = btrfs_merge_extent_hook,
10667 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010668 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010669};
10670
Chris Mason35054392009-01-21 13:11:13 -050010671/*
10672 * btrfs doesn't support the bmap operation because swapfiles
10673 * use bmap to make a mapping of extents in the file. They assume
10674 * these extents won't change over the life of the file and they
10675 * use the bmap result to do IO directly to the drive.
10676 *
10677 * the btrfs bmap call would return logical addresses that aren't
10678 * suitable for IO and they also will change frequently as COW
10679 * operations happen. So, swapfile + btrfs == corruption.
10680 *
10681 * For now we're avoiding this by dropping bmap.
10682 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010683static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010684 .readpage = btrfs_readpage,
10685 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010686 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010687 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010688 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010689 .invalidatepage = btrfs_invalidatepage,
10690 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010691 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010692 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010693};
10694
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010695static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010696 .readpage = btrfs_readpage,
10697 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010698 .invalidatepage = btrfs_invalidatepage,
10699 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010700};
10701
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010702static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010703 .getattr = btrfs_getattr,
10704 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010705 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010706 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010707 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010708 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010709 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010710 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010711};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010712static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010713 .getattr = btrfs_getattr,
10714 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010715 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010716 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010717 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010718 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010719 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010720};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010721static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010722 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010723 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010724 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010725 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010726 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010727 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010728};
Yan, Zheng76dda932009-09-21 16:00:26 -040010729
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010730const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010731 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010732 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010733};