blob: bacf71ccf0f2944ad6c479f9161142df6d0702c2 [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,
2103 page_end, &cached_state, GFP_NOFS);
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,
2133 &cached_state, GFP_NOFS);
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,
2725 &cached, GFP_NOFS);
2726 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
Josef Bacik2aaa6652012-08-29 14:27:18 -04004772 if ((offset & (blocksize - 1)) == 0 &&
4773 (!len || ((len & (blocksize - 1)) == 0)))
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,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004815 &cached_state, GFP_NOFS);
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,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004832 &cached_state, GFP_NOFS);
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);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304851 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004852 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004853
Chris Mason89642222008-07-24 09:41:53 -04004854out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004855 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004856 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304857 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004858 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004859 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004860 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004861out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004862 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004863 return ret;
4864}
4865
Josef Bacik16e75492013-10-22 12:18:51 -04004866static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4867 u64 offset, u64 len)
4868{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004869 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004870 struct btrfs_trans_handle *trans;
4871 int ret;
4872
4873 /*
4874 * Still need to make sure the inode looks like it's been updated so
4875 * that any holes get logged if we fsync.
4876 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004877 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4878 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004879 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4880 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4881 return 0;
4882 }
4883
4884 /*
4885 * 1 - for the one we're dropping
4886 * 1 - for the one we're adding
4887 * 1 - for updating the inode.
4888 */
4889 trans = btrfs_start_transaction(root, 3);
4890 if (IS_ERR(trans))
4891 return PTR_ERR(trans);
4892
4893 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4894 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004895 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004896 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004897 return ret;
4898 }
4899
David Sterbaf85b7372017-01-20 14:54:07 +01004900 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4901 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004902 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004903 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004904 else
4905 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004906 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004907 return ret;
4908}
4909
Josef Bacik695a0d02011-03-04 15:46:53 -05004910/*
4911 * This function puts in dummy file extents for the area we're creating a hole
4912 * for. So if we are truncating this file to a larger size we need to insert
4913 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4914 * the range between oldsize and size
4915 */
Josef Bacika41ad392011-01-31 15:30:16 -05004916int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004917{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004918 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004919 struct btrfs_root *root = BTRFS_I(inode)->root;
4920 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004921 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004922 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004923 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004924 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4925 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004926 u64 last_byte;
4927 u64 cur_offset;
4928 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004929 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004930
Josef Bacika71754f2013-06-17 17:14:39 -04004931 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304932 * If our size started in the middle of a block we need to zero out the
4933 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004934 * expose stale data.
4935 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304936 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004937 if (err)
4938 return err;
4939
Yan Zheng9036c102008-10-30 14:19:41 -04004940 if (size <= hole_start)
4941 return 0;
4942
Yan Zheng9036c102008-10-30 14:19:41 -04004943 while (1) {
4944 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004945
David Sterbaff13db42015-12-03 14:30:40 +01004946 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004947 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004948 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004949 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004950 if (!ordered)
4951 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004952 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4953 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004954 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004955 btrfs_put_ordered_extent(ordered);
4956 }
4957
Yan Zheng9036c102008-10-30 14:19:41 -04004958 cur_offset = hole_start;
4959 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004960 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004961 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004962 if (IS_ERR(em)) {
4963 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004964 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004965 break;
4966 }
Yan Zheng9036c102008-10-30 14:19:41 -04004967 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004968 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004969 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004970 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004971 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004972
Josef Bacik16e75492013-10-22 12:18:51 -04004973 err = maybe_insert_hole(root, inode, cur_offset,
4974 hole_size);
4975 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004976 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004977 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004978 cur_offset + hole_size - 1, 0);
4979 hole_em = alloc_extent_map();
4980 if (!hole_em) {
4981 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4982 &BTRFS_I(inode)->runtime_flags);
4983 goto next;
4984 }
4985 hole_em->start = cur_offset;
4986 hole_em->len = hole_size;
4987 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004988
Josef Bacik5dc562c2012-08-17 13:14:17 -04004989 hole_em->block_start = EXTENT_MAP_HOLE;
4990 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004991 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004992 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004993 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004994 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004995 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004996
4997 while (1) {
4998 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004999 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005000 write_unlock(&em_tree->lock);
5001 if (err != -EEXIST)
5002 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005003 btrfs_drop_extent_cache(BTRFS_I(inode),
5004 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005005 cur_offset +
5006 hole_size - 1, 0);
5007 }
5008 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005009 }
Josef Bacik16e75492013-10-22 12:18:51 -04005010next:
Yan Zheng9036c102008-10-30 14:19:41 -04005011 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005012 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005013 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005014 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005015 break;
5016 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005017 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005018 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
5019 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04005020 return err;
5021}
5022
Eric Sandeen3972f262013-01-12 02:57:22 +00005023static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005024{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005025 struct btrfs_root *root = BTRFS_I(inode)->root;
5026 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005027 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005028 loff_t newsize = attr->ia_size;
5029 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005030 int ret;
5031
Eric Sandeen3972f262013-01-12 02:57:22 +00005032 /*
5033 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5034 * special case where we need to update the times despite not having
5035 * these flags set. For all other operations the VFS set these flags
5036 * explicitly if it wants a timestamp update.
5037 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005038 if (newsize != oldsize) {
5039 inode_inc_iversion(inode);
5040 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5041 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005042 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005043 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005044
Josef Bacika41ad392011-01-31 15:30:16 -05005045 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005046 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005047 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005048 * This is to ensure the snapshot captures a fully consistent
5049 * state of this file - if the snapshot captures this expanding
5050 * truncation, it must capture all writes that happened before
5051 * this truncation.
5052 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005053 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005054 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005055 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005056 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005057 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005058 }
Yan, Zheng80825102009-11-12 09:35:36 +00005059
Miao Xief4a2f4c2011-12-14 20:12:01 -05005060 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005061 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005062 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005063 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005064 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005065
5066 i_size_write(inode, newsize);
5067 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305068 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005069 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005070 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005071 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005072 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005073
Josef Bacika41ad392011-01-31 15:30:16 -05005074 /*
5075 * We're truncating a file that used to have good data down to
5076 * zero. Make sure it gets into the ordered flush list so that
5077 * any new writes get down to disk quickly.
5078 */
5079 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005080 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5081 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005082
Josef Bacikf3fe8202013-01-07 17:03:21 -05005083 /*
5084 * 1 for the orphan item we're going to add
5085 * 1 for the orphan item deletion.
5086 */
5087 trans = btrfs_start_transaction(root, 2);
5088 if (IS_ERR(trans))
5089 return PTR_ERR(trans);
5090
5091 /*
5092 * We need to do this in case we fail at _any_ point during the
5093 * actual truncate. Once we do the truncate_setsize we could
5094 * invalidate pages which forces any outstanding ordered io to
5095 * be instantly completed which will give us extents that need
5096 * to be truncated. If we fail to get an orphan inode down we
5097 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005098 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005099 * will be consistent.
5100 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005101 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005102 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005103 if (ret)
5104 return ret;
5105
Josef Bacika41ad392011-01-31 15:30:16 -05005106 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5107 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005108
5109 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005110 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005111 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005112 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005113
Josef Bacika41ad392011-01-31 15:30:16 -05005114 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005115 if (ret && inode->i_nlink) {
5116 int err;
5117
Liu Bo19fd2df2016-12-01 13:01:02 -08005118 /* To get a stable disk_i_size */
5119 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5120 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005121 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005122 return err;
5123 }
5124
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005125 /*
5126 * failed to truncate, disk_i_size is only adjusted down
5127 * as we remove extents, so it should represent the true
5128 * size of the inode, so reset the in memory size and
5129 * delete our orphan entry.
5130 */
5131 trans = btrfs_join_transaction(root);
5132 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005133 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005134 return ret;
5135 }
5136 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005137 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005138 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005139 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005140 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005141 }
Yan, Zheng80825102009-11-12 09:35:36 +00005142 }
5143
Josef Bacika41ad392011-01-31 15:30:16 -05005144 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005145}
5146
Chris Mason39279cc2007-06-12 06:35:45 -04005147static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5148{
David Howells2b0143b2015-03-17 22:25:59 +00005149 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005150 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005151 int err;
5152
Li Zefanb83cc962010-12-20 16:04:08 +08005153 if (btrfs_root_readonly(root))
5154 return -EROFS;
5155
Jan Kara31051c82016-05-26 16:55:18 +02005156 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005157 if (err)
5158 return err;
5159
Chris Mason5a3f23d2009-03-31 13:27:11 -04005160 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005161 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005162 if (err)
5163 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005164 }
Yan Zheng9036c102008-10-30 14:19:41 -04005165
Christoph Hellwig10257742010-06-04 11:30:02 +02005166 if (attr->ia_valid) {
5167 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005168 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005169 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005170
Josef Bacik22c44fe2011-11-30 10:45:38 -05005171 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005172 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005173 }
5174
Chris Mason39279cc2007-06-12 06:35:45 -04005175 return err;
5176}
Chris Mason61295eb2008-01-14 16:24:38 -05005177
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005178/*
5179 * While truncating the inode pages during eviction, we get the VFS calling
5180 * btrfs_invalidatepage() against each page of the inode. This is slow because
5181 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5182 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5183 * extent_state structures over and over, wasting lots of time.
5184 *
5185 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5186 * those expensive operations on a per page basis and do only the ordered io
5187 * finishing, while we release here the extent_map and extent_state structures,
5188 * without the excessive merging and splitting.
5189 */
5190static void evict_inode_truncate_pages(struct inode *inode)
5191{
5192 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5193 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5194 struct rb_node *node;
5195
5196 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005197 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005198
5199 write_lock(&map_tree->lock);
5200 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5201 struct extent_map *em;
5202
5203 node = rb_first(&map_tree->map);
5204 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005205 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5206 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005207 remove_extent_mapping(map_tree, em);
5208 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005209 if (need_resched()) {
5210 write_unlock(&map_tree->lock);
5211 cond_resched();
5212 write_lock(&map_tree->lock);
5213 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005214 }
5215 write_unlock(&map_tree->lock);
5216
Filipe Manana6ca07092015-05-26 00:55:42 +01005217 /*
5218 * Keep looping until we have no more ranges in the io tree.
5219 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005220 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5221 * still in progress (unlocked the pages in the bio but did not yet
5222 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005223 * ranges can still be locked and eviction started because before
5224 * submitting those bios, which are executed by a separate task (work
5225 * queue kthread), inode references (inode->i_count) were not taken
5226 * (which would be dropped in the end io callback of each bio).
5227 * Therefore here we effectively end up waiting for those bios and
5228 * anyone else holding locked ranges without having bumped the inode's
5229 * reference count - if we don't do it, when they access the inode's
5230 * io_tree to unlock a range it may be too late, leading to an
5231 * use-after-free issue.
5232 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005233 spin_lock(&io_tree->lock);
5234 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5235 struct extent_state *state;
5236 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005237 u64 start;
5238 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005239
5240 node = rb_first(&io_tree->state);
5241 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005242 start = state->start;
5243 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005244 spin_unlock(&io_tree->lock);
5245
David Sterbaff13db42015-12-03 14:30:40 +01005246 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005247
5248 /*
5249 * If still has DELALLOC flag, the extent didn't reach disk,
5250 * and its reserved space won't be freed by delayed_ref.
5251 * So we need to free its reserved space here.
5252 * (Refer to comment in btrfs_invalidatepage, case 2)
5253 *
5254 * Note, end is the bytenr of last byte, so we need + 1 here.
5255 */
5256 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005257 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005258
Filipe Manana6ca07092015-05-26 00:55:42 +01005259 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005260 EXTENT_LOCKED | EXTENT_DIRTY |
5261 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005262 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005263
Filipe Manana7064dd52014-08-08 02:47:05 +01005264 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005265 spin_lock(&io_tree->lock);
5266 }
5267 spin_unlock(&io_tree->lock);
5268}
5269
Al Virobd555972010-06-07 11:35:40 -04005270void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005271{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005272 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005273 struct btrfs_trans_handle *trans;
5274 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005275 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005276 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005277 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005278 int ret;
5279
liubo1abe9b82011-03-24 11:18:59 +00005280 trace_btrfs_inode_evict(inode);
5281
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005282 if (!root) {
5283 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5284 return;
5285 }
5286
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005287 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005288
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005289 evict_inode_truncate_pages(inode);
5290
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005291 if (inode->i_nlink &&
5292 ((btrfs_root_refs(&root->root_item) != 0 &&
5293 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005294 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005295 goto no_delete;
5296
Chris Mason39279cc2007-06-12 06:35:45 -04005297 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005298 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005299 goto no_delete;
5300 }
Al Virobd555972010-06-07 11:35:40 -04005301 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005302 if (!special_file(inode->i_mode))
5303 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005304
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005305 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005306
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005307 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005308 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005309 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005310 goto no_delete;
5311 }
5312
Yan, Zheng76dda932009-09-21 16:00:26 -04005313 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005314 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5315 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005316 goto no_delete;
5317 }
5318
Nikolay Borisovaa790212017-01-10 20:35:40 +02005319 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005320 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005321 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005322 goto no_delete;
5323 }
5324
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005325 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005326 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005327 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005328 goto no_delete;
5329 }
Josef Bacik4a338542011-08-29 11:01:31 -04005330 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005331 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005332 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005333
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005334 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005335
Josef Bacik4289a662011-08-05 13:22:24 -04005336 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005337 * This is a bit simpler than btrfs_truncate since we've already
5338 * reserved our space for our orphan item in the unlink, so we just
5339 * need to reserve some slack space in case we add bytes and update
5340 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005341 */
Yan, Zheng80825102009-11-12 09:35:36 +00005342 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005343 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5344 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005345
Josef Bacik726c35f2011-09-26 15:46:06 -04005346 /*
5347 * Try and steal from the global reserve since we will
5348 * likely not use this space anyway, we want to try as
5349 * hard as possible to get this to work.
5350 */
5351 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005352 steal_from_global++;
5353 else
5354 steal_from_global = 0;
5355 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005356
Josef Bacik3bce8762015-02-24 12:35:51 -08005357 /*
5358 * steal_from_global == 0: we reserved stuff, hooray!
5359 * steal_from_global == 1: we didn't reserve stuff, boo!
5360 * steal_from_global == 2: we've committed, still not a lot of
5361 * room but maybe we'll have room in the global reserve this
5362 * time.
5363 * steal_from_global == 3: abandon all hope!
5364 */
5365 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005366 btrfs_warn(fs_info,
5367 "Could not get space for a delete, will truncate on mount %d",
5368 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005369 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005370 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005371 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005372 }
5373
Miao Xie0e8c36a2012-12-19 06:59:51 +00005374 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005375 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005376 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005377 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005378 goto no_delete;
5379 }
5380
Josef Bacik3bce8762015-02-24 12:35:51 -08005381 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005382 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005383 * sure there is room to do it, if not we need to commit and try
5384 * again.
5385 */
5386 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005387 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005388 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005389 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005390 else
5391 ret = -ENOSPC;
5392 }
5393
5394 /*
5395 * Couldn't steal from the global reserve, we have too much
5396 * pending stuff built up, commit the transaction and try it
5397 * again.
5398 */
5399 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005400 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005401 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005402 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005403 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005404 goto no_delete;
5405 }
5406 continue;
5407 } else {
5408 steal_from_global = 0;
5409 }
5410
Josef Bacik4289a662011-08-05 13:22:24 -04005411 trans->block_rsv = rsv;
5412
Yan, Zhengd68fc572010-05-16 10:49:58 -04005413 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005414 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005415 break;
5416
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005417 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005418 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005419 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005420 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005421 }
5422
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005423 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005424
Josef Bacik4ef31a42013-08-13 14:10:08 -04005425 /*
5426 * Errors here aren't a big deal, it just means we leave orphan items
5427 * in the tree. They will be cleaned up on the next mount.
5428 */
Yan, Zheng80825102009-11-12 09:35:36 +00005429 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005430 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005431 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005432 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005433 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005434 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005435
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005436 trans->block_rsv = &fs_info->trans_block_rsv;
5437 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005438 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005439 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005440
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005441 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005442 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005443no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005444 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005445 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005446}
5447
5448/*
5449 * this returns the key found in the dir entry in the location pointer.
5450 * If no dir entries were found, location->objectid is 0.
5451 */
5452static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5453 struct btrfs_key *location)
5454{
5455 const char *name = dentry->d_name.name;
5456 int namelen = dentry->d_name.len;
5457 struct btrfs_dir_item *di;
5458 struct btrfs_path *path;
5459 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005460 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005461
5462 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005463 if (!path)
5464 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005465
David Sterbaf85b7372017-01-20 14:54:07 +01005466 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5467 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005468 if (IS_ERR(di))
5469 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005470
David Sterbac7040052011-04-19 18:00:01 +02005471 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005472 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005473
Chris Mason5f39d392007-10-15 16:14:19 -04005474 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005475 if (location->type != BTRFS_INODE_ITEM_KEY &&
5476 location->type != BTRFS_ROOT_ITEM_KEY) {
5477 btrfs_warn(root->fs_info,
5478"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5479 __func__, name, btrfs_ino(BTRFS_I(dir)),
5480 location->objectid, location->type, location->offset);
5481 goto out_err;
5482 }
Chris Mason39279cc2007-06-12 06:35:45 -04005483out:
Chris Mason39279cc2007-06-12 06:35:45 -04005484 btrfs_free_path(path);
5485 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005486out_err:
5487 location->objectid = 0;
5488 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005489}
5490
5491/*
5492 * when we hit a tree root in a directory, the btrfs part of the inode
5493 * needs to be changed to reflect the root directory of the tree root. This
5494 * is kind of like crossing a mount point.
5495 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005496static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005497 struct inode *dir,
5498 struct dentry *dentry,
5499 struct btrfs_key *location,
5500 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005501{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005502 struct btrfs_path *path;
5503 struct btrfs_root *new_root;
5504 struct btrfs_root_ref *ref;
5505 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005506 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005507 int ret;
5508 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005509
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005510 path = btrfs_alloc_path();
5511 if (!path) {
5512 err = -ENOMEM;
5513 goto out;
5514 }
Chris Mason39279cc2007-06-12 06:35:45 -04005515
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005516 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005517 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5518 key.type = BTRFS_ROOT_REF_KEY;
5519 key.offset = location->objectid;
5520
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005521 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005522 if (ret) {
5523 if (ret < 0)
5524 err = ret;
5525 goto out;
5526 }
Chris Mason39279cc2007-06-12 06:35:45 -04005527
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005528 leaf = path->nodes[0];
5529 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005530 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005531 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5532 goto out;
5533
5534 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5535 (unsigned long)(ref + 1),
5536 dentry->d_name.len);
5537 if (ret)
5538 goto out;
5539
David Sterbab3b4aa72011-04-21 01:20:15 +02005540 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005541
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005542 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005543 if (IS_ERR(new_root)) {
5544 err = PTR_ERR(new_root);
5545 goto out;
5546 }
5547
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005548 *sub_root = new_root;
5549 location->objectid = btrfs_root_dirid(&new_root->root_item);
5550 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005551 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005552 err = 0;
5553out:
5554 btrfs_free_path(path);
5555 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005556}
5557
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005558static void inode_tree_add(struct inode *inode)
5559{
5560 struct btrfs_root *root = BTRFS_I(inode)->root;
5561 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005562 struct rb_node **p;
5563 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005564 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005565 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005566
Al Viro1d3382cb2010-10-23 15:19:20 -04005567 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005568 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005569 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005570 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005571 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005572 while (*p) {
5573 parent = *p;
5574 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5575
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005576 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005577 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005578 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005579 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005580 else {
5581 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005582 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005583 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005584 RB_CLEAR_NODE(parent);
5585 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005586 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005587 }
5588 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005589 rb_link_node(new, parent, p);
5590 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005591 spin_unlock(&root->inode_lock);
5592}
5593
5594static void inode_tree_del(struct inode *inode)
5595{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005596 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005597 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005598 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005599
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005600 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005602 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005603 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005604 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005605 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005606 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005607
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005608 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005609 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005610 spin_lock(&root->inode_lock);
5611 empty = RB_EMPTY_ROOT(&root->inode_tree);
5612 spin_unlock(&root->inode_lock);
5613 if (empty)
5614 btrfs_add_dead_root(root);
5615 }
5616}
5617
Jeff Mahoney143bede2012-03-01 14:56:26 +01005618void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005619{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005620 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005621 struct rb_node *node;
5622 struct rb_node *prev;
5623 struct btrfs_inode *entry;
5624 struct inode *inode;
5625 u64 objectid = 0;
5626
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005627 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005628 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005629
5630 spin_lock(&root->inode_lock);
5631again:
5632 node = root->inode_tree.rb_node;
5633 prev = NULL;
5634 while (node) {
5635 prev = node;
5636 entry = rb_entry(node, struct btrfs_inode, rb_node);
5637
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005638 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005639 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005640 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005641 node = node->rb_right;
5642 else
5643 break;
5644 }
5645 if (!node) {
5646 while (prev) {
5647 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005648 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005649 node = prev;
5650 break;
5651 }
5652 prev = rb_next(prev);
5653 }
5654 }
5655 while (node) {
5656 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005657 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005658 inode = igrab(&entry->vfs_inode);
5659 if (inode) {
5660 spin_unlock(&root->inode_lock);
5661 if (atomic_read(&inode->i_count) > 1)
5662 d_prune_aliases(inode);
5663 /*
Al Viro45321ac2010-06-07 13:43:19 -04005664 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005665 * the inode cache when its usage count
5666 * hits zero.
5667 */
5668 iput(inode);
5669 cond_resched();
5670 spin_lock(&root->inode_lock);
5671 goto again;
5672 }
5673
5674 if (cond_resched_lock(&root->inode_lock))
5675 goto again;
5676
5677 node = rb_next(node);
5678 }
5679 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005680}
5681
Chris Masone02119d2008-09-05 16:13:11 -04005682static int btrfs_init_locked_inode(struct inode *inode, void *p)
5683{
5684 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005685 inode->i_ino = args->location->objectid;
5686 memcpy(&BTRFS_I(inode)->location, args->location,
5687 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005688 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005689 return 0;
5690}
5691
5692static int btrfs_find_actor(struct inode *inode, void *opaque)
5693{
5694 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005695 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005696 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005697}
5698
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005699static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005700 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005701 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005702{
5703 struct inode *inode;
5704 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005705 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005706
Chris Mason90d3e592014-01-09 17:28:00 -08005707 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005708 args.root = root;
5709
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005710 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005711 btrfs_init_locked_inode,
5712 (void *)&args);
5713 return inode;
5714}
5715
Balaji Rao1a54ef82008-07-21 02:01:04 +05305716/* Get an inode object given its location and corresponding root.
5717 * Returns in *is_new if the inode was read from disk
5718 */
5719struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005720 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305721{
5722 struct inode *inode;
5723
Chris Mason90d3e592014-01-09 17:28:00 -08005724 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305725 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005726 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305727
5728 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005729 int ret;
5730
5731 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005732 if (!is_bad_inode(inode)) {
5733 inode_tree_add(inode);
5734 unlock_new_inode(inode);
5735 if (new)
5736 *new = 1;
5737 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005738 unlock_new_inode(inode);
5739 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005740 ASSERT(ret < 0);
5741 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005742 }
5743 }
5744
Balaji Rao1a54ef82008-07-21 02:01:04 +05305745 return inode;
5746}
5747
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005748static struct inode *new_simple_dir(struct super_block *s,
5749 struct btrfs_key *key,
5750 struct btrfs_root *root)
5751{
5752 struct inode *inode = new_inode(s);
5753
5754 if (!inode)
5755 return ERR_PTR(-ENOMEM);
5756
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005757 BTRFS_I(inode)->root = root;
5758 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005759 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005760
5761 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005762 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005763 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005764 inode->i_fop = &simple_dir_operations;
5765 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005766 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305767 inode->i_atime = inode->i_mtime;
5768 inode->i_ctime = inode->i_mtime;
5769 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005770
5771 return inode;
5772}
5773
Chris Mason3de45862008-11-17 21:02:50 -05005774struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005775{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005776 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005777 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005778 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005779 struct btrfs_root *sub_root = root;
5780 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005781 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005782 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005783
5784 if (dentry->d_name.len > BTRFS_NAME_LEN)
5785 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005786
Jeff Layton39e3c952012-11-28 11:30:53 -05005787 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005788 if (ret < 0)
5789 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005790
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005791 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005792 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005793
5794 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005795 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005796 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005797 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005798
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005799 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005800 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005801 &location, &sub_root);
5802 if (ret < 0) {
5803 if (ret != -ENOENT)
5804 inode = ERR_PTR(ret);
5805 else
5806 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5807 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005808 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005809 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005810 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005811
Julia Lawall34d19ba2011-01-24 19:55:19 +00005812 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005813 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005814 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005815 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005816 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005817 if (ret) {
5818 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005819 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005820 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005821 }
5822
Chris Mason3de45862008-11-17 21:02:50 -05005823 return inode;
5824}
5825
Nick Pigginfe15ce42011-01-07 17:49:23 +11005826static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005827{
5828 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005829 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005830
Li Zefan848cce02012-02-21 17:04:28 +08005831 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005832 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005833
Li Zefan848cce02012-02-21 17:04:28 +08005834 if (inode) {
5835 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005836 if (btrfs_root_refs(&root->root_item) == 0)
5837 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005838
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005839 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005840 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005841 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005842 return 0;
5843}
5844
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005845static void btrfs_dentry_release(struct dentry *dentry)
5846{
Daeseok Youn944a4512014-04-14 15:37:02 +09005847 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005848}
5849
Chris Mason3de45862008-11-17 21:02:50 -05005850static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005851 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005852{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005853 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005854
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005855 inode = btrfs_lookup_dentry(dir, dentry);
5856 if (IS_ERR(inode)) {
5857 if (PTR_ERR(inode) == -ENOENT)
5858 inode = NULL;
5859 else
5860 return ERR_CAST(inode);
5861 }
5862
Al Viro41d28bc2014-10-12 22:24:21 -04005863 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005864}
5865
Miao Xie16cdcec2011-04-22 18:12:22 +08005866unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005867 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5868};
5869
Josef Bacik23b5ec72017-07-24 15:14:25 -04005870/*
5871 * All this infrastructure exists because dir_emit can fault, and we are holding
5872 * the tree lock when doing readdir. For now just allocate a buffer and copy
5873 * our information into that, and then dir_emit from the buffer. This is
5874 * similar to what NFS does, only we don't keep the buffer around in pagecache
5875 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5876 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5877 * tree lock.
5878 */
5879static int btrfs_opendir(struct inode *inode, struct file *file)
5880{
5881 struct btrfs_file_private *private;
5882
5883 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5884 if (!private)
5885 return -ENOMEM;
5886 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5887 if (!private->filldir_buf) {
5888 kfree(private);
5889 return -ENOMEM;
5890 }
5891 file->private_data = private;
5892 return 0;
5893}
5894
5895struct dir_entry {
5896 u64 ino;
5897 u64 offset;
5898 unsigned type;
5899 int name_len;
5900};
5901
5902static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5903{
5904 while (entries--) {
5905 struct dir_entry *entry = addr;
5906 char *name = (char *)(entry + 1);
5907
5908 ctx->pos = entry->offset;
5909 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5910 entry->type))
5911 return 1;
5912 addr += sizeof(struct dir_entry) + entry->name_len;
5913 ctx->pos++;
5914 }
5915 return 0;
5916}
5917
Al Viro9cdda8d2013-05-22 16:48:09 -04005918static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005919{
Al Viro9cdda8d2013-05-22 16:48:09 -04005920 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005921 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005922 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005923 struct btrfs_dir_item *di;
5924 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005925 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005926 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005927 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005928 struct list_head ins_list;
5929 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005930 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005931 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005932 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005933 char *name_ptr;
5934 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005935 int entries = 0;
5936 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005937 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005938 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005939
Al Viro9cdda8d2013-05-22 16:48:09 -04005940 if (!dir_emit_dots(file, ctx))
5941 return 0;
5942
David Woodhouse49593bf2008-08-17 17:08:36 +01005943 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005944 if (!path)
5945 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005946
Josef Bacik23b5ec72017-07-24 15:14:25 -04005947 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005948 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005949
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005950 INIT_LIST_HEAD(&ins_list);
5951 INIT_LIST_HEAD(&del_list);
5952 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005953
Josef Bacik23b5ec72017-07-24 15:14:25 -04005954again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005955 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005956 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005957 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005958
Chris Mason39279cc2007-06-12 06:35:45 -04005959 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5960 if (ret < 0)
5961 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005962
5963 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005964 struct dir_entry *entry;
5965
Chris Mason5f39d392007-10-15 16:14:19 -04005966 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005967 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005968 if (slot >= btrfs_header_nritems(leaf)) {
5969 ret = btrfs_next_leaf(root, path);
5970 if (ret < 0)
5971 goto err;
5972 else if (ret > 0)
5973 break;
5974 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005975 }
Chris Mason3de45862008-11-17 21:02:50 -05005976
Chris Mason5f39d392007-10-15 16:14:19 -04005977 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5978
5979 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005980 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005981 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005982 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005983 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005984 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005985 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005986 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005987 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005988 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005989 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5990 PAGE_SIZE) {
5991 btrfs_release_path(path);
5992 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5993 if (ret)
5994 goto nopos;
5995 addr = private->filldir_buf;
5996 entries = 0;
5997 total_len = 0;
5998 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005999 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006000
6001 entry = addr;
6002 entry->name_len = name_len;
6003 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006004 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6005 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006006 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006007 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006008 entry->ino = location.objectid;
6009 entry->offset = found_key.offset;
6010 entries++;
6011 addr += sizeof(struct dir_entry) + name_len;
6012 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006013next:
6014 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006015 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006016 btrfs_release_path(path);
6017
6018 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6019 if (ret)
6020 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006021
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006022 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006023 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006024 goto nopos;
6025
Zach Browndb62efb2013-07-11 16:19:42 -07006026 /*
6027 * Stop new entries from being returned after we return the last
6028 * entry.
6029 *
6030 * New directory entries are assigned a strictly increasing
6031 * offset. This means that new entries created during readdir
6032 * are *guaranteed* to be seen in the future by that readdir.
6033 * This has broken buggy programs which operate on names as
6034 * they're returned by readdir. Until we re-use freed offsets
6035 * we have this hack to stop new entries from being returned
6036 * under the assumption that they'll never reach this huge
6037 * offset.
6038 *
6039 * This is being careful not to overflow 32bit loff_t unless the
6040 * last entry requires it because doing so has broken 32bit apps
6041 * in the past.
6042 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006043 if (ctx->pos >= INT_MAX)
6044 ctx->pos = LLONG_MAX;
6045 else
6046 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006047nopos:
6048 ret = 0;
6049err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006050 if (put)
6051 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006052 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006053 return ret;
6054}
6055
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006056int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006057{
6058 struct btrfs_root *root = BTRFS_I(inode)->root;
6059 struct btrfs_trans_handle *trans;
6060 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006061 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006062
Josef Bacik72ac3c02012-05-23 14:13:11 -04006063 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006064 return 0;
6065
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006066 if (btrfs_fs_closing(root->fs_info) &&
6067 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006068 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006069
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006070 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006071 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006072 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006073 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006074 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006075 if (IS_ERR(trans))
6076 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006077 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006078 }
6079 return ret;
6080}
6081
6082/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006083 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006084 * inode changes. But, it is most likely to find the inode in cache.
6085 * FIXME, needs more benchmarking...there are no reasons other than performance
6086 * to keep or drop this code.
6087 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006088static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006089{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006090 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006091 struct btrfs_root *root = BTRFS_I(inode)->root;
6092 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006093 int ret;
6094
Josef Bacik72ac3c02012-05-23 14:13:11 -04006095 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006096 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006097
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006098 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006099 if (IS_ERR(trans))
6100 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006101
6102 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006103 if (ret && ret == -ENOSPC) {
6104 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006105 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006106 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006107 if (IS_ERR(trans))
6108 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006109
Chris Mason94b60442010-05-26 11:02:00 -04006110 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006111 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006112 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006113 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006114 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006115
6116 return ret;
6117}
6118
6119/*
6120 * This is a copy of file_update_time. We need this so we can return error on
6121 * ENOSPC for updating the inode in the case of file write and mmap writes.
6122 */
Josef Bacike41f9412012-03-26 09:46:47 -04006123static int btrfs_update_time(struct inode *inode, struct timespec *now,
6124 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006125{
Alexander Block2bc5565282012-06-15 09:49:33 +02006126 struct btrfs_root *root = BTRFS_I(inode)->root;
6127
6128 if (btrfs_root_readonly(root))
6129 return -EROFS;
6130
Josef Bacike41f9412012-03-26 09:46:47 -04006131 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006132 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006133 if (flags & S_CTIME)
6134 inode->i_ctime = *now;
6135 if (flags & S_MTIME)
6136 inode->i_mtime = *now;
6137 if (flags & S_ATIME)
6138 inode->i_atime = *now;
6139 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006140}
6141
Chris Masond352ac62008-09-29 15:18:18 -04006142/*
6143 * find the highest existing sequence number in a directory
6144 * and then set the in-memory index_cnt variable to reflect
6145 * free sequence numbers
6146 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006147static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006148{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006149 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006150 struct btrfs_key key, found_key;
6151 struct btrfs_path *path;
6152 struct extent_buffer *leaf;
6153 int ret;
6154
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006155 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006156 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006157 key.offset = (u64)-1;
6158
6159 path = btrfs_alloc_path();
6160 if (!path)
6161 return -ENOMEM;
6162
6163 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6164 if (ret < 0)
6165 goto out;
6166 /* FIXME: we should be able to handle this */
6167 if (ret == 0)
6168 goto out;
6169 ret = 0;
6170
6171 /*
6172 * MAGIC NUMBER EXPLANATION:
6173 * since we search a directory based on f_pos we have to start at 2
6174 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6175 * else has to start at 2
6176 */
6177 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006178 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006179 goto out;
6180 }
6181
6182 path->slots[0]--;
6183
6184 leaf = path->nodes[0];
6185 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6186
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006187 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006188 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006189 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006190 goto out;
6191 }
6192
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006193 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006194out:
6195 btrfs_free_path(path);
6196 return ret;
6197}
6198
Chris Masond352ac62008-09-29 15:18:18 -04006199/*
6200 * helper to find a free sequence number in a given directory. This current
6201 * code is very simple, later versions will do smarter things in the btree
6202 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006203int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006204{
6205 int ret = 0;
6206
Nikolay Borisov877574e2017-02-20 13:50:33 +02006207 if (dir->index_cnt == (u64)-1) {
6208 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006209 if (ret) {
6210 ret = btrfs_set_inode_index_count(dir);
6211 if (ret)
6212 return ret;
6213 }
Josef Bacikaec74772008-07-24 12:12:38 -04006214 }
6215
Nikolay Borisov877574e2017-02-20 13:50:33 +02006216 *index = dir->index_cnt;
6217 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006218
6219 return ret;
6220}
6221
Chris Masonb0d5d102014-09-08 13:08:51 -07006222static int btrfs_insert_inode_locked(struct inode *inode)
6223{
6224 struct btrfs_iget_args args;
6225 args.location = &BTRFS_I(inode)->location;
6226 args.root = BTRFS_I(inode)->root;
6227
6228 return insert_inode_locked4(inode,
6229 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6230 btrfs_find_actor, &args);
6231}
6232
Anand Jain19aee8d2017-07-18 17:37:05 +08006233/*
6234 * Inherit flags from the parent inode.
6235 *
6236 * Currently only the compression flags and the cow flags are inherited.
6237 */
6238static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6239{
6240 unsigned int flags;
6241
6242 if (!dir)
6243 return;
6244
6245 flags = BTRFS_I(dir)->flags;
6246
6247 if (flags & BTRFS_INODE_NOCOMPRESS) {
6248 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6249 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6250 } else if (flags & BTRFS_INODE_COMPRESS) {
6251 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6252 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6253 }
6254
6255 if (flags & BTRFS_INODE_NODATACOW) {
6256 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6257 if (S_ISREG(inode->i_mode))
6258 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6259 }
6260
6261 btrfs_update_iflags(inode);
6262}
6263
Chris Mason39279cc2007-06-12 06:35:45 -04006264static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6265 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006266 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006267 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006268 u64 ref_objectid, u64 objectid,
6269 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006270{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006271 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006272 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006273 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006274 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006275 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006276 struct btrfs_inode_ref *ref;
6277 struct btrfs_key key[2];
6278 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006279 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006280 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006281 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006282
Chris Mason5f39d392007-10-15 16:14:19 -04006283 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006284 if (!path)
6285 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006286
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006287 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006288 if (!inode) {
6289 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006290 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006291 }
Chris Mason39279cc2007-06-12 06:35:45 -04006292
Li Zefan581bb052011-04-20 10:06:11 +08006293 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006294 * O_TMPFILE, set link count to 0, so that after this point,
6295 * we fill in an inode item with the correct link count.
6296 */
6297 if (!name)
6298 set_nlink(inode, 0);
6299
6300 /*
Li Zefan581bb052011-04-20 10:06:11 +08006301 * we have to initialize this early, so we can reclaim the inode
6302 * number if we fail afterwards in this function.
6303 */
6304 inode->i_ino = objectid;
6305
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006306 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006307 trace_btrfs_inode_request(dir);
6308
Nikolay Borisov877574e2017-02-20 13:50:33 +02006309 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006310 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006311 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006312 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006313 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006314 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006315 } else if (dir) {
6316 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006317 }
6318 /*
6319 * index_cnt is ignored for everything but a dir,
6320 * btrfs_get_inode_index_count has an explanation for the magic
6321 * number
6322 */
6323 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006324 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006325 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006326 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006327 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006328
Josef Bacik5dc562c2012-08-17 13:14:17 -04006329 /*
6330 * We could have gotten an inode number from somebody who was fsynced
6331 * and then removed in this same transaction, so let's just set full
6332 * sync since it will be a full sync anyway and this will blow away the
6333 * old info in the log.
6334 */
6335 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6336
Chris Mason9c583092008-01-29 15:15:18 -05006337 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006338 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006339 key[0].offset = 0;
6340
Chris Mason9c583092008-01-29 15:15:18 -05006341 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006342
6343 if (name) {
6344 /*
6345 * Start new inodes with an inode_ref. This is slightly more
6346 * efficient for small numbers of hard links since they will
6347 * be packed into one item. Extended refs will kick in if we
6348 * add more hard links than can fit in the ref item.
6349 */
6350 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006351 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006352 key[1].offset = ref_objectid;
6353
6354 sizes[1] = name_len + sizeof(*ref);
6355 }
Chris Mason9c583092008-01-29 15:15:18 -05006356
Chris Masonb0d5d102014-09-08 13:08:51 -07006357 location = &BTRFS_I(inode)->location;
6358 location->objectid = objectid;
6359 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006360 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006361
6362 ret = btrfs_insert_inode_locked(inode);
6363 if (ret < 0)
6364 goto fail;
6365
Chris Masonb9473432009-03-13 11:00:37 -04006366 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006367 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006368 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006369 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006370
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006371 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006372 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306373
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006374 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306375 inode->i_atime = inode->i_mtime;
6376 inode->i_ctime = inode->i_mtime;
6377 BTRFS_I(inode)->i_otime = inode->i_mtime;
6378
Chris Mason5f39d392007-10-15 16:14:19 -04006379 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6380 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006381 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006382 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006383 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006384
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006385 if (name) {
6386 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6387 struct btrfs_inode_ref);
6388 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6389 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6390 ptr = (unsigned long)(ref + 1);
6391 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6392 }
Chris Mason9c583092008-01-29 15:15:18 -05006393
Chris Mason5f39d392007-10-15 16:14:19 -04006394 btrfs_mark_buffer_dirty(path->nodes[0]);
6395 btrfs_free_path(path);
6396
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006397 btrfs_inherit_iflags(inode, dir);
6398
Al Viro569254b2011-07-24 17:08:40 -04006399 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006400 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006401 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006402 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006403 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6404 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006405 }
6406
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006407 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006408
6409 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006410 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006411
Alexander Block8ea05e32012-07-25 17:35:53 +02006412 btrfs_update_root_times(trans, root);
6413
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006414 ret = btrfs_inode_inherit_props(trans, inode, dir);
6415 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006416 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006417 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006418 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006419
Chris Mason39279cc2007-06-12 06:35:45 -04006420 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006421
6422fail_unlock:
6423 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006424fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006425 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006426 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006427 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006428 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006429 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006430}
6431
6432static inline u8 btrfs_inode_type(struct inode *inode)
6433{
6434 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6435}
6436
Chris Masond352ac62008-09-29 15:18:18 -04006437/*
6438 * utility function to add 'inode' into 'parent_inode' with
6439 * a give name and a given sequence number.
6440 * if 'add_backref' is true, also insert a backref from the
6441 * inode to the parent directory.
6442 */
Chris Masone02119d2008-09-05 16:13:11 -04006443int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006444 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006445 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006446{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006447 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006448 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006449 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006450 struct btrfs_root *root = parent_inode->root;
6451 u64 ino = btrfs_ino(inode);
6452 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006453
Li Zefan33345d012011-04-20 10:31:50 +08006454 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006455 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006456 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006457 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006458 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006459 key.offset = 0;
6460 }
Chris Mason39279cc2007-06-12 06:35:45 -04006461
Li Zefan33345d012011-04-20 10:31:50 +08006462 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006463 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6464 root->root_key.objectid, parent_ino,
6465 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006466 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006467 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6468 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006469 }
6470
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006471 /* Nothing to clean up yet */
6472 if (ret)
6473 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006474
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006475 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6476 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006477 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006478 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006479 goto fail_dir_item;
6480 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006481 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006482 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006483 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006484
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006485 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006486 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006487 inode_inc_iversion(&parent_inode->vfs_inode);
6488 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6489 current_time(&parent_inode->vfs_inode);
6490 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006491 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006492 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006493 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006494
6495fail_dir_item:
6496 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6497 u64 local_index;
6498 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006499 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6500 root->root_key.objectid, parent_ino,
6501 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006502
6503 } else if (add_backref) {
6504 u64 local_index;
6505 int err;
6506
6507 err = btrfs_del_inode_ref(trans, root, name, name_len,
6508 ino, parent_ino, &local_index);
6509 }
6510 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006511}
6512
6513static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006514 struct btrfs_inode *dir, struct dentry *dentry,
6515 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006516{
Josef Bacika1b075d2010-11-19 20:36:11 +00006517 int err = btrfs_add_link(trans, dir, inode,
6518 dentry->d_name.name, dentry->d_name.len,
6519 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006520 if (err > 0)
6521 err = -EEXIST;
6522 return err;
6523}
6524
Josef Bacik618e21d2007-07-11 10:18:17 -04006525static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006526 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006527{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006528 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006529 struct btrfs_trans_handle *trans;
6530 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006531 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006532 int err;
6533 int drop_inode = 0;
6534 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006535 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006536
Josef Bacik9ed74f22009-09-11 16:12:44 -04006537 /*
6538 * 2 for inode item and ref
6539 * 2 for dir items
6540 * 1 for xattr if selinux is on
6541 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006542 trans = btrfs_start_transaction(root, 5);
6543 if (IS_ERR(trans))
6544 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006545
Li Zefan581bb052011-04-20 10:06:11 +08006546 err = btrfs_find_free_ino(root, &objectid);
6547 if (err)
6548 goto out_unlock;
6549
Josef Bacikaec74772008-07-24 12:12:38 -04006550 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006551 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6552 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006553 if (IS_ERR(inode)) {
6554 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006555 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006556 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006557
Casey Schauflerad19db72011-12-15 10:09:07 -05006558 /*
6559 * If the active LSM wants to access the inode during
6560 * d_instantiate it needs these. Smack checks to see
6561 * if the filesystem supports xattrs by looking at the
6562 * ops vector.
6563 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006564 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006565 init_special_inode(inode, inode->i_mode, rdev);
6566
6567 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006568 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006569 goto out_unlock_inode;
6570
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006571 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6572 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006573 if (err) {
6574 goto out_unlock_inode;
6575 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006576 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006577 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006578 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006579 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006580
Josef Bacik618e21d2007-07-11 10:18:17 -04006581out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006582 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006583 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006584 if (drop_inode) {
6585 inode_dec_link_count(inode);
6586 iput(inode);
6587 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006588 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006589
6590out_unlock_inode:
6591 drop_inode = 1;
6592 unlock_new_inode(inode);
6593 goto out_unlock;
6594
Josef Bacik618e21d2007-07-11 10:18:17 -04006595}
6596
Chris Mason39279cc2007-06-12 06:35:45 -04006597static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006598 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006599{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006600 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006601 struct btrfs_trans_handle *trans;
6602 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006603 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006604 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006605 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006606 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006607 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006608
Josef Bacik9ed74f22009-09-11 16:12:44 -04006609 /*
6610 * 2 for inode item and ref
6611 * 2 for dir items
6612 * 1 for xattr if selinux is on
6613 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006614 trans = btrfs_start_transaction(root, 5);
6615 if (IS_ERR(trans))
6616 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006617
Li Zefan581bb052011-04-20 10:06:11 +08006618 err = btrfs_find_free_ino(root, &objectid);
6619 if (err)
6620 goto out_unlock;
6621
Josef Bacikaec74772008-07-24 12:12:38 -04006622 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006623 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6624 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006625 if (IS_ERR(inode)) {
6626 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006627 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006628 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006629 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006630 /*
6631 * If the active LSM wants to access the inode during
6632 * d_instantiate it needs these. Smack checks to see
6633 * if the filesystem supports xattrs by looking at the
6634 * ops vector.
6635 */
6636 inode->i_fop = &btrfs_file_operations;
6637 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006638 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006639
6640 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6641 if (err)
6642 goto out_unlock_inode;
6643
6644 err = btrfs_update_inode(trans, root, inode);
6645 if (err)
6646 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006647
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006648 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6649 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006650 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006651 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006652
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006653 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006654 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006655 d_instantiate(dentry, inode);
6656
Chris Mason39279cc2007-06-12 06:35:45 -04006657out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006658 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006659 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006660 inode_dec_link_count(inode);
6661 iput(inode);
6662 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006663 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006664 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006665
6666out_unlock_inode:
6667 unlock_new_inode(inode);
6668 goto out_unlock;
6669
Chris Mason39279cc2007-06-12 06:35:45 -04006670}
6671
6672static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6673 struct dentry *dentry)
6674{
Filipe Manana271dba452016-01-05 16:24:05 +00006675 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006676 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006677 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006678 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006679 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006680 int err;
6681 int drop_inode = 0;
6682
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006683 /* do not allow sys_link's with other subvols of the same device */
6684 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006685 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006686
Mark Fashehf1863732012-08-08 11:32:27 -07006687 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006688 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006689
Nikolay Borisov877574e2017-02-20 13:50:33 +02006690 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006691 if (err)
6692 goto fail;
6693
Yan, Zhenga22285a2010-05-16 10:48:46 -04006694 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006695 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006696 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006697 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006698 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006699 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006700 if (IS_ERR(trans)) {
6701 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006702 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006703 goto fail;
6704 }
Chris Mason5f39d392007-10-15 16:14:19 -04006705
Miao Xie67de1172013-12-26 13:07:06 +08006706 /* There are several dir indexes for this inode, clear the cache. */
6707 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006708 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006709 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006710 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006711 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006712 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006713
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006714 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6715 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006716
Yan, Zhenga5719522009-09-24 09:17:31 -04006717 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006718 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006719 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006720 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006721 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006722 if (err)
6723 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006724 if (inode->i_nlink == 1) {
6725 /*
6726 * If new hard link count is 1, it's a file created
6727 * with open(2) O_TMPFILE flag.
6728 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006729 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006730 if (err)
6731 goto fail;
6732 }
Al Viro08c422c2011-12-23 07:58:13 -05006733 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006734 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006735 }
Chris Mason39279cc2007-06-12 06:35:45 -04006736
Chris Mason1832a6d2007-12-21 16:27:21 -05006737fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006738 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006739 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006740 if (drop_inode) {
6741 inode_dec_link_count(inode);
6742 iput(inode);
6743 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006744 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006745 return err;
6746}
6747
Al Viro18bb1db2011-07-26 01:41:39 -04006748static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006749{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006750 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006751 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006752 struct btrfs_trans_handle *trans;
6753 struct btrfs_root *root = BTRFS_I(dir)->root;
6754 int err = 0;
6755 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006756 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006757 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006758
Josef Bacik9ed74f22009-09-11 16:12:44 -04006759 /*
6760 * 2 items for inode and ref
6761 * 2 items for dir items
6762 * 1 for xattr if selinux is on
6763 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006764 trans = btrfs_start_transaction(root, 5);
6765 if (IS_ERR(trans))
6766 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006767
Li Zefan581bb052011-04-20 10:06:11 +08006768 err = btrfs_find_free_ino(root, &objectid);
6769 if (err)
6770 goto out_fail;
6771
Josef Bacikaec74772008-07-24 12:12:38 -04006772 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006773 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6774 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006775 if (IS_ERR(inode)) {
6776 err = PTR_ERR(inode);
6777 goto out_fail;
6778 }
Chris Mason5f39d392007-10-15 16:14:19 -04006779
Chris Mason39279cc2007-06-12 06:35:45 -04006780 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006781 /* these must be set before we unlock the inode */
6782 inode->i_op = &btrfs_dir_inode_operations;
6783 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006784
Eric Paris2a7dba32011-02-01 11:05:39 -05006785 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006786 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006787 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006788
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006789 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006790 err = btrfs_update_inode(trans, root, inode);
6791 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006792 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006793
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006794 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6795 dentry->d_name.name,
6796 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006797 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006798 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006799
Chris Mason39279cc2007-06-12 06:35:45 -04006800 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006801 /*
6802 * mkdir is special. We're unlocking after we call d_instantiate
6803 * to avoid a race with nfsd calling d_instantiate.
6804 */
6805 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006806 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006807
6808out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006809 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006810 if (drop_on_err) {
6811 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006812 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006813 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006814 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006815 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006816
6817out_fail_inode:
6818 unlock_new_inode(inode);
6819 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006820}
6821
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006822/* Find next extent map of a given extent map, caller needs to ensure locks */
6823static struct extent_map *next_extent_map(struct extent_map *em)
6824{
6825 struct rb_node *next;
6826
6827 next = rb_next(&em->rb_node);
6828 if (!next)
6829 return NULL;
6830 return container_of(next, struct extent_map, rb_node);
6831}
6832
6833static struct extent_map *prev_extent_map(struct extent_map *em)
6834{
6835 struct rb_node *prev;
6836
6837 prev = rb_prev(&em->rb_node);
6838 if (!prev)
6839 return NULL;
6840 return container_of(prev, struct extent_map, rb_node);
6841}
6842
Chris Masond352ac62008-09-29 15:18:18 -04006843/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006844 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006845 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006846 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006847 */
Chris Mason3b951512008-04-17 11:29:12 -04006848static int merge_extent_mapping(struct extent_map_tree *em_tree,
6849 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006850 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006851 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006852{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006853 struct extent_map *prev;
6854 struct extent_map *next;
6855 u64 start;
6856 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006857 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006858
Chris Masone6dcd2d2008-07-17 12:53:50 -04006859 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006860
6861 if (existing->start > map_start) {
6862 next = existing;
6863 prev = prev_extent_map(next);
6864 } else {
6865 prev = existing;
6866 next = next_extent_map(prev);
6867 }
6868
6869 start = prev ? extent_map_end(prev) : em->start;
6870 start = max_t(u64, start, em->start);
6871 end = next ? next->start : extent_map_end(em);
6872 end = min_t(u64, end, extent_map_end(em));
6873 start_diff = start - em->start;
6874 em->start = start;
6875 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006876 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6877 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006878 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006879 em->block_len -= start_diff;
6880 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006881 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006882}
6883
Chris Masonc8b97812008-10-29 14:49:59 -04006884static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006885 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006886 size_t pg_offset, u64 extent_offset,
6887 struct btrfs_file_extent_item *item)
6888{
6889 int ret;
6890 struct extent_buffer *leaf = path->nodes[0];
6891 char *tmp;
6892 size_t max_size;
6893 unsigned long inline_size;
6894 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006895 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006896
6897 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006898 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006899 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6900 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006901 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006902 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006903 if (!tmp)
6904 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006905 ptr = btrfs_file_extent_inline_start(item);
6906
6907 read_extent_buffer(leaf, tmp, ptr, inline_size);
6908
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006909 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006910 ret = btrfs_decompress(compress_type, tmp, page,
6911 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006912
6913 /*
6914 * decompression code contains a memset to fill in any space between the end
6915 * of the uncompressed data and the end of max_size in case the decompressed
6916 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6917 * the end of an inline extent and the beginning of the next block, so we
6918 * cover that region here.
6919 */
6920
6921 if (max_size + pg_offset < PAGE_SIZE) {
6922 char *map = kmap(page);
6923 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6924 kunmap(page);
6925 }
Chris Masonc8b97812008-10-29 14:49:59 -04006926 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006927 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006928}
6929
Chris Masond352ac62008-09-29 15:18:18 -04006930/*
6931 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006932 * the ugly parts come from merging extents from the disk with the in-ram
6933 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006934 * where the in-ram extents might be locked pending data=ordered completion.
6935 *
6936 * This also copies inline extents directly into the page.
6937 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006938struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6939 struct page *page,
6940 size_t pg_offset, u64 start, u64 len,
6941 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006942{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006943 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006944 int ret;
6945 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006946 u64 extent_start = 0;
6947 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006948 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006949 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006950 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006951 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006952 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006953 struct extent_buffer *leaf;
6954 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006955 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006956 struct extent_map_tree *em_tree = &inode->extent_tree;
6957 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006958 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006959
Chris Mason890871b2009-09-02 16:24:52 -04006960 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006961 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006962 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006963 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006964 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006965
Chris Masona52d9a82007-08-27 16:49:44 -04006966 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006967 if (em->start > start || em->start + em->len <= start)
6968 free_extent_map(em);
6969 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006970 free_extent_map(em);
6971 else
6972 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006973 }
David Sterba172ddd62011-04-21 00:48:27 +02006974 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006975 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006976 err = -ENOMEM;
6977 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006978 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006979 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006980 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006981 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006982 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006983 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006984
6985 if (!path) {
6986 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006987 if (!path) {
6988 err = -ENOMEM;
6989 goto out;
6990 }
6991 /*
6992 * Chances are we'll be called again, so go ahead and do
6993 * readahead
6994 */
David Sterbae4058b52015-11-27 16:31:35 +01006995 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006996 }
6997
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006998 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006999 if (ret < 0) {
7000 err = ret;
7001 goto out;
7002 }
7003
7004 if (ret != 0) {
7005 if (path->slots[0] == 0)
7006 goto not_found;
7007 path->slots[0]--;
7008 }
7009
Chris Mason5f39d392007-10-15 16:14:19 -04007010 leaf = path->nodes[0];
7011 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007012 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04007013 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04007014 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02007015 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04007016 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04007017 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007018 /*
7019 * If we backup past the first extent we want to move forward
7020 * and see if there is an extent in front of us, otherwise we'll
7021 * say there is a hole for our whole search range which can
7022 * cause problems.
7023 */
7024 extent_end = start;
7025 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007026 }
7027
Chris Mason5f39d392007-10-15 16:14:19 -04007028 found_type = btrfs_file_extent_type(leaf, item);
7029 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007030 if (found_type == BTRFS_FILE_EXTENT_REG ||
7031 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007032 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007033 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007034
7035 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7036 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007037 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7038 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007039 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007040 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007041 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007042
7043 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7044 path->slots[0],
7045 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007046 }
Josef Bacik25a50342013-10-14 12:08:38 -04007047next:
Yan Zheng9036c102008-10-30 14:19:41 -04007048 if (start >= extent_end) {
7049 path->slots[0]++;
7050 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7051 ret = btrfs_next_leaf(root, path);
7052 if (ret < 0) {
7053 err = ret;
7054 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007055 }
Yan Zheng9036c102008-10-30 14:19:41 -04007056 if (ret > 0)
7057 goto not_found;
7058 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007059 }
Yan Zheng9036c102008-10-30 14:19:41 -04007060 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7061 if (found_key.objectid != objectid ||
7062 found_key.type != BTRFS_EXTENT_DATA_KEY)
7063 goto not_found;
7064 if (start + len <= found_key.offset)
7065 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007066 if (start > found_key.offset)
7067 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007068 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007069 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007070 em->len = found_key.offset - start;
7071 goto not_found_em;
7072 }
7073
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007074 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007075 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007076
Yan Zhengd899e052008-10-30 14:25:28 -04007077 if (found_type == BTRFS_FILE_EXTENT_REG ||
7078 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007079 goto insert;
7080 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007081 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007082 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007083 size_t size;
7084 size_t extent_offset;
7085 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007086
Filipe Manana7ffbb592014-06-09 03:48:05 +01007087 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007088 goto out;
Yan689f9342007-10-29 11:41:07 -04007089
Chris Mason514ac8a2014-01-03 21:07:00 -08007090 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007091 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007092 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7093 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007094 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007095 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007096 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007097 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007098 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007099 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007100 if (btrfs_file_extent_compression(leaf, item) !=
7101 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007102 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007103 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007104 if (ret) {
7105 err = ret;
7106 goto out;
7107 }
Chris Masonc8b97812008-10-29 14:49:59 -04007108 } else {
7109 map = kmap(page);
7110 read_extent_buffer(leaf, map + pg_offset, ptr,
7111 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007112 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007113 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007114 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007115 copy_size);
7116 }
Chris Masonc8b97812008-10-29 14:49:59 -04007117 kunmap(page);
7118 }
Chris Mason179e29e2007-11-01 11:28:41 -04007119 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007120 }
Chris Masond1310b22008-01-24 16:13:08 -05007121 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007122 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007123 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007124 }
7125not_found:
7126 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007127 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007128 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007129not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007130 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007131insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007132 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007133 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007134 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007135 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7136 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007137 err = -EIO;
7138 goto out;
7139 }
Chris Masond1310b22008-01-24 16:13:08 -05007140
7141 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007142 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007143 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007144 /* it is possible that someone inserted the extent into the tree
7145 * while we had the lock dropped. It is also possible that
7146 * an overlapping map exists in the tree
7147 */
Chris Masona52d9a82007-08-27 16:49:44 -04007148 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007149 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007150
7151 ret = 0;
7152
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007153 existing = search_extent_mapping(em_tree, start, len);
7154 /*
7155 * existing will always be non-NULL, since there must be
7156 * extent causing the -EEXIST.
7157 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007158 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007159 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007160 em->block_start == existing->block_start) {
7161 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007162 * The existing extent map already encompasses the
7163 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007164 */
7165 free_extent_map(em);
7166 em = existing;
7167 err = 0;
7168
7169 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007170 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007171 /*
7172 * The existing extent map is the one nearest to
7173 * the [start, start + len) range which overlaps
7174 */
7175 err = merge_extent_mapping(em_tree, existing,
7176 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007177 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007178 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007179 free_extent_map(em);
7180 em = NULL;
7181 }
7182 } else {
7183 free_extent_map(em);
7184 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007185 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007186 }
Chris Masona52d9a82007-08-27 16:49:44 -04007187 }
Chris Mason890871b2009-09-02 16:24:52 -04007188 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007189out:
liubo1abe9b82011-03-24 11:18:59 +00007190
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007191 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007192
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007193 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007194 if (err) {
7195 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007196 return ERR_PTR(err);
7197 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007198 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007199 return em;
7200}
7201
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007202struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7203 struct page *page,
7204 size_t pg_offset, u64 start, u64 len,
7205 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007206{
7207 struct extent_map *em;
7208 struct extent_map *hole_em = NULL;
7209 u64 range_start = start;
7210 u64 end;
7211 u64 found;
7212 u64 found_end;
7213 int err = 0;
7214
7215 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7216 if (IS_ERR(em))
7217 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007218 /*
7219 * If our em maps to:
7220 * - a hole or
7221 * - a pre-alloc extent,
7222 * there might actually be delalloc bytes behind it.
7223 */
7224 if (em->block_start != EXTENT_MAP_HOLE &&
7225 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7226 return em;
7227 else
7228 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007229
7230 /* check to see if we've wrapped (len == -1 or similar) */
7231 end = start + len;
7232 if (end < start)
7233 end = (u64)-1;
7234 else
7235 end -= 1;
7236
7237 em = NULL;
7238
7239 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007240 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007241 end, len, EXTENT_DELALLOC, 1);
7242 found_end = range_start + found;
7243 if (found_end < range_start)
7244 found_end = (u64)-1;
7245
7246 /*
7247 * we didn't find anything useful, return
7248 * the original results from get_extent()
7249 */
7250 if (range_start > end || found_end <= start) {
7251 em = hole_em;
7252 hole_em = NULL;
7253 goto out;
7254 }
7255
7256 /* adjust the range_start to make sure it doesn't
7257 * go backwards from the start they passed in
7258 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307259 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007260 found = found_end - range_start;
7261
7262 if (found > 0) {
7263 u64 hole_start = start;
7264 u64 hole_len = len;
7265
David Sterba172ddd62011-04-21 00:48:27 +02007266 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007267 if (!em) {
7268 err = -ENOMEM;
7269 goto out;
7270 }
7271 /*
7272 * when btrfs_get_extent can't find anything it
7273 * returns one huge hole
7274 *
7275 * make sure what it found really fits our range, and
7276 * adjust to make sure it is based on the start from
7277 * the caller
7278 */
7279 if (hole_em) {
7280 u64 calc_end = extent_map_end(hole_em);
7281
7282 if (calc_end <= start || (hole_em->start > end)) {
7283 free_extent_map(hole_em);
7284 hole_em = NULL;
7285 } else {
7286 hole_start = max(hole_em->start, start);
7287 hole_len = calc_end - hole_start;
7288 }
7289 }
7290 em->bdev = NULL;
7291 if (hole_em && range_start > hole_start) {
7292 /* our hole starts before our delalloc, so we
7293 * have to return just the parts of the hole
7294 * that go until the delalloc starts
7295 */
7296 em->len = min(hole_len,
7297 range_start - hole_start);
7298 em->start = hole_start;
7299 em->orig_start = hole_start;
7300 /*
7301 * don't adjust block start at all,
7302 * it is fixed at EXTENT_MAP_HOLE
7303 */
7304 em->block_start = hole_em->block_start;
7305 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007306 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7307 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007308 } else {
7309 em->start = range_start;
7310 em->len = found;
7311 em->orig_start = range_start;
7312 em->block_start = EXTENT_MAP_DELALLOC;
7313 em->block_len = found;
7314 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007315 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007316 return hole_em;
7317 }
7318out:
7319
7320 free_extent_map(hole_em);
7321 if (err) {
7322 free_extent_map(em);
7323 return ERR_PTR(err);
7324 }
7325 return em;
7326}
7327
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007328static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7329 const u64 start,
7330 const u64 len,
7331 const u64 orig_start,
7332 const u64 block_start,
7333 const u64 block_len,
7334 const u64 orig_block_len,
7335 const u64 ram_bytes,
7336 const int type)
7337{
7338 struct extent_map *em = NULL;
7339 int ret;
7340
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007341 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007342 em = create_io_em(inode, start, len, orig_start,
7343 block_start, block_len, orig_block_len,
7344 ram_bytes,
7345 BTRFS_COMPRESS_NONE, /* compress_type */
7346 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007347 if (IS_ERR(em))
7348 goto out;
7349 }
7350 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7351 len, block_len, type);
7352 if (ret) {
7353 if (em) {
7354 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007355 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007356 start + len - 1, 0);
7357 }
7358 em = ERR_PTR(ret);
7359 }
7360 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007361
7362 return em;
7363}
7364
Josef Bacik4b46fce2010-05-23 11:00:55 -04007365static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7366 u64 start, u64 len)
7367{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007368 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007369 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007370 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007371 struct btrfs_key ins;
7372 u64 alloc_hint;
7373 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007374
Josef Bacik4b46fce2010-05-23 11:00:55 -04007375 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007376 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007377 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007378 if (ret)
7379 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007380
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007381 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7382 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007383 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007384 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007385 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007386 btrfs_free_reserved_extent(fs_info, ins.objectid,
7387 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007388
Josef Bacik4b46fce2010-05-23 11:00:55 -04007389 return em;
7390}
7391
Chris Mason46bfbb52010-05-26 11:04:10 -04007392/*
7393 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7394 * block must be cow'd
7395 */
Josef Bacik00361582013-08-14 14:02:47 -04007396noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007397 u64 *orig_start, u64 *orig_block_len,
7398 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007399{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007400 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007401 struct btrfs_path *path;
7402 int ret;
7403 struct extent_buffer *leaf;
7404 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007405 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007406 struct btrfs_file_extent_item *fi;
7407 struct btrfs_key key;
7408 u64 disk_bytenr;
7409 u64 backref_offset;
7410 u64 extent_end;
7411 u64 num_bytes;
7412 int slot;
7413 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007414 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007415
Chris Mason46bfbb52010-05-26 11:04:10 -04007416 path = btrfs_alloc_path();
7417 if (!path)
7418 return -ENOMEM;
7419
David Sterbaf85b7372017-01-20 14:54:07 +01007420 ret = btrfs_lookup_file_extent(NULL, root, path,
7421 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007422 if (ret < 0)
7423 goto out;
7424
7425 slot = path->slots[0];
7426 if (ret == 1) {
7427 if (slot == 0) {
7428 /* can't find the item, must cow */
7429 ret = 0;
7430 goto out;
7431 }
7432 slot--;
7433 }
7434 ret = 0;
7435 leaf = path->nodes[0];
7436 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007437 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007438 key.type != BTRFS_EXTENT_DATA_KEY) {
7439 /* not our file or wrong item type, must cow */
7440 goto out;
7441 }
7442
7443 if (key.offset > offset) {
7444 /* Wrong offset, must cow */
7445 goto out;
7446 }
7447
7448 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7449 found_type = btrfs_file_extent_type(leaf, fi);
7450 if (found_type != BTRFS_FILE_EXTENT_REG &&
7451 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7452 /* not a regular extent, must cow */
7453 goto out;
7454 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007455
7456 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7457 goto out;
7458
Miao Xiee77751a2013-12-27 21:11:50 +08007459 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7460 if (extent_end <= offset)
7461 goto out;
7462
Chris Mason46bfbb52010-05-26 11:04:10 -04007463 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007464 if (disk_bytenr == 0)
7465 goto out;
7466
7467 if (btrfs_file_extent_compression(leaf, fi) ||
7468 btrfs_file_extent_encryption(leaf, fi) ||
7469 btrfs_file_extent_other_encoding(leaf, fi))
7470 goto out;
7471
Chris Mason46bfbb52010-05-26 11:04:10 -04007472 backref_offset = btrfs_file_extent_offset(leaf, fi);
7473
Josef Bacik7ee9e442013-06-21 16:37:03 -04007474 if (orig_start) {
7475 *orig_start = key.offset - backref_offset;
7476 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7477 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7478 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007479
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007480 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007481 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007482
7483 num_bytes = min(offset + *len, extent_end) - offset;
7484 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7485 u64 range_end;
7486
Jeff Mahoneyda170662016-06-15 09:22:56 -04007487 range_end = round_up(offset + num_bytes,
7488 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007489 ret = test_range_bit(io_tree, offset, range_end,
7490 EXTENT_DELALLOC, 0, NULL);
7491 if (ret) {
7492 ret = -EAGAIN;
7493 goto out;
7494 }
7495 }
7496
Josef Bacik1bda19e2013-10-18 12:10:36 -04007497 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007498
7499 /*
7500 * look for other files referencing this extent, if we
7501 * find any we must cow
7502 */
Josef Bacik00361582013-08-14 14:02:47 -04007503
Liu Boe4c3b2d2017-01-30 12:25:28 -08007504 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007505 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007506 if (ret) {
7507 ret = 0;
7508 goto out;
7509 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007510
7511 /*
7512 * adjust disk_bytenr and num_bytes to cover just the bytes
7513 * in this extent we are about to write. If there
7514 * are any csums in that range we have to cow in order
7515 * to keep the csums correct
7516 */
7517 disk_bytenr += backref_offset;
7518 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007519 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7520 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007521 /*
7522 * all of the above have passed, it is safe to overwrite this extent
7523 * without cow
7524 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007525 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007526 ret = 1;
7527out:
7528 btrfs_free_path(path);
7529 return ret;
7530}
7531
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007532bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7533{
7534 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007535 bool found = false;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007536 void **pagep = NULL;
7537 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007538 unsigned long start_idx;
7539 unsigned long end_idx;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007540
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007541 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007542
7543 /*
7544 * end is the last byte in the last page. end == start is legal
7545 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007546 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007547
7548 rcu_read_lock();
7549
7550 /* Most of the code in this while loop is lifted from
7551 * find_get_page. It's been modified to begin searching from a
7552 * page and return just the first page found in that range. If the
7553 * found idx is less than or equal to the end idx then we know that
7554 * a page exists. If no pages are found or if those pages are
7555 * outside of the range then we're fine (yay!) */
7556 while (page == NULL &&
7557 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7558 page = radix_tree_deref_slot(pagep);
7559 if (unlikely(!page))
7560 break;
7561
7562 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007563 if (radix_tree_deref_retry(page)) {
7564 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007565 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007566 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007567 /*
7568 * Otherwise, shmem/tmpfs must be storing a swap entry
7569 * here as an exceptional entry: so return it without
7570 * attempting to raise page count.
7571 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007572 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007573 break; /* TODO: Is this relevant for this use case? */
7574 }
7575
Filipe Manana91405152014-06-05 13:22:24 +01007576 if (!page_cache_get_speculative(page)) {
7577 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007578 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007579 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007580
7581 /*
7582 * Has the page moved?
7583 * This is part of the lockless pagecache protocol. See
7584 * include/linux/pagemap.h for details.
7585 */
7586 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007587 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007588 page = NULL;
7589 }
7590 }
7591
7592 if (page) {
7593 if (page->index <= end_idx)
7594 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007595 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007596 }
7597
7598 rcu_read_unlock();
7599 return found;
7600}
7601
Josef Bacikeb838e72012-07-31 16:28:48 -04007602static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7603 struct extent_state **cached_state, int writing)
7604{
7605 struct btrfs_ordered_extent *ordered;
7606 int ret = 0;
7607
7608 while (1) {
7609 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007610 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007611 /*
7612 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007613 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007614 * extents in this range.
7615 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007616 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007617 lockend - lockstart + 1);
7618
7619 /*
7620 * We need to make sure there are no buffered pages in this
7621 * range either, we could have raced between the invalidate in
7622 * generic_file_direct_write and locking the extent. The
7623 * invalidate needs to happen so that reads after a write do not
7624 * get stale data.
7625 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007626 if (!ordered &&
7627 (!writing ||
7628 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007629 break;
7630
7631 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7632 cached_state, GFP_NOFS);
7633
7634 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007635 /*
7636 * If we are doing a DIO read and the ordered extent we
7637 * found is for a buffered write, we can not wait for it
7638 * to complete and retry, because if we do so we can
7639 * deadlock with concurrent buffered writes on page
7640 * locks. This happens only if our DIO read covers more
7641 * than one extent map, if at this point has already
7642 * created an ordered extent for a previous extent map
7643 * and locked its range in the inode's io tree, and a
7644 * concurrent write against that previous extent map's
7645 * range and this range started (we unlock the ranges
7646 * in the io tree only when the bios complete and
7647 * buffered writes always lock pages before attempting
7648 * to lock range in the io tree).
7649 */
7650 if (writing ||
7651 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7652 btrfs_start_ordered_extent(inode, ordered, 1);
7653 else
7654 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007655 btrfs_put_ordered_extent(ordered);
7656 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007657 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007658 * We could trigger writeback for this range (and wait
7659 * for it to complete) and then invalidate the pages for
7660 * this range (through invalidate_inode_pages2_range()),
7661 * but that can lead us to a deadlock with a concurrent
7662 * call to readpages() (a buffered read or a defrag call
7663 * triggered a readahead) on a page lock due to an
7664 * ordered dio extent we created before but did not have
7665 * yet a corresponding bio submitted (whence it can not
7666 * complete), which makes readpages() wait for that
7667 * ordered extent to complete while holding a lock on
7668 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007669 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007670 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007671 }
7672
Filipe Mananaade77022016-02-18 14:28:55 +00007673 if (ret)
7674 break;
7675
Josef Bacikeb838e72012-07-31 16:28:48 -04007676 cond_resched();
7677 }
7678
7679 return ret;
7680}
7681
Liu Bo6f9994d2017-01-31 07:50:22 -08007682/* The callers of this must take lock_extent() */
7683static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7684 u64 orig_start, u64 block_start,
7685 u64 block_len, u64 orig_block_len,
7686 u64 ram_bytes, int compress_type,
7687 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007688{
7689 struct extent_map_tree *em_tree;
7690 struct extent_map *em;
7691 struct btrfs_root *root = BTRFS_I(inode)->root;
7692 int ret;
7693
Liu Bo6f9994d2017-01-31 07:50:22 -08007694 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7695 type == BTRFS_ORDERED_COMPRESSED ||
7696 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007697 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007698
Josef Bacik69ffb542012-09-11 15:40:07 -04007699 em_tree = &BTRFS_I(inode)->extent_tree;
7700 em = alloc_extent_map();
7701 if (!em)
7702 return ERR_PTR(-ENOMEM);
7703
7704 em->start = start;
7705 em->orig_start = orig_start;
7706 em->len = len;
7707 em->block_len = block_len;
7708 em->block_start = block_start;
7709 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007710 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007711 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007712 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007713 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007714 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007715 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007716 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007717 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7718 em->compress_type = compress_type;
7719 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007720
7721 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007722 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007723 em->start + em->len - 1, 0);
7724 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007725 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007726 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007727 /*
7728 * The caller has taken lock_extent(), who could race with us
7729 * to add em?
7730 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007731 } while (ret == -EEXIST);
7732
7733 if (ret) {
7734 free_extent_map(em);
7735 return ERR_PTR(ret);
7736 }
7737
Liu Bo6f9994d2017-01-31 07:50:22 -08007738 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007739 return em;
7740}
7741
Josef Bacik4b46fce2010-05-23 11:00:55 -04007742static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7743 struct buffer_head *bh_result, int create)
7744{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007745 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007746 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007747 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307748 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007749 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007750 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007751 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007752 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007753 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007754
Miao Xie172a5042013-02-21 02:48:22 -07007755 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007756 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007757 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007758 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007759
Josef Bacikc3298612012-08-03 16:49:19 -04007760 lockstart = start;
7761 lockend = start + len - 1;
7762
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007763 if (current->journal_info) {
7764 /*
7765 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007766 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007767 * confused.
7768 */
chandan50745b02015-08-28 21:10:13 +05307769 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007770 current->journal_info = NULL;
7771 }
7772
Josef Bacikeb838e72012-07-31 16:28:48 -04007773 /*
7774 * If this errors out it's because we couldn't invalidate pagecache for
7775 * this range and we need to fallback to buffered.
7776 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007777 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7778 create)) {
7779 ret = -ENOTBLK;
7780 goto err;
7781 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007782
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007783 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007784 if (IS_ERR(em)) {
7785 ret = PTR_ERR(em);
7786 goto unlock_err;
7787 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007788
7789 /*
7790 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7791 * io. INLINE is special, and we could probably kludge it in here, but
7792 * it's still buffered so for safety lets just fall back to the generic
7793 * buffered path.
7794 *
7795 * For COMPRESSED we _have_ to read the entire extent in so we can
7796 * decompress it, so there will be buffering required no matter what we
7797 * do, so go ahead and fallback to buffered.
7798 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007799 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007800 * to buffered IO. Don't blame me, this is the price we pay for using
7801 * the generic code.
7802 */
7803 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7804 em->block_start == EXTENT_MAP_INLINE) {
7805 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007806 ret = -ENOTBLK;
7807 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007808 }
7809
7810 /* Just a good old fashioned hole, return */
7811 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7812 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7813 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007814 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007815 }
7816
7817 /*
7818 * We don't allocate a new extent in the following cases
7819 *
7820 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7821 * existing extent.
7822 * 2) The extent is marked as PREALLOC. We're good to go here and can
7823 * just use the extent.
7824 *
7825 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007826 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007827 len = min(len, em->len - (start - em->start));
7828 lockstart = start + len;
7829 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007830 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007831
7832 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7833 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7834 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007835 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007836 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007837
7838 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7839 type = BTRFS_ORDERED_PREALLOC;
7840 else
7841 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007842 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007843 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007844
Josef Bacik00361582013-08-14 14:02:47 -04007845 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007846 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007847 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007848 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007849
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007850 em2 = btrfs_create_dio_extent(inode, start, len,
7851 orig_start, block_start,
7852 len, orig_block_len,
7853 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007854 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007855 if (type == BTRFS_ORDERED_PREALLOC) {
7856 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007857 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007858 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007859 if (em2 && IS_ERR(em2)) {
7860 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007861 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007862 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007863 /*
7864 * For inode marked NODATACOW or extent marked PREALLOC,
7865 * use the existing or preallocated extent, so does not
7866 * need to adjust btrfs_space_info's bytes_may_use.
7867 */
7868 btrfs_free_reserved_data_space_noquota(inode,
7869 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007870 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007871 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007872 }
Josef Bacik00361582013-08-14 14:02:47 -04007873
Chris Mason46bfbb52010-05-26 11:04:10 -04007874 /*
7875 * this will cow the extent, reset the len in case we changed
7876 * it above
7877 */
7878 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007879 free_extent_map(em);
7880 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007881 if (IS_ERR(em)) {
7882 ret = PTR_ERR(em);
7883 goto unlock_err;
7884 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007885 len = min(len, em->len - (start - em->start));
7886unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007887 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7888 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007889 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007890 bh_result->b_bdev = em->bdev;
7891 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007892 if (create) {
7893 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7894 set_buffer_new(bh_result);
7895
7896 /*
7897 * Need to update the i_size under the extent lock so buffered
7898 * readers will get the updated i_size when we unlock.
7899 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007900 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007901 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007902
chandan50745b02015-08-28 21:10:13 +05307903 WARN_ON(dio_data->reserve < len);
7904 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007905 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307906 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007907 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007908
Josef Bacikeb838e72012-07-31 16:28:48 -04007909 /*
7910 * In the case of write we need to clear and unlock the entire range,
7911 * in the case of read we need to unlock only the end area that we
7912 * aren't using if there is any left over space.
7913 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007914 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007915 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7916 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007917 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007918 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007919 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007920 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007921
Josef Bacik4b46fce2010-05-23 11:00:55 -04007922 free_extent_map(em);
7923
7924 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007925
7926unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007927 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007928 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007929err:
chandan50745b02015-08-28 21:10:13 +05307930 if (dio_data)
7931 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007932 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007933}
7934
Omar Sandoval58efbc92017-08-22 23:45:59 -07007935static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7936 struct bio *bio,
7937 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007938{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007939 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007940 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007941
Mike Christie37226b22016-06-05 14:31:52 -05007942 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007943
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007944 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007945 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007946 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007947
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007948 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007949
Miao Xie8b110e32014-09-12 18:44:03 +08007950 return ret;
7951}
7952
7953static int btrfs_check_dio_repairable(struct inode *inode,
7954 struct bio *failed_bio,
7955 struct io_failure_record *failrec,
7956 int failed_mirror)
7957{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007958 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007959 int num_copies;
7960
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007961 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007962 if (num_copies == 1) {
7963 /*
7964 * we only have a single copy of the data, so don't bother with
7965 * all the retry and error correction code that follows. no
7966 * matter what the error is, it is very likely to persist.
7967 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007968 btrfs_debug(fs_info,
7969 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7970 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007971 return 0;
7972 }
7973
7974 failrec->failed_mirror = failed_mirror;
7975 failrec->this_mirror++;
7976 if (failrec->this_mirror == failed_mirror)
7977 failrec->this_mirror++;
7978
7979 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007980 btrfs_debug(fs_info,
7981 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7982 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007983 return 0;
7984 }
7985
7986 return 1;
7987}
7988
Omar Sandoval58efbc92017-08-22 23:45:59 -07007989static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7990 struct page *page, unsigned int pgoff,
7991 u64 start, u64 end, int failed_mirror,
7992 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007993{
7994 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007995 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7996 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007997 struct bio *bio;
7998 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007999 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07008000 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08008001 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008002 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008003
Mike Christie37226b22016-06-05 14:31:52 -05008004 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008005
8006 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8007 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07008008 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08008009
8010 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8011 failed_mirror);
8012 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008013 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008014 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08008015 }
8016
Liu Bo17347ce2017-05-15 15:33:27 -07008017 segs = bio_segments(failed_bio);
8018 if (segs > 1 ||
8019 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008020 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008021
8022 isector = start - btrfs_io_bio(failed_bio)->logical;
8023 isector >>= inode->i_sb->s_blocksize_bits;
8024 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308025 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05008026 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008027
8028 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02008029 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08008030 read_mode, failrec->this_mirror, failrec->in_validation);
8031
Omar Sandoval58efbc92017-08-22 23:45:59 -07008032 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
8033 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04008034 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008035 bio_put(bio);
8036 }
8037
Omar Sandoval58efbc92017-08-22 23:45:59 -07008038 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08008039}
8040
8041struct btrfs_retry_complete {
8042 struct completion done;
8043 struct inode *inode;
8044 u64 start;
8045 int uptodate;
8046};
8047
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008048static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008049{
8050 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008051 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008052 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008053 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008054 int i;
8055
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008056 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008057 goto end;
8058
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308059 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008060 io_tree = &BTRFS_I(inode)->io_tree;
8061 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8062 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308063
Miao Xie8b110e32014-09-12 18:44:03 +08008064 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008065 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008066 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008067 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8068 io_tree, done->start, bvec->bv_page,
8069 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008070end:
8071 complete(&done->done);
8072 bio_put(bio);
8073}
8074
Omar Sandoval58efbc92017-08-22 23:45:59 -07008075static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8076 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008077{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308078 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008079 struct bio_vec bvec;
8080 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008081 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008082 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308083 unsigned int pgoff;
8084 u32 sectorsize;
8085 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008086 blk_status_t ret;
8087 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008088
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308089 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008090 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308091
Miao Xiec1dc0892014-09-12 18:43:56 +08008092 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008093 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008094 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008095
Liu Bo17347ce2017-05-15 15:33:27 -07008096 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8097 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8098 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308099
8100next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008101 done.uptodate = 0;
8102 done.start = start;
8103 init_completion(&done.done);
8104
Liu Bo17347ce2017-05-15 15:33:27 -07008105 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308106 pgoff, start, start + sectorsize - 1,
8107 io_bio->mirror_num,
8108 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008109 if (ret) {
8110 err = ret;
8111 goto next;
8112 }
Miao Xie8b110e32014-09-12 18:44:03 +08008113
David Sterba9c17f6c2017-07-19 19:26:45 +02008114 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008115
8116 if (!done.uptodate) {
8117 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308118 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008119 }
8120
Liu Bo629ebf42017-05-15 17:20:07 -07008121next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308122 start += sectorsize;
8123
Liu Bo97bf5a52017-04-07 13:11:10 -07008124 nr_sectors--;
8125 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308126 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008127 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308128 goto next_block_or_try_again;
8129 }
Miao Xie8b110e32014-09-12 18:44:03 +08008130 }
8131
Liu Bo629ebf42017-05-15 17:20:07 -07008132 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008133}
8134
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008135static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008136{
8137 struct btrfs_retry_complete *done = bio->bi_private;
8138 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008139 struct extent_io_tree *io_tree, *failure_tree;
8140 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008141 struct bio_vec *bvec;
8142 int uptodate;
8143 int ret;
8144 int i;
8145
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008146 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008147 goto end;
8148
8149 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308150
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308151 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008152 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308153
Josef Bacik7870d082017-05-05 11:57:15 -04008154 io_tree = &BTRFS_I(inode)->io_tree;
8155 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8156
David Sterbac09abff2017-07-13 18:10:07 +02008157 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008158 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008159 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8160 bvec->bv_offset, done->start,
8161 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008162 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008163 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8164 failure_tree, io_tree, done->start,
8165 bvec->bv_page,
8166 btrfs_ino(BTRFS_I(inode)),
8167 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008168 else
8169 uptodate = 0;
8170 }
8171
8172 done->uptodate = uptodate;
8173end:
8174 complete(&done->done);
8175 bio_put(bio);
8176}
8177
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008178static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8179 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008180{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308181 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008182 struct bio_vec bvec;
8183 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008184 struct btrfs_retry_complete done;
8185 u64 start;
8186 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308187 u32 sectorsize;
8188 int nr_sectors;
8189 unsigned int pgoff;
8190 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008191 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008192 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008193 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008194
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308195 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008196 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308197
Omar Sandoval58efbc92017-08-22 23:45:59 -07008198 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008199 start = io_bio->logical;
8200 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008201 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008202
Liu Bo17347ce2017-05-15 15:33:27 -07008203 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8204 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308205
Liu Bo17347ce2017-05-15 15:33:27 -07008206 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308207next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008208 if (uptodate) {
8209 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8210 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8211 bvec.bv_page, pgoff, start, sectorsize);
8212 if (likely(!ret))
8213 goto next;
8214 }
Miao Xie8b110e32014-09-12 18:44:03 +08008215try_again:
8216 done.uptodate = 0;
8217 done.start = start;
8218 init_completion(&done.done);
8219
Omar Sandoval58efbc92017-08-22 23:45:59 -07008220 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8221 pgoff, start, start + sectorsize - 1,
8222 io_bio->mirror_num, btrfs_retry_endio,
8223 &done);
8224 if (status) {
8225 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008226 goto next;
8227 }
8228
David Sterba9c17f6c2017-07-19 19:26:45 +02008229 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008230
8231 if (!done.uptodate) {
8232 /* We might have another mirror, so try again */
8233 goto try_again;
8234 }
8235next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308236 offset += sectorsize;
8237 start += sectorsize;
8238
8239 ASSERT(nr_sectors);
8240
Liu Bo97bf5a52017-04-07 13:11:10 -07008241 nr_sectors--;
8242 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308243 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008244 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308245 goto next_block;
8246 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008247 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008248
8249 return err;
8250}
8251
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008252static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8253 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008254{
8255 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8256
8257 if (skip_csum) {
8258 if (unlikely(err))
8259 return __btrfs_correct_data_nocsum(inode, io_bio);
8260 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008261 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008262 } else {
8263 return __btrfs_subio_endio_read(inode, io_bio, err);
8264 }
8265}
8266
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008267static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008268{
8269 struct btrfs_dio_private *dip = bio->bi_private;
8270 struct inode *inode = dip->inode;
8271 struct bio *dio_bio;
8272 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008273 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008274
Liu Bo99c4e3b92017-09-15 15:06:51 -06008275 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008276 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008277
Josef Bacik4b46fce2010-05-23 11:00:55 -04008278 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008279 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008280 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008281
Josef Bacik4b46fce2010-05-23 11:00:55 -04008282 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008283
Liu Bo99c4e3b92017-09-15 15:06:51 -06008284 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008285 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008286
8287 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008288 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008289 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008290}
8291
Qu Wenruo524272602017-03-08 10:25:52 +08008292static void __endio_write_update_ordered(struct inode *inode,
8293 const u64 offset, const u64 bytes,
8294 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008295{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008296 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008297 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008298 struct btrfs_workqueue *wq;
8299 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008300 u64 ordered_offset = offset;
8301 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008302 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008303 int ret;
8304
Qu Wenruo524272602017-03-08 10:25:52 +08008305 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8306 wq = fs_info->endio_freespace_worker;
8307 func = btrfs_freespace_write_helper;
8308 } else {
8309 wq = fs_info->endio_write_workers;
8310 func = btrfs_endio_write_helper;
8311 }
8312
Chris Mason163cf092010-11-28 19:56:33 -05008313again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008314 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008315 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8316 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008317 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008318 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008319 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008320 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008321
Qu Wenruo524272602017-03-08 10:25:52 +08008322 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8323 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008324out_test:
8325 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008326 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8327 * in the range, we can exit.
8328 */
8329 if (ordered_offset == last_offset)
8330 return;
8331 /*
Chris Mason163cf092010-11-28 19:56:33 -05008332 * our bio might span multiple ordered extents. If we haven't
8333 * completed the accounting for the whole dio, go back and try again
8334 */
Filipe Manana14543772015-11-24 16:23:54 +00008335 if (ordered_offset < offset + bytes) {
8336 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008337 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008338 goto again;
8339 }
Filipe Manana14543772015-11-24 16:23:54 +00008340}
8341
8342static void btrfs_endio_direct_write(struct bio *bio)
8343{
8344 struct btrfs_dio_private *dip = bio->bi_private;
8345 struct bio *dio_bio = dip->dio_bio;
8346
Qu Wenruo524272602017-03-08 10:25:52 +08008347 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008348 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008349
Josef Bacik4b46fce2010-05-23 11:00:55 -04008350 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008351
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008352 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008353 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008354 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008355}
8356
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008357static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008358 struct bio *bio, int mirror_num,
8359 unsigned long bio_flags, u64 offset)
8360{
Josef Bacikc6100a42017-05-05 11:57:13 -04008361 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008362 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008363 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008364 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008365 return 0;
8366}
8367
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008368static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008369{
8370 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008371 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008372
Miao Xie8b110e32014-09-12 18:44:03 +08008373 if (err)
8374 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008375 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008376 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8377 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008378 (unsigned long long)bio->bi_iter.bi_sector,
8379 bio->bi_iter.bi_size, err);
8380
8381 if (dip->subio_endio)
8382 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008383
8384 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008385 dip->errors = 1;
8386
8387 /*
8388 * before atomic variable goto zero, we must make sure
8389 * dip->errors is perceived to be set.
8390 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008391 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008392 }
8393
8394 /* if there are more bios still pending for this dio, just exit */
8395 if (!atomic_dec_and_test(&dip->pending_bios))
8396 goto out;
8397
Chris Mason9be33952013-05-17 18:30:14 -04008398 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008399 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008400 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008401 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008402 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008403 }
8404out:
8405 bio_put(bio);
8406}
8407
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008408static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008409 struct btrfs_dio_private *dip,
8410 struct bio *bio,
8411 u64 file_offset)
8412{
8413 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8414 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008415 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008416
8417 /*
8418 * We load all the csum data we need when we submit
8419 * the first bio to reduce the csum tree search and
8420 * contention.
8421 */
8422 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008423 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008424 file_offset);
8425 if (ret)
8426 return ret;
8427 }
8428
8429 if (bio == dip->orig_bio)
8430 return 0;
8431
8432 file_offset -= dip->logical_offset;
8433 file_offset >>= inode->i_sb->s_blocksize_bits;
8434 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8435
8436 return 0;
8437}
8438
Omar Sandoval58efbc92017-08-22 23:45:59 -07008439static inline blk_status_t
8440__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008441 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008442{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008443 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008444 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008445 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008446 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008447
Liu Bo4c274bc2017-11-01 17:19:27 -06008448 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008449 if (async_submit)
8450 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8451
Miao Xiee65e1532010-11-22 03:04:43 +00008452 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008453
8454 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008455 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008456 if (ret)
8457 goto err;
8458 }
Miao Xiee65e1532010-11-22 03:04:43 +00008459
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008460 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008461 goto map;
8462
8463 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008464 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8465 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008466 __btrfs_submit_bio_start_direct_io,
8467 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008468 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008469 } else if (write) {
8470 /*
8471 * If we aren't doing async submit, calculate the csum of the
8472 * bio now.
8473 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008474 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008475 if (ret)
8476 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008477 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008478 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008479 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008480 if (ret)
8481 goto err;
8482 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008483map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008484 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008485err:
8486 bio_put(bio);
8487 return ret;
8488}
8489
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008490static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008491{
8492 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008493 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008494 struct bio *bio;
8495 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008496 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008497 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008498 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008499 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008500 u64 submit_len;
8501 int clone_offset = 0;
8502 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308503 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008504 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008505
Kent Overstreet4f024f32013-10-11 15:44:27 -07008506 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008507 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008508 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8509 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008510 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008511 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008512
Liu Bo725130b2017-05-16 09:51:39 -07008513 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008514 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008515 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008516 goto submit;
8517 }
8518
David Woodhouse53b381b2013-01-29 18:40:14 -05008519 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008520 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008521 async_submit = 0;
8522 else
8523 async_submit = 1;
8524
Liu Bo725130b2017-05-16 09:51:39 -07008525 /* bio split */
8526 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008527 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008528 do {
Liu Bo725130b2017-05-16 09:51:39 -07008529 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008530
Liu Bo725130b2017-05-16 09:51:39 -07008531 /*
8532 * This will never fail as it's passing GPF_NOFS and
8533 * the allocation is backed by btrfs_bioset.
8534 */
Liu Boe4770942017-05-16 10:57:14 -07008535 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008536 clone_len);
8537 bio->bi_private = dip;
8538 bio->bi_end_io = btrfs_end_dio_bio;
8539 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008540
Liu Bo725130b2017-05-16 09:51:39 -07008541 ASSERT(submit_len >= clone_len);
8542 submit_len -= clone_len;
8543 if (submit_len == 0)
8544 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008545
Liu Bo725130b2017-05-16 09:51:39 -07008546 /*
8547 * Increase the count before we submit the bio so we know
8548 * the end IO handler won't happen before we increase the
8549 * count. Otherwise, the dip might get freed before we're
8550 * done setting it up.
8551 */
8552 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008553
Linus Torvalds66ba7722017-09-09 13:27:51 -07008554 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008555 async_submit);
8556 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008557 bio_put(bio);
8558 atomic_dec(&dip->pending_bios);
8559 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008560 }
Liu Bo725130b2017-05-16 09:51:39 -07008561
8562 clone_offset += clone_len;
8563 start_sector += clone_len >> 9;
8564 file_offset += clone_len;
8565
8566 map_length = submit_len;
8567 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8568 start_sector << 9, &map_length, NULL, 0);
8569 if (ret)
8570 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008571 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008572
Josef Bacik02f57c72011-04-06 14:25:44 -04008573submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008574 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008575 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008576 return 0;
8577
8578 bio_put(bio);
8579out_err:
8580 dip->errors = 1;
8581 /*
8582 * before atomic variable goto zero, we must
8583 * make sure dip->errors is perceived to be set.
8584 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008585 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008586 if (atomic_dec_and_test(&dip->pending_bios))
8587 bio_io_error(dip->orig_bio);
8588
8589 /* bio_end_io() will handle error, so we needn't return it */
8590 return 0;
8591}
8592
Mike Christie8a4c1e42016-06-05 14:31:50 -05008593static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8594 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008595{
Filipe Manana61de7182015-07-01 12:13:10 +01008596 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008597 struct bio *bio = NULL;
8598 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008599 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008600 int ret = 0;
8601
David Sterba8b6c1d52017-06-02 17:48:13 +02008602 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008603
Miao Xiec1dc0892014-09-12 18:43:56 +08008604 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008605 if (!dip) {
8606 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008607 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008608 }
8609
8610 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008611 dip->inode = inode;
8612 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008613 dip->bytes = dio_bio->bi_iter.bi_size;
8614 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008615 bio->bi_private = dip;
8616 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008617 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008618 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008619 io_bio = btrfs_io_bio(bio);
8620 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008621
Miao Xiec1dc0892014-09-12 18:43:56 +08008622 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008623 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008624 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008625 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008626 dip->subio_endio = btrfs_subio_endio_read;
8627 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008628
Filipe Mananaf28a4922015-12-08 19:23:20 +00008629 /*
8630 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8631 * even if we fail to submit a bio, because in such case we do the
8632 * corresponding error handling below and it must not be done a second
8633 * time by btrfs_direct_IO().
8634 */
8635 if (write) {
8636 struct btrfs_dio_data *dio_data = current->journal_info;
8637
8638 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8639 dip->bytes;
8640 dio_data->unsubmitted_oe_range_start =
8641 dio_data->unsubmitted_oe_range_end;
8642 }
8643
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008644 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008645 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008646 return;
Chris Mason9be33952013-05-17 18:30:14 -04008647
Liu Bo3892ac92017-04-17 15:00:28 -07008648 if (io_bio->end_io)
8649 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008650
Josef Bacik4b46fce2010-05-23 11:00:55 -04008651free_ordered:
8652 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008653 * If we arrived here it means either we failed to submit the dip
8654 * or we either failed to clone the dio_bio or failed to allocate the
8655 * dip. If we cloned the dio_bio and allocated the dip, we can just
8656 * call bio_endio against our io_bio so that we get proper resource
8657 * cleanup if we fail to submit the dip, otherwise, we must do the
8658 * same as btrfs_endio_direct_[write|read] because we can't call these
8659 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008660 */
Liu Bo3892ac92017-04-17 15:00:28 -07008661 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008662 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008663 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008664 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008665 * and all the cleanup and final put for dio_bio (through
8666 * dio_end_io()).
8667 */
8668 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008669 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008670 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008671 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008672 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008673 file_offset,
8674 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008675 false);
Filipe Manana14543772015-11-24 16:23:54 +00008676 else
Filipe Manana61de7182015-07-01 12:13:10 +01008677 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8678 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008679
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008680 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008681 /*
8682 * Releases and cleans up our dio_bio, no need to bio_put()
8683 * nor bio_endio()/bio_io_error() against dio_bio.
8684 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008685 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008686 }
Liu Bo3892ac92017-04-17 15:00:28 -07008687 if (bio)
8688 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008689 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008690}
8691
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008692static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008693 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008694{
8695 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008696 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008697 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008698 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008699
8700 if (offset & blocksize_mask)
8701 goto out;
8702
Al Viro28060d52014-03-22 05:15:17 -04008703 if (iov_iter_alignment(iter) & blocksize_mask)
8704 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008705
Al Viro28060d52014-03-22 05:15:17 -04008706 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008707 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008708 return 0;
8709 /*
8710 * Check to make sure we don't have duplicate iov_base's in this
8711 * iovec, if so return EINVAL, otherwise we'll get csum errors
8712 * when reading back.
8713 */
8714 for (seg = 0; seg < iter->nr_segs; seg++) {
8715 for (i = seg + 1; i < iter->nr_segs; i++) {
8716 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008717 goto out;
8718 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008719 }
8720 retval = 0;
8721out:
8722 return retval;
8723}
Josef Bacikeb838e72012-07-31 16:28:48 -04008724
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008725static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008726{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008727 struct file *file = iocb->ki_filp;
8728 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008729 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308730 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008731 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008732 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008733 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008734 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008735 bool wakeup = true;
8736 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008737 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008738
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008739 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008740 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008741
Jens Axboefe0f07d2015-04-15 17:05:48 -06008742 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008743
Josef Bacik0e267c42013-07-02 10:38:02 -04008744 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008745 * The generic stuff only does filemap_write_and_wait_range, which
8746 * isn't enough if we've written compressed pages to this area, so
8747 * we need to flush the dirty pages again to make absolutely sure
8748 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008749 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008750 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008751 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8752 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008753 filemap_fdatawrite_range(inode->i_mapping, offset,
8754 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008755
Omar Sandoval6f673762015-03-16 04:33:52 -07008756 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008757 /*
8758 * If the write DIO is beyond the EOF, we need update
8759 * the isize, but it is protected by i_mutex. So we can
8760 * not unlock the i_mutex at this case.
8761 */
8762 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008763 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008764 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008765 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008766 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8767 ret = -EAGAIN;
8768 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008769 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008770 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8771 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008772 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008773 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008774
8775 /*
8776 * We need to know how many extents we reserved so that we can
8777 * do the accounting properly if we go over the number we
8778 * originally calculated. Abuse current->journal_info for this.
8779 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008780 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008781 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008782 dio_data.unsubmitted_oe_range_start = (u64)offset;
8783 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308784 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308785 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008786 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8787 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008788 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008789 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8790 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008791 }
8792
Omar Sandoval17f8c842015-03-16 04:33:50 -07008793 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008794 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008795 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008796 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008797 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308798 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008799 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008800 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308801 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008802 btrfs_delalloc_release_space(inode, data_reserved,
8803 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008804 /*
8805 * On error we might have left some ordered extents
8806 * without submitting corresponding bios for them, so
8807 * cleanup them up to avoid other tasks getting them
8808 * and waiting for them to complete forever.
8809 */
8810 if (dio_data.unsubmitted_oe_range_start <
8811 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008812 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008813 dio_data.unsubmitted_oe_range_start,
8814 dio_data.unsubmitted_oe_range_end -
8815 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008816 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008817 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008818 btrfs_delalloc_release_space(inode, data_reserved,
8819 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008820 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008821 }
Miao Xie38851cc2013-02-08 07:04:11 +00008822out:
Miao Xie2e60a512013-02-08 07:01:08 +00008823 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008824 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008825 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008826 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008827
Qu Wenruo364ecf32017-02-27 15:10:38 +08008828 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008829 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008830}
8831
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008832#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8833
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008834static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8835 __u64 start, __u64 len)
8836{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008837 int ret;
8838
8839 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8840 if (ret)
8841 return ret;
8842
David Sterba2135fb92017-06-23 04:09:57 +02008843 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008844}
8845
Chris Mason9ebefb182007-06-15 13:50:00 -04008846int btrfs_readpage(struct file *file, struct page *page)
8847{
Chris Masond1310b22008-01-24 16:13:08 -05008848 struct extent_io_tree *tree;
8849 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008850 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008851}
Chris Mason1832a6d2007-12-21 16:27:21 -05008852
Chris Mason39279cc2007-06-12 06:35:45 -04008853static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8854{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008855 struct inode *inode = page->mapping->host;
8856 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008857
8858 if (current->flags & PF_MEMALLOC) {
8859 redirty_page_for_writepage(wbc, page);
8860 unlock_page(page);
8861 return 0;
8862 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008863
8864 /*
8865 * If we are under memory pressure we will call this directly from the
8866 * VM, we need to make sure we have the inode referenced for the ordered
8867 * extent. If not just return like we didn't do anything.
8868 */
8869 if (!igrab(inode)) {
8870 redirty_page_for_writepage(wbc, page);
8871 return AOP_WRITEPAGE_ACTIVATE;
8872 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008873 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008874 btrfs_add_delayed_iput(inode);
8875 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008876}
Chris Mason39279cc2007-06-12 06:35:45 -04008877
Eric Sandeen48a3b632013-04-25 20:41:01 +00008878static int btrfs_writepages(struct address_space *mapping,
8879 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008880{
Chris Masond1310b22008-01-24 16:13:08 -05008881 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008882
Chris Masond1310b22008-01-24 16:13:08 -05008883 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba43317592017-06-23 03:46:07 +02008884 return extent_writepages(tree, mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008885}
8886
Chris Mason3ab2fb52007-11-08 10:59:22 -05008887static int
8888btrfs_readpages(struct file *file, struct address_space *mapping,
8889 struct list_head *pages, unsigned nr_pages)
8890{
Chris Masond1310b22008-01-24 16:13:08 -05008891 struct extent_io_tree *tree;
8892 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba09325842017-06-23 04:09:57 +02008893 return extent_readpages(tree, mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008894}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008895static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008896{
Chris Masond1310b22008-01-24 16:13:08 -05008897 struct extent_io_tree *tree;
8898 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008899 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008900
Chris Masond1310b22008-01-24 16:13:08 -05008901 tree = &BTRFS_I(page->mapping->host)->io_tree;
8902 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008903 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008904 if (ret == 1) {
8905 ClearPagePrivate(page);
8906 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008907 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008908 }
8909 return ret;
8910}
Chris Mason39279cc2007-06-12 06:35:45 -04008911
Chris Masone6dcd2d2008-07-17 12:53:50 -04008912static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8913{
Chris Mason98509cf2008-09-11 15:51:43 -04008914 if (PageWriteback(page) || PageDirty(page))
8915 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008916 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008917}
8918
Lukas Czernerd47992f2013-05-21 23:17:23 -04008919static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8920 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008921{
Josef Bacik5fd02042012-05-02 14:00:54 -04008922 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008923 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008924 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008925 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008926 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008927 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308928 u64 start;
8929 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008930 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008931
Chris Mason8b62b722009-09-02 16:53:46 -04008932 /*
8933 * we have the page locked, so new writeback can't start,
8934 * and the dirty bit won't be cleared while we are here.
8935 *
8936 * Wait for IO on this page so that we can safely clear
8937 * the PagePrivate2 bit and do ordered accounting
8938 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008939 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008940
Josef Bacik5fd02042012-05-02 14:00:54 -04008941 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008942 if (offset) {
8943 btrfs_releasepage(page, GFP_NOFS);
8944 return;
8945 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008946
8947 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008948 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308949again:
8950 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008951 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308952 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008953 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308954 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008955 /*
8956 * IO on this page will never be started, so we need
8957 * to account for any ordered extents now
8958 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008959 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308960 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008961 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008962 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008963 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008964 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008965 /*
8966 * whoever cleared the private bit is responsible
8967 * for the finish_ordered_io
8968 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008969 if (TestClearPagePrivate2(page)) {
8970 struct btrfs_ordered_inode_tree *tree;
8971 u64 new_len;
8972
8973 tree = &BTRFS_I(inode)->ordered_tree;
8974
8975 spin_lock_irq(&tree->lock);
8976 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308977 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008978 if (new_len < ordered->truncated_len)
8979 ordered->truncated_len = new_len;
8980 spin_unlock_irq(&tree->lock);
8981
8982 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308983 start,
8984 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008985 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008986 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008987 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008988 if (!inode_evicting) {
8989 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308990 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008991 &cached_state);
8992 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308993
8994 start = end + 1;
8995 if (start < page_end)
8996 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008997 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008998
Qu Wenruob9d0b382015-09-29 10:35:16 +08008999 /*
9000 * Qgroup reserved space handler
9001 * Page here will be either
9002 * 1) Already written to disk
9003 * In this case, its reserved space is released from data rsv map
9004 * and will be freed by delayed_ref handler finally.
9005 * So even we call qgroup_free_data(), it won't decrease reserved
9006 * space.
9007 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009008 * This means the reserved space should be freed here. However,
9009 * if a truncate invalidates the page (by clearing PageDirty)
9010 * and the page is accounted for while allocating extent
9011 * in btrfs_check_data_free_space() we let delayed_ref to
9012 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009013 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009014 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08009015 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009016 if (!inode_evicting) {
9017 clear_extent_bit(tree, page_start, page_end,
9018 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009019 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9020 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01009021 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009022
9023 __btrfs_releasepage(page, GFP_NOFS);
9024 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009025
Chris Mason4a096752008-07-21 10:29:44 -04009026 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009027 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009028 ClearPagePrivate(page);
9029 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009030 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009031 }
Chris Mason39279cc2007-06-12 06:35:45 -04009032}
9033
Chris Mason9ebefb182007-06-15 13:50:00 -04009034/*
9035 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9036 * called from a page fault handler when a page is first dirtied. Hence we must
9037 * be careful to check for EOF conditions here. We set the page up correctly
9038 * for a written page which means we get ENOSPC checking when writing into
9039 * holes and correct delalloc and unwritten extent mapping on filesystems that
9040 * support these features.
9041 *
9042 * We are not allowed to take the i_mutex here so we have to play games to
9043 * protect against truncate races as the page could now be beyond EOF. Because
9044 * vmtruncate() writes the inode size before removing pages, once we have the
9045 * page lock we can determine safely if the page is beyond EOF. If it is not
9046 * beyond EOF, then the page is guaranteed safe against truncation until we
9047 * unlock the page.
9048 */
Dave Jiang11bac802017-02-24 14:56:41 -08009049int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009050{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009051 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009052 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009053 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009054 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9055 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009056 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009057 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009058 char *kaddr;
9059 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009060 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009061 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009062 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309063 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009064 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009065 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309066 u64 end;
9067
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009068 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009069
Jan Karab2b5ef52012-06-12 16:20:45 +02009070 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009071 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009072 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309073 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009074
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309075 /*
9076 * Reserving delalloc space after obtaining the page lock can lead to
9077 * deadlock. For example, if a dirty page is locked by this function
9078 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9079 * dirty page write out, then the btrfs_writepage() function could
9080 * end up waiting indefinitely to get a lock on the page currently
9081 * being processed by btrfs_page_mkwrite() function.
9082 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009083 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309084 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009085 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009086 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009087 reserved = 1;
9088 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009089 if (ret) {
9090 if (ret == -ENOMEM)
9091 ret = VM_FAULT_OOM;
9092 else /* -ENOSPC, -EIO, etc */
9093 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009094 if (reserved)
9095 goto out;
9096 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009097 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009098
Nick Piggin56a76f82009-03-31 15:23:23 -07009099 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009100again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009101 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009102 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009103
Chris Mason9ebefb182007-06-15 13:50:00 -04009104 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009105 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009106 /* page got truncated out from underneath us */
9107 goto out_unlock;
9108 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009109 wait_on_page_writeback(page);
9110
David Sterbaff13db42015-12-03 14:30:40 +01009111 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009112 set_page_extent_mapped(page);
9113
Chris Masoneb84ae02008-07-17 13:53:27 -04009114 /*
9115 * we can't set the delalloc bits if there are pending ordered
9116 * extents. Drop our locks and wait for them to finish
9117 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009118 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9119 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009120 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009121 unlock_extent_cached(io_tree, page_start, page_end,
9122 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009123 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009124 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009125 btrfs_put_ordered_extent(ordered);
9126 goto again;
9127 }
9128
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009129 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009130 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009131 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009132 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309133 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009134 btrfs_delalloc_release_space(inode, data_reserved,
9135 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309136 }
9137 }
9138
Josef Bacikfbf19082009-10-01 17:10:23 -04009139 /*
Liu Bo54160342016-12-13 12:15:19 -08009140 * page_mkwrite gets called when the page is firstly dirtied after it's
9141 * faulted in, but write(2) could also dirty a page and set delalloc
9142 * bits, thus in this case for space account reason, we still need to
9143 * clear any delalloc bits within this page range since we have to
9144 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009145 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309146 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009147 EXTENT_DIRTY | EXTENT_DELALLOC |
9148 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009149 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009150
Filipe Mananae3b8a482017-11-04 00:16:59 +00009151 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009152 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009153 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009154 unlock_extent_cached(io_tree, page_start, page_end,
9155 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009156 ret = VM_FAULT_SIGBUS;
9157 goto out_unlock;
9158 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009159 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009160
9161 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009162 if (page_start + PAGE_SIZE > size)
9163 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009164 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009165 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009166
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009167 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009168 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009169 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009170 flush_dcache_page(page);
9171 kunmap(page);
9172 }
Chris Mason247e7432008-07-17 12:53:51 -04009173 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009174 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009175 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009176
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009177 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009178 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009179 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009180
Josef Bacik2ac55d42010-02-03 19:33:23 +00009181 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009182
9183out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009184 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009185 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009186 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009187 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009188 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009189 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009190 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009191out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009192 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009193 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9194 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009195out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009196 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009197 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009198 return ret;
9199}
9200
Josef Bacika41ad392011-01-31 15:30:16 -05009201static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009202{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009203 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009204 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009205 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009206 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009207 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009208 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009209 u64 mask = fs_info->sectorsize - 1;
9210 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009211
Josef Bacik0ef8b722013-10-25 16:13:35 -04009212 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9213 (u64)-1);
9214 if (ret)
9215 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009216
Josef Bacikfcb80c22011-05-03 10:40:22 -04009217 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009218 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009219 * 3 things going on here
9220 *
9221 * 1) We need to reserve space for our orphan item and the space to
9222 * delete our orphan item. Lord knows we don't want to have a dangling
9223 * orphan item because we didn't reserve space to remove it.
9224 *
9225 * 2) We need to reserve space to update our inode.
9226 *
9227 * 3) We need to have something to cache all the space that is going to
9228 * be free'd up by the truncate operation, but also have some slack
9229 * space reserved in case it uses space during the truncate (thank you
9230 * very much snapshotting).
9231 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009232 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009233 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009234 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009235 * doesn't end up using space reserved for updating the inode or
9236 * removing the orphan item. We also need to be able to stop the
9237 * transaction and start a new one, which means we need to be able to
9238 * update the inode several times, and we have no idea of knowing how
9239 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009240 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009241 * Then there is the orphan item, which does indeed need to be held on
9242 * to for the whole operation, and we need nobody to touch this reserved
9243 * space except the orphan code.
9244 *
9245 * So that leaves us with
9246 *
9247 * 1) root->orphan_block_rsv - for the orphan deletion.
9248 * 2) rsv - for the truncate reservation, which we will steal from the
9249 * transaction reservation.
9250 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9251 * updating the inode.
9252 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009253 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009254 if (!rsv)
9255 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009256 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009257 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009258
Josef Bacik907cbce2011-08-08 13:46:15 -04009259 /*
Josef Bacik07127182011-08-19 10:29:59 -04009260 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009261 * 1 for updating the inode.
9262 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009263 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009264 if (IS_ERR(trans)) {
9265 err = PTR_ERR(trans);
9266 goto out;
9267 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009268
Josef Bacik907cbce2011-08-08 13:46:15 -04009269 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009270 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009271 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009272 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009273
Chris Mason5a3f23d2009-03-31 13:27:11 -04009274 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009275 * So if we truncate and then write and fsync we normally would just
9276 * write the extents that changed, which is a problem if we need to
9277 * first truncate that entire inode. So set this flag so we write out
9278 * all of the extents in the inode to the sync log so we're completely
9279 * safe.
9280 */
9281 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009282 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009283
Yan, Zheng80825102009-11-12 09:35:36 +00009284 while (1) {
9285 ret = btrfs_truncate_inode_items(trans, root, inode,
9286 inode->i_size,
9287 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009288 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009289 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009290 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009291 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009292 }
Chris Mason39279cc2007-06-12 06:35:45 -04009293
Yan, Zheng80825102009-11-12 09:35:36 +00009294 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009295 if (ret) {
9296 err = ret;
9297 break;
9298 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009299
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009300 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009301 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009302
9303 trans = btrfs_start_transaction(root, 2);
9304 if (IS_ERR(trans)) {
9305 ret = err = PTR_ERR(trans);
9306 trans = NULL;
9307 break;
9308 }
9309
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009310 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009311 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009312 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009313 BUG_ON(ret); /* shouldn't happen */
9314 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009315 }
9316
Josef Bacikddfae632017-10-19 14:16:02 -04009317 /*
9318 * We can't call btrfs_truncate_block inside a trans handle as we could
9319 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9320 * we've truncated everything except the last little bit, and can do
9321 * btrfs_truncate_block and then update the disk_i_size.
9322 */
9323 if (ret == NEED_TRUNCATE_BLOCK) {
9324 btrfs_end_transaction(trans);
9325 btrfs_btree_balance_dirty(fs_info);
9326
9327 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9328 if (ret)
9329 goto out;
9330 trans = btrfs_start_transaction(root, 1);
9331 if (IS_ERR(trans)) {
9332 ret = PTR_ERR(trans);
9333 goto out;
9334 }
9335 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9336 }
9337
Yan, Zheng80825102009-11-12 09:35:36 +00009338 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009339 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009340 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009341 if (ret)
9342 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009343 }
9344
Chris Mason917c16b2011-11-08 14:49:59 -05009345 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009346 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009347 ret = btrfs_update_inode(trans, root, inode);
9348 if (ret && !err)
9349 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009350
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009351 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009352 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009353 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009354out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009355 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009356
Josef Bacik3893e332011-01-31 16:03:11 -05009357 if (ret && !err)
9358 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009359
Josef Bacik3893e332011-01-31 16:03:11 -05009360 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009361}
9362
Sven Wegener3b963622008-06-09 21:57:42 -04009363/*
Chris Masond352ac62008-09-29 15:18:18 -04009364 * create a new subvolume directory/inode (helper for the ioctl).
9365 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009366int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009367 struct btrfs_root *new_root,
9368 struct btrfs_root *parent_root,
9369 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009370{
Chris Mason39279cc2007-06-12 06:35:45 -04009371 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009372 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009373 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009374
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009375 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9376 new_dirid, new_dirid,
9377 S_IFDIR | (~current_umask() & S_IRWXUGO),
9378 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009379 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009380 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009381 inode->i_op = &btrfs_dir_inode_operations;
9382 inode->i_fop = &btrfs_dir_file_operations;
9383
Miklos Szeredibfe86842011-10-28 14:13:29 +02009384 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009385 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009386 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009387
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009388 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9389 if (err)
9390 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009391 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009392 new_root->root_key.objectid, err);
9393
Yan, Zheng76dda932009-09-21 16:00:26 -04009394 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009395
Yan, Zheng76dda932009-09-21 16:00:26 -04009396 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009397 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009398}
9399
Chris Mason39279cc2007-06-12 06:35:45 -04009400struct inode *btrfs_alloc_inode(struct super_block *sb)
9401{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009402 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009403 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009404 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009405
David Sterba712e36c2017-10-31 17:08:27 +01009406 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009407 if (!ei)
9408 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009409
9410 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009411 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009412 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009413 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009414 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009415 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009416 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009417 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009418 ei->disk_i_size = 0;
9419 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009420 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009421 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009422 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009423 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009424 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009425 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009426
Josef Bacik9e0baf62011-07-15 15:16:44 +00009427 spin_lock_init(&ei->lock);
9428 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009429 if (sb->s_magic != BTRFS_TEST_MAGIC)
9430 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9431 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009432 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009433 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009434 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009435
Miao Xie16cdcec2011-04-22 18:12:22 +08009436 ei->delayed_node = NULL;
9437
chandan r9cc97d62012-07-04 12:48:07 +05309438 ei->i_otime.tv_sec = 0;
9439 ei->i_otime.tv_nsec = 0;
9440
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009441 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009442 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009443 extent_io_tree_init(&ei->io_tree, inode);
9444 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009445 ei->io_tree.track_uptodate = 1;
9446 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009447 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009448 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009449 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009450 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009451 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009452 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009453 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009454 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009455
9456 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009457}
9458
Josef Bacikaaedb552013-10-11 14:44:09 -04009459#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9460void btrfs_test_destroy_inode(struct inode *inode)
9461{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009462 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009463 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9464}
9465#endif
9466
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009467static void btrfs_i_callback(struct rcu_head *head)
9468{
9469 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009470 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9471}
9472
Chris Mason39279cc2007-06-12 06:35:45 -04009473void btrfs_destroy_inode(struct inode *inode)
9474{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009475 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009476 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009477 struct btrfs_root *root = BTRFS_I(inode)->root;
9478
Al Virob3d9b7a2012-06-09 13:51:19 -04009479 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009480 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009481 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9482 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009483 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009484 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009485 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009486 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009487 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009488
Chris Mason5a3f23d2009-03-31 13:27:11 -04009489 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009490 * This can happen where we create an inode, but somebody else also
9491 * created the same inode and we need to destroy the one we already
9492 * created.
9493 */
9494 if (!root)
9495 goto free;
9496
Josef Bacik8a35d952012-05-23 14:26:42 -04009497 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9498 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009499 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009500 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009501 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009502 }
Josef Bacik7b128762008-07-24 12:17:14 -04009503
Chris Masond3977122009-01-05 21:25:51 -05009504 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009505 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9506 if (!ordered)
9507 break;
9508 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009509 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009510 "found ordered extent %llu %llu on inode cleanup",
9511 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009512 btrfs_remove_ordered_extent(inode, ordered);
9513 btrfs_put_ordered_extent(ordered);
9514 btrfs_put_ordered_extent(ordered);
9515 }
9516 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009517 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009518 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009519 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009520free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009521 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009522}
9523
Al Viro45321ac2010-06-07 13:43:19 -04009524int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009525{
9526 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009527
Naohiro Aota6379ef92013-06-06 09:56:34 +00009528 if (root == NULL)
9529 return 1;
9530
Liu Bofa6ac872013-02-20 14:10:23 +00009531 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009532 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009533 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009534 else
Al Viro45321ac2010-06-07 13:43:19 -04009535 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009536}
9537
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009538static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009539{
9540 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9541
9542 inode_init_once(&ei->vfs_inode);
9543}
9544
9545void btrfs_destroy_cachep(void)
9546{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009547 /*
9548 * Make sure all delayed rcu free inodes are flushed before we
9549 * destroy cache.
9550 */
9551 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009552 kmem_cache_destroy(btrfs_inode_cachep);
9553 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009554 kmem_cache_destroy(btrfs_path_cachep);
9555 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009556}
9557
Liu Bof5c29bd2017-11-02 17:21:50 -06009558int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009559{
David Sterba837e1972012-09-07 03:00:48 -06009560 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009561 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009562 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9563 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009564 if (!btrfs_inode_cachep)
9565 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009566
David Sterba837e1972012-09-07 03:00:48 -06009567 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009568 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009569 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009570 if (!btrfs_trans_handle_cachep)
9571 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009572
David Sterba837e1972012-09-07 03:00:48 -06009573 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009574 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009575 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009576 if (!btrfs_path_cachep)
9577 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009578
David Sterba837e1972012-09-07 03:00:48 -06009579 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009580 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009581 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009582 if (!btrfs_free_space_cachep)
9583 goto fail;
9584
Chris Mason39279cc2007-06-12 06:35:45 -04009585 return 0;
9586fail:
9587 btrfs_destroy_cachep();
9588 return -ENOMEM;
9589}
9590
David Howellsa528d352017-01-31 16:46:22 +00009591static int btrfs_getattr(const struct path *path, struct kstat *stat,
9592 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009593{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009594 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009595 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009596 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009597 u32 bi_flags = BTRFS_I(inode)->flags;
9598
9599 stat->result_mask |= STATX_BTIME;
9600 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9601 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9602 if (bi_flags & BTRFS_INODE_APPEND)
9603 stat->attributes |= STATX_ATTR_APPEND;
9604 if (bi_flags & BTRFS_INODE_COMPRESS)
9605 stat->attributes |= STATX_ATTR_COMPRESSED;
9606 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9607 stat->attributes |= STATX_ATTR_IMMUTABLE;
9608 if (bi_flags & BTRFS_INODE_NODUMP)
9609 stat->attributes |= STATX_ATTR_NODUMP;
9610
9611 stat->attributes_mask |= (STATX_ATTR_APPEND |
9612 STATX_ATTR_COMPRESSED |
9613 STATX_ATTR_IMMUTABLE |
9614 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009615
Chris Mason39279cc2007-06-12 06:35:45 -04009616 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009617 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009618
9619 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009620 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009621 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009622 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009623 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009624 return 0;
9625}
9626
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009627static int btrfs_rename_exchange(struct inode *old_dir,
9628 struct dentry *old_dentry,
9629 struct inode *new_dir,
9630 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009631{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009632 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009633 struct btrfs_trans_handle *trans;
9634 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009635 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009636 struct inode *new_inode = new_dentry->d_inode;
9637 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009638 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009639 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009640 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9641 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009642 u64 old_idx = 0;
9643 u64 new_idx = 0;
9644 u64 root_objectid;
9645 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009646 bool root_log_pinned = false;
9647 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009648
9649 /* we only allow rename subvolume link between subvolumes */
9650 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9651 return -EXDEV;
9652
9653 /* close the race window with snapshot create/destroy ioctl */
9654 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009655 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009656 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009657 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009658
9659 /*
9660 * We want to reserve the absolute worst case amount of items. So if
9661 * both inodes are subvols and we need to unlink them then that would
9662 * require 4 item modifications, but if they are both normal inodes it
9663 * would require 5 item modifications, so we'll assume their normal
9664 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9665 * should cover the worst case number of items we'll modify.
9666 */
9667 trans = btrfs_start_transaction(root, 12);
9668 if (IS_ERR(trans)) {
9669 ret = PTR_ERR(trans);
9670 goto out_notrans;
9671 }
9672
9673 /*
9674 * We need to find a free sequence number both in the source and
9675 * in the destination directory for the exchange.
9676 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009677 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009678 if (ret)
9679 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009680 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009681 if (ret)
9682 goto out_fail;
9683
9684 BTRFS_I(old_inode)->dir_index = 0ULL;
9685 BTRFS_I(new_inode)->dir_index = 0ULL;
9686
9687 /* Reference for the source. */
9688 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9689 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009690 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009691 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009692 btrfs_pin_log_trans(root);
9693 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009694 ret = btrfs_insert_inode_ref(trans, dest,
9695 new_dentry->d_name.name,
9696 new_dentry->d_name.len,
9697 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009698 btrfs_ino(BTRFS_I(new_dir)),
9699 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009700 if (ret)
9701 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009702 }
9703
9704 /* And now for the dest. */
9705 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9706 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009707 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009708 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009709 btrfs_pin_log_trans(dest);
9710 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009711 ret = btrfs_insert_inode_ref(trans, root,
9712 old_dentry->d_name.name,
9713 old_dentry->d_name.len,
9714 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009715 btrfs_ino(BTRFS_I(old_dir)),
9716 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009717 if (ret)
9718 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009719 }
9720
9721 /* Update inode version and ctime/mtime. */
9722 inode_inc_iversion(old_dir);
9723 inode_inc_iversion(new_dir);
9724 inode_inc_iversion(old_inode);
9725 inode_inc_iversion(new_inode);
9726 old_dir->i_ctime = old_dir->i_mtime = ctime;
9727 new_dir->i_ctime = new_dir->i_mtime = ctime;
9728 old_inode->i_ctime = ctime;
9729 new_inode->i_ctime = ctime;
9730
9731 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009732 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9733 BTRFS_I(old_inode), 1);
9734 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9735 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009736 }
9737
9738 /* src is a subvolume */
9739 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9740 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9741 ret = btrfs_unlink_subvol(trans, root, old_dir,
9742 root_objectid,
9743 old_dentry->d_name.name,
9744 old_dentry->d_name.len);
9745 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009746 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9747 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009748 old_dentry->d_name.name,
9749 old_dentry->d_name.len);
9750 if (!ret)
9751 ret = btrfs_update_inode(trans, root, old_inode);
9752 }
9753 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009754 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009755 goto out_fail;
9756 }
9757
9758 /* dest is a subvolume */
9759 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9760 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9761 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9762 root_objectid,
9763 new_dentry->d_name.name,
9764 new_dentry->d_name.len);
9765 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009766 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9767 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009768 new_dentry->d_name.name,
9769 new_dentry->d_name.len);
9770 if (!ret)
9771 ret = btrfs_update_inode(trans, dest, new_inode);
9772 }
9773 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009774 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009775 goto out_fail;
9776 }
9777
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009778 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009779 new_dentry->d_name.name,
9780 new_dentry->d_name.len, 0, old_idx);
9781 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009782 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009783 goto out_fail;
9784 }
9785
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009786 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009787 old_dentry->d_name.name,
9788 old_dentry->d_name.len, 0, new_idx);
9789 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009790 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009791 goto out_fail;
9792 }
9793
9794 if (old_inode->i_nlink == 1)
9795 BTRFS_I(old_inode)->dir_index = old_idx;
9796 if (new_inode->i_nlink == 1)
9797 BTRFS_I(new_inode)->dir_index = new_idx;
9798
Filipe Manana86e8aa02016-05-05 02:02:27 +01009799 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009800 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009801 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9802 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009803 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009804 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009805 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009806 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009807 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009808 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9809 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009810 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009811 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009812 }
9813out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009814 /*
9815 * If we have pinned a log and an error happened, we unpin tasks
9816 * trying to sync the log and force them to fallback to a transaction
9817 * commit if the log currently contains any of the inodes involved in
9818 * this rename operation (to ensure we do not persist a log with an
9819 * inconsistent state for any of these inodes or leading to any
9820 * inconsistencies when replayed). If the transaction was aborted, the
9821 * abortion reason is propagated to userspace when attempting to commit
9822 * the transaction. If the log does not contain any of these inodes, we
9823 * allow the tasks to sync it.
9824 */
9825 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009826 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9827 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9828 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009829 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009830 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009831 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009832
9833 if (root_log_pinned) {
9834 btrfs_end_log_trans(root);
9835 root_log_pinned = false;
9836 }
9837 if (dest_log_pinned) {
9838 btrfs_end_log_trans(dest);
9839 dest_log_pinned = false;
9840 }
9841 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009842 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009843out_notrans:
9844 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009845 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009846 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009847 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009848
9849 return ret;
9850}
9851
9852static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9853 struct btrfs_root *root,
9854 struct inode *dir,
9855 struct dentry *dentry)
9856{
9857 int ret;
9858 struct inode *inode;
9859 u64 objectid;
9860 u64 index;
9861
9862 ret = btrfs_find_free_ino(root, &objectid);
9863 if (ret)
9864 return ret;
9865
9866 inode = btrfs_new_inode(trans, root, dir,
9867 dentry->d_name.name,
9868 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009869 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009870 objectid,
9871 S_IFCHR | WHITEOUT_MODE,
9872 &index);
9873
9874 if (IS_ERR(inode)) {
9875 ret = PTR_ERR(inode);
9876 return ret;
9877 }
9878
9879 inode->i_op = &btrfs_special_inode_operations;
9880 init_special_inode(inode, inode->i_mode,
9881 WHITEOUT_DEV);
9882
9883 ret = btrfs_init_inode_security(trans, inode, dir,
9884 &dentry->d_name);
9885 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009886 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009887
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009888 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9889 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009890 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009891 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009892
9893 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009894out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009895 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009896 if (ret)
9897 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009898 iput(inode);
9899
Filipe Mananac9901612016-05-05 01:41:57 +01009900 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009901}
9902
Chris Mason39279cc2007-06-12 06:35:45 -04009903static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009904 struct inode *new_dir, struct dentry *new_dentry,
9905 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009906{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009907 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009908 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009909 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009910 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9911 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009912 struct inode *new_inode = d_inode(new_dentry);
9913 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009914 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009915 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009916 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009917 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009918 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009919
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009920 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009921 return -EPERM;
9922
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009923 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009924 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009925 return -EXDEV;
9926
Li Zefan33345d012011-04-20 10:31:50 +08009927 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009928 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009929 return -ENOTEMPTY;
9930
Chris Mason39279cc2007-06-12 06:35:45 -04009931 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009932 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009933 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009934
9935
9936 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009937 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009938 new_dentry->d_name.name,
9939 new_dentry->d_name.len);
9940
9941 if (ret) {
9942 if (ret == -EEXIST) {
9943 /* we shouldn't get
9944 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309945 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009946 return ret;
9947 }
9948 } else {
9949 /* maybe -EOVERFLOW */
9950 return ret;
9951 }
9952 }
9953 ret = 0;
9954
Chris Mason5a3f23d2009-03-31 13:27:11 -04009955 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009956 * we're using rename to replace one file with another. Start IO on it
9957 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009958 */
Chris Mason8d875f92014-08-12 10:47:42 -07009959 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009960 filemap_flush(old_inode->i_mapping);
9961
Yan, Zheng76dda932009-09-21 16:00:26 -04009962 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009963 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009964 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009965 /*
9966 * We want to reserve the absolute worst case amount of items. So if
9967 * both inodes are subvols and we need to unlink them then that would
9968 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009969 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009970 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9971 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009972 * If our rename has the whiteout flag, we need more 5 units for the
9973 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9974 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009975 */
Filipe Manana5062af32016-05-05 10:26:26 +01009976 trans_num_items = 11;
9977 if (flags & RENAME_WHITEOUT)
9978 trans_num_items += 5;
9979 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009980 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009981 ret = PTR_ERR(trans);
9982 goto out_notrans;
9983 }
Chris Mason5f39d392007-10-15 16:14:19 -04009984
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009985 if (dest != root)
9986 btrfs_record_root_in_trans(trans, dest);
9987
Nikolay Borisov877574e2017-02-20 13:50:33 +02009988 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009989 if (ret)
9990 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009991
Miao Xie67de1172013-12-26 13:07:06 +08009992 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009993 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009994 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009995 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009996 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009997 btrfs_pin_log_trans(root);
9998 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009999 ret = btrfs_insert_inode_ref(trans, dest,
10000 new_dentry->d_name.name,
10001 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +080010002 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010003 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010004 if (ret)
10005 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010006 }
Chris Mason5a3f23d2009-03-31 13:27:11 -040010007
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010008 inode_inc_iversion(old_dir);
10009 inode_inc_iversion(new_dir);
10010 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010011 old_dir->i_ctime = old_dir->i_mtime =
10012 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010013 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -040010014
Chris Mason12fcfd22009-03-24 10:24:20 -040010015 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +010010016 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10017 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -040010018
Li Zefan33345d012011-04-20 10:31:50 +080010019 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010020 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
10021 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
10022 old_dentry->d_name.name,
10023 old_dentry->d_name.len);
10024 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010025 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10026 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +000010027 old_dentry->d_name.name,
10028 old_dentry->d_name.len);
10029 if (!ret)
10030 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010031 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010032 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010033 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010034 goto out_fail;
10035 }
Chris Mason39279cc2007-06-12 06:35:45 -040010036
10037 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010038 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010039 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010040 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010041 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10042 root_objectid = BTRFS_I(new_inode)->location.objectid;
10043 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10044 root_objectid,
10045 new_dentry->d_name.name,
10046 new_dentry->d_name.len);
10047 BUG_ON(new_inode->i_nlink == 0);
10048 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010049 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10050 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010051 new_dentry->d_name.name,
10052 new_dentry->d_name.len);
10053 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010054 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010055 ret = btrfs_orphan_add(trans,
10056 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010057 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010058 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010059 goto out_fail;
10060 }
Chris Mason39279cc2007-06-12 06:35:45 -040010061 }
Josef Bacikaec74772008-07-24 12:12:38 -040010062
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010063 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010064 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010065 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010066 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010067 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010068 goto out_fail;
10069 }
Chris Mason39279cc2007-06-12 06:35:45 -040010070
Miao Xie67de1172013-12-26 13:07:06 +080010071 if (old_inode->i_nlink == 1)
10072 BTRFS_I(old_inode)->dir_index = index;
10073
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010074 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010075 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010076
David Sterbaf85b7372017-01-20 14:54:07 +010010077 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10078 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010079 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010080 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010081 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010082
10083 if (flags & RENAME_WHITEOUT) {
10084 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10085 old_dentry);
10086
10087 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010088 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010089 goto out_fail;
10090 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010091 }
Chris Mason39279cc2007-06-12 06:35:45 -040010092out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010093 /*
10094 * If we have pinned the log and an error happened, we unpin tasks
10095 * trying to sync the log and force them to fallback to a transaction
10096 * commit if the log currently contains any of the inodes involved in
10097 * this rename operation (to ensure we do not persist a log with an
10098 * inconsistent state for any of these inodes or leading to any
10099 * inconsistencies when replayed). If the transaction was aborted, the
10100 * abortion reason is propagated to userspace when attempting to commit
10101 * the transaction. If the log does not contain any of these inodes, we
10102 * allow the tasks to sync it.
10103 */
10104 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010105 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10106 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10107 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010108 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010109 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010110 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010111
10112 btrfs_end_log_trans(root);
10113 log_pinned = false;
10114 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010115 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010116out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010117 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010118 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010119
Chris Mason39279cc2007-06-12 06:35:45 -040010120 return ret;
10121}
10122
Miklos Szeredi80ace852014-07-23 15:15:32 +020010123static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10124 struct inode *new_dir, struct dentry *new_dentry,
10125 unsigned int flags)
10126{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010127 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010128 return -EINVAL;
10129
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010130 if (flags & RENAME_EXCHANGE)
10131 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10132 new_dentry);
10133
10134 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010135}
10136
Miao Xie8ccf6f192012-10-25 09:28:04 +000010137static void btrfs_run_delalloc_work(struct btrfs_work *work)
10138{
10139 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010140 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010141
10142 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10143 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010144 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010145 filemap_flush(inode->i_mapping);
10146 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10147 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010148 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010149
10150 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010151 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010152 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010153 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010154 complete(&delalloc_work->completion);
10155}
10156
10157struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010158 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010159{
10160 struct btrfs_delalloc_work *work;
10161
David Sterba100d5702015-12-08 14:39:32 +010010162 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010163 if (!work)
10164 return NULL;
10165
10166 init_completion(&work->completion);
10167 INIT_LIST_HEAD(&work->list);
10168 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010169 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010170 WARN_ON_ONCE(!inode);
10171 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10172 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010173
10174 return work;
10175}
10176
10177void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10178{
10179 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010180 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010181}
10182
Chris Masond352ac62008-09-29 15:18:18 -040010183/*
10184 * some fairly slow code that needs optimization. This walks the list
10185 * of all the inodes with pending delalloc and forces them to disk.
10186 */
Miao Xie6c255e62014-03-06 13:55:01 +080010187static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10188 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010189{
Chris Masonea8c2812008-08-04 23:17:27 -040010190 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010191 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010192 struct btrfs_delalloc_work *work, *next;
10193 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010194 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010195 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010196
Miao Xie8ccf6f192012-10-25 09:28:04 +000010197 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010198 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010199
Miao Xie573bfb72014-03-06 13:55:03 +080010200 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010201 spin_lock(&root->delalloc_lock);
10202 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010203 while (!list_empty(&splice)) {
10204 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010205 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010206
Miao Xieeb73c1b2013-05-15 07:48:22 +000010207 list_move_tail(&binode->delalloc_inodes,
10208 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010209 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010210 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010211 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010212 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010213 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010214 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010215
David Sterba651d4942015-11-27 19:24:16 +010010216 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010217 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010218 if (delay_iput)
10219 btrfs_add_delayed_iput(inode);
10220 else
10221 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010222 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010223 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010224 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010225 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010226 btrfs_queue_work(root->fs_info->flush_workers,
10227 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010228 ret++;
10229 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010230 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010231 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010232 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010233 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010234 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010235
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010236out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010237 list_for_each_entry_safe(work, next, &works, list) {
10238 list_del_init(&work->list);
10239 btrfs_wait_and_free_delalloc_work(work);
10240 }
10241
Miao Xieeb73c1b2013-05-15 07:48:22 +000010242 if (!list_empty_careful(&splice)) {
10243 spin_lock(&root->delalloc_lock);
10244 list_splice_tail(&splice, &root->delalloc_inodes);
10245 spin_unlock(&root->delalloc_lock);
10246 }
Miao Xie573bfb72014-03-06 13:55:03 +080010247 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010248 return ret;
10249}
10250
10251int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010253 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010254 int ret;
10255
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010256 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010257 return -EROFS;
10258
Miao Xie6c255e62014-03-06 13:55:01 +080010259 ret = __start_delalloc_inodes(root, delay_iput, -1);
10260 if (ret > 0)
10261 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010262 return ret;
10263}
10264
Miao Xie6c255e62014-03-06 13:55:01 +080010265int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10266 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010267{
10268 struct btrfs_root *root;
10269 struct list_head splice;
10270 int ret;
10271
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010272 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010273 return -EROFS;
10274
10275 INIT_LIST_HEAD(&splice);
10276
Miao Xie573bfb72014-03-06 13:55:03 +080010277 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010278 spin_lock(&fs_info->delalloc_root_lock);
10279 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010280 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010281 root = list_first_entry(&splice, struct btrfs_root,
10282 delalloc_root);
10283 root = btrfs_grab_fs_root(root);
10284 BUG_ON(!root);
10285 list_move_tail(&root->delalloc_root,
10286 &fs_info->delalloc_roots);
10287 spin_unlock(&fs_info->delalloc_root_lock);
10288
Miao Xie6c255e62014-03-06 13:55:01 +080010289 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010290 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010291 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010292 goto out;
10293
Miao Xie6c255e62014-03-06 13:55:01 +080010294 if (nr != -1) {
10295 nr -= ret;
10296 WARN_ON(nr < 0);
10297 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010298 spin_lock(&fs_info->delalloc_root_lock);
10299 }
10300 spin_unlock(&fs_info->delalloc_root_lock);
10301
Miao Xie6c255e62014-03-06 13:55:01 +080010302 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010303out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010304 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010305 spin_lock(&fs_info->delalloc_root_lock);
10306 list_splice_tail(&splice, &fs_info->delalloc_roots);
10307 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010308 }
Miao Xie573bfb72014-03-06 13:55:03 +080010309 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010310 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010311}
10312
Chris Mason39279cc2007-06-12 06:35:45 -040010313static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10314 const char *symname)
10315{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010316 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010317 struct btrfs_trans_handle *trans;
10318 struct btrfs_root *root = BTRFS_I(dir)->root;
10319 struct btrfs_path *path;
10320 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010321 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010322 int err;
10323 int drop_inode = 0;
10324 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010325 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010326 int name_len;
10327 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010328 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010329 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010330 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010331
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010332 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010333 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010334 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010335
Josef Bacik9ed74f22009-09-11 16:12:44 -040010336 /*
10337 * 2 items for inode item and ref
10338 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010339 * 1 item for updating parent inode item
10340 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010341 * 1 item for xattr if selinux is on
10342 */
Filipe Manana9269d122015-12-31 18:16:29 +000010343 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010344 if (IS_ERR(trans))
10345 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010346
Li Zefan581bb052011-04-20 10:06:11 +080010347 err = btrfs_find_free_ino(root, &objectid);
10348 if (err)
10349 goto out_unlock;
10350
Josef Bacikaec74772008-07-24 12:12:38 -040010351 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010352 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10353 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010354 if (IS_ERR(inode)) {
10355 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010356 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010357 }
Chris Mason39279cc2007-06-12 06:35:45 -040010358
Casey Schauflerad19db72011-12-15 10:09:07 -050010359 /*
10360 * If the active LSM wants to access the inode during
10361 * d_instantiate it needs these. Smack checks to see
10362 * if the filesystem supports xattrs by looking at the
10363 * ops vector.
10364 */
10365 inode->i_fop = &btrfs_file_operations;
10366 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010367 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010368 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10369
10370 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10371 if (err)
10372 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010373
Chris Mason39279cc2007-06-12 06:35:45 -040010374 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010375 if (!path) {
10376 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010377 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010378 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010379 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010380 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010381 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010382 datasize = btrfs_file_extent_calc_inline_size(name_len);
10383 err = btrfs_insert_empty_item(trans, root, path, &key,
10384 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010385 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010386 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010387 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010388 }
Chris Mason5f39d392007-10-15 16:14:19 -040010389 leaf = path->nodes[0];
10390 ei = btrfs_item_ptr(leaf, path->slots[0],
10391 struct btrfs_file_extent_item);
10392 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10393 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010394 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010395 btrfs_set_file_extent_encryption(leaf, ei, 0);
10396 btrfs_set_file_extent_compression(leaf, ei, 0);
10397 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10398 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10399
Chris Mason39279cc2007-06-12 06:35:45 -040010400 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010401 write_extent_buffer(leaf, symname, ptr, name_len);
10402 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010403 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010404
Chris Mason39279cc2007-06-12 06:35:45 -040010405 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010406 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010407 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010408 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010409 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010410 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010411 /*
10412 * Last step, add directory indexes for our symlink inode. This is the
10413 * last step to avoid extra cleanup of these indexes if an error happens
10414 * elsewhere above.
10415 */
10416 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010417 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10418 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010419 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010420 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010421 goto out_unlock_inode;
10422 }
10423
10424 unlock_new_inode(inode);
10425 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010426
10427out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010428 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010429 if (drop_inode) {
10430 inode_dec_link_count(inode);
10431 iput(inode);
10432 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010433 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010434 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010435
10436out_unlock_inode:
10437 drop_inode = 1;
10438 unlock_new_inode(inode);
10439 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010440}
Chris Mason16432982008-04-10 10:23:21 -040010441
Josef Bacik0af3d002010-06-21 14:48:16 -040010442static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10443 u64 start, u64 num_bytes, u64 min_size,
10444 loff_t actual_len, u64 *alloc_hint,
10445 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010446{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010447 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010448 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10449 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010450 struct btrfs_root *root = BTRFS_I(inode)->root;
10451 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010452 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010453 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010454 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010455 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010456 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010457 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010458 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010459
Josef Bacik0af3d002010-06-21 14:48:16 -040010460 if (trans)
10461 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010462 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010463 if (own_trans) {
10464 trans = btrfs_start_transaction(root, 3);
10465 if (IS_ERR(trans)) {
10466 ret = PTR_ERR(trans);
10467 break;
10468 }
Yan Zhengd899e052008-10-30 14:25:28 -040010469 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010470
Byongho Leeee221842015-12-15 01:42:10 +090010471 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010472 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010473 /*
10474 * If we are severely fragmented we could end up with really
10475 * small allocations, so if the allocator is returning small
10476 * chunks lets make its job easier by only searching for those
10477 * sized chunks.
10478 */
10479 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010480 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10481 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010482 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010483 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010484 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010485 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010486 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010487 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010488
Josef Bacik0b670dc2015-09-23 17:11:16 -040010489 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010490 ret = insert_reserved_file_extent(trans, inode,
10491 cur_offset, ins.objectid,
10492 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010493 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010494 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010495 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010496 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010497 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010498 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010499 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010500 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010501 break;
10502 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010503
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010504 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010505 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010506
Josef Bacik5dc562c2012-08-17 13:14:17 -040010507 em = alloc_extent_map();
10508 if (!em) {
10509 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10510 &BTRFS_I(inode)->runtime_flags);
10511 goto next;
10512 }
10513
10514 em->start = cur_offset;
10515 em->orig_start = cur_offset;
10516 em->len = ins.offset;
10517 em->block_start = ins.objectid;
10518 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010519 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010520 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010521 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010522 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10523 em->generation = trans->transid;
10524
10525 while (1) {
10526 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010527 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010528 write_unlock(&em_tree->lock);
10529 if (ret != -EEXIST)
10530 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010531 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010532 cur_offset + ins.offset - 1,
10533 0);
10534 }
10535 free_extent_map(em);
10536next:
Yan Zhengd899e052008-10-30 14:25:28 -040010537 num_bytes -= ins.offset;
10538 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010539 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010540
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010541 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010542 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010543 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010544 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010545 (actual_len > inode->i_size) &&
10546 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010547 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010548 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010549 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010550 i_size = cur_offset;
10551 i_size_write(inode, i_size);
10552 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010553 }
10554
Yan Zhengd899e052008-10-30 14:25:28 -040010555 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010556
10557 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010558 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010559 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010560 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010561 break;
10562 }
Yan Zhengd899e052008-10-30 14:25:28 -040010563
Josef Bacik0af3d002010-06-21 14:48:16 -040010564 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010565 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010566 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010567 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010568 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010569 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010570 return ret;
10571}
10572
Josef Bacik0af3d002010-06-21 14:48:16 -040010573int btrfs_prealloc_file_range(struct inode *inode, int mode,
10574 u64 start, u64 num_bytes, u64 min_size,
10575 loff_t actual_len, u64 *alloc_hint)
10576{
10577 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10578 min_size, actual_len, alloc_hint,
10579 NULL);
10580}
10581
10582int btrfs_prealloc_file_range_trans(struct inode *inode,
10583 struct btrfs_trans_handle *trans, int mode,
10584 u64 start, u64 num_bytes, u64 min_size,
10585 loff_t actual_len, u64 *alloc_hint)
10586{
10587 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10588 min_size, actual_len, alloc_hint, trans);
10589}
10590
Chris Masone6dcd2d2008-07-17 12:53:50 -040010591static int btrfs_set_page_dirty(struct page *page)
10592{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010593 return __set_page_dirty_nobuffers(page);
10594}
10595
Al Viro10556cb22011-06-20 19:28:19 -040010596static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010597{
Li Zefanb83cc962010-12-20 16:04:08 +080010598 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010599 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010600
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010601 if (mask & MAY_WRITE &&
10602 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10603 if (btrfs_root_readonly(root))
10604 return -EROFS;
10605 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10606 return -EACCES;
10607 }
Al Viro2830ba72011-06-20 19:16:29 -040010608 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010609}
Chris Mason39279cc2007-06-12 06:35:45 -040010610
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010611static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10612{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010613 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010614 struct btrfs_trans_handle *trans;
10615 struct btrfs_root *root = BTRFS_I(dir)->root;
10616 struct inode *inode = NULL;
10617 u64 objectid;
10618 u64 index;
10619 int ret = 0;
10620
10621 /*
10622 * 5 units required for adding orphan entry
10623 */
10624 trans = btrfs_start_transaction(root, 5);
10625 if (IS_ERR(trans))
10626 return PTR_ERR(trans);
10627
10628 ret = btrfs_find_free_ino(root, &objectid);
10629 if (ret)
10630 goto out;
10631
10632 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010633 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010634 if (IS_ERR(inode)) {
10635 ret = PTR_ERR(inode);
10636 inode = NULL;
10637 goto out;
10638 }
10639
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010640 inode->i_fop = &btrfs_file_operations;
10641 inode->i_op = &btrfs_file_inode_operations;
10642
10643 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010644 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10645
Chris Masonb0d5d102014-09-08 13:08:51 -070010646 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10647 if (ret)
10648 goto out_inode;
10649
10650 ret = btrfs_update_inode(trans, root, inode);
10651 if (ret)
10652 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010653 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010654 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010655 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010656
Filipe Manana5762b5c2014-08-01 00:10:32 +010010657 /*
10658 * We set number of links to 0 in btrfs_new_inode(), and here we set
10659 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10660 * through:
10661 *
10662 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10663 */
10664 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010665 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010666 d_tmpfile(dentry, inode);
10667 mark_inode_dirty(inode);
10668
10669out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010670 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010671 if (ret)
10672 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010673 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010674 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010675
10676out_inode:
10677 unlock_new_inode(inode);
10678 goto out;
10679
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010680}
10681
David Sterba20a7db82017-02-17 16:24:29 +010010682__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010683static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010684{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010685 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010686}
10687
Josef Bacikc6100a42017-05-05 11:57:13 -040010688static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10689{
10690 struct inode *inode = private_data;
10691 return btrfs_sb(inode->i_sb);
10692}
10693
10694static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10695 u64 start, u64 end)
10696{
10697 struct inode *inode = private_data;
10698 u64 isize;
10699
10700 isize = i_size_read(inode);
10701 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10702 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10703 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10704 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10705 }
10706}
10707
10708void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10709{
10710 struct inode *inode = private_data;
10711 unsigned long index = start >> PAGE_SHIFT;
10712 unsigned long end_index = end >> PAGE_SHIFT;
10713 struct page *page;
10714
10715 while (index <= end_index) {
10716 page = find_get_page(inode->i_mapping, index);
10717 ASSERT(page); /* Pages should be in the extent_io_tree */
10718 set_page_writeback(page);
10719 put_page(page);
10720 index++;
10721 }
10722}
10723
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010724static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010725 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010726 .lookup = btrfs_lookup,
10727 .create = btrfs_create,
10728 .unlink = btrfs_unlink,
10729 .link = btrfs_link,
10730 .mkdir = btrfs_mkdir,
10731 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010732 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010733 .symlink = btrfs_symlink,
10734 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010735 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010736 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010737 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010738 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010739 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010740 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010741 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010742};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010743static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010744 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010745 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010746 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010747};
Yan, Zheng76dda932009-09-21 16:00:26 -040010748
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010749static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010750 .llseek = generic_file_llseek,
10751 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010752 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010753 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010754 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010755#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010756 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010757#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010758 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010759 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010760};
10761
David Sterba20e55062015-11-19 11:42:28 +010010762static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010763 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010764 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010765 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010766 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010767 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010768 .tree_fs_info = iotree_fs_info,
10769 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010770
10771 /* optional callbacks */
10772 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010773 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010774 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010775 .set_bit_hook = btrfs_set_bit_hook,
10776 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010777 .merge_extent_hook = btrfs_merge_extent_hook,
10778 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010779 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010780};
10781
Chris Mason35054392009-01-21 13:11:13 -050010782/*
10783 * btrfs doesn't support the bmap operation because swapfiles
10784 * use bmap to make a mapping of extents in the file. They assume
10785 * these extents won't change over the life of the file and they
10786 * use the bmap result to do IO directly to the drive.
10787 *
10788 * the btrfs bmap call would return logical addresses that aren't
10789 * suitable for IO and they also will change frequently as COW
10790 * operations happen. So, swapfile + btrfs == corruption.
10791 *
10792 * For now we're avoiding this by dropping bmap.
10793 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010794static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010795 .readpage = btrfs_readpage,
10796 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010797 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010798 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010799 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010800 .invalidatepage = btrfs_invalidatepage,
10801 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010802 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010803 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010804};
10805
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010806static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010807 .readpage = btrfs_readpage,
10808 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010809 .invalidatepage = btrfs_invalidatepage,
10810 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010811};
10812
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010813static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010814 .getattr = btrfs_getattr,
10815 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010816 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010817 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010818 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010819 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010820 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010821 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010822};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010823static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010824 .getattr = btrfs_getattr,
10825 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010826 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010827 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010828 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010829 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010830 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010831};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010832static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010833 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010834 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010835 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010836 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010837 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010838 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010839};
Yan, Zheng76dda932009-09-21 16:00:26 -040010840
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010841const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010842 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010843 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010844};