blob: 25764de68b9207c428a797630ec7d4c30bad9bf3 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Herbert Xu79990962020-06-12 16:57:37 +10006#include <crypto/hash.h>
Chris Mason8f18cf12008-04-25 16:53:30 -04007#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -05008#include <linux/bio.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -04009#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040010#include <linux/fs.h>
11#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
14#include <linux/init.h>
15#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040016#include <linux/backing-dev.h>
Chris Mason39279cc2007-06-12 06:35:45 -040017#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040018#include <linux/compat.h>
Josef Bacik5103e942007-11-16 11:45:54 -050019#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040020#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040021#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020023#include <linux/ratelimit.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000024#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050025#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040026#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080027#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040028#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050029#include <linux/iversion.h>
Omar Sandovaled46ff32016-11-03 10:28:14 -070030#include <linux/swap.h>
Roman Gushchinf8e66082020-03-04 16:57:35 -080031#include <linux/migrate.h>
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +030032#include <linux/sched/mm.h>
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050033#include <linux/iomap.h>
David Sterba92d32172018-04-16 21:10:14 +020034#include <asm/unaligned.h>
David Sterba602cbe92019-08-21 18:48:25 +020035#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "ctree.h"
37#include "disk-io.h"
38#include "transaction.h"
39#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040041#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040042#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040043#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020044#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040045#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050046#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050047#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080048#include "inode-map.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000049#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080050#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040051#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040052#include "block-group.h"
Filipe Manana467dc472020-05-27 11:16:07 +010053#include "space-info.h"
Chris Mason39279cc2007-06-12 06:35:45 -040054
55struct btrfs_iget_args {
David Sterba0202e832020-05-15 19:35:59 +020056 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -040057 struct btrfs_root *root;
58};
59
Filipe Mananaf28a4922015-12-08 19:23:20 +000060struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000061 u64 reserve;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050062 loff_t length;
63 ssize_t submitted;
64 struct extent_changeset *data_reserved;
Filipe Mananaf28a4922015-12-08 19:23:20 +000065};
66
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070067static const struct inode_operations btrfs_dir_inode_operations;
68static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070069static const struct inode_operations btrfs_special_inode_operations;
70static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070071static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070072static const struct file_operations btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040073
74static struct kmem_cache *btrfs_inode_cachep;
75struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050077struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000078struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079
Eric Sandeen3972f262013-01-12 02:57:22 +000080static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000081static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040082static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Nikolay Borisov6e26c442020-06-03 08:55:14 +030083static noinline int cow_file_range(struct btrfs_inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -050084 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030085 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030086 unsigned long *nr_written, int unlock);
Nikolay Borisov4b67c112020-06-03 08:55:05 +030087static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
88 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -080089 u64 block_len, u64 orig_block_len,
90 u64 ram_bytes, int compress_type,
91 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040092
Nikolay Borisovb672b5c2020-06-03 08:55:24 +030093static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +080094 const u64 offset, const u64 bytes,
95 const bool uptodate);
96
97/*
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -050098 * btrfs_inode_lock - lock inode i_rwsem based on arguments passed
99 *
100 * ilock_flags can have the following bit set:
101 *
102 * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode
103 * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt
104 * return -EAGAIN
105 */
106int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags)
107{
108 if (ilock_flags & BTRFS_ILOCK_SHARED) {
109 if (ilock_flags & BTRFS_ILOCK_TRY) {
110 if (!inode_trylock_shared(inode))
111 return -EAGAIN;
112 else
113 return 0;
114 }
115 inode_lock_shared(inode);
116 } else {
117 if (ilock_flags & BTRFS_ILOCK_TRY) {
118 if (!inode_trylock(inode))
119 return -EAGAIN;
120 else
121 return 0;
122 }
123 inode_lock(inode);
124 }
125 return 0;
126}
127
128/*
129 * btrfs_inode_unlock - unock inode i_rwsem
130 *
131 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
132 * to decide whether the lock acquired is shared or exclusive.
133 */
134void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags)
135{
136 if (ilock_flags & BTRFS_ILOCK_SHARED)
137 inode_unlock_shared(inode);
138 else
139 inode_unlock(inode);
140}
141
142/*
Qu Wenruo524272602017-03-08 10:25:52 +0800143 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +0100144 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800145 *
146 * NOTE: caller must ensure that when an error happens, it can not call
147 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
148 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
149 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200150 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800151 */
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300152static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200153 struct page *locked_page,
154 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800155{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900156 unsigned long index = offset >> PAGE_SHIFT;
157 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200158 u64 page_start = page_offset(locked_page);
159 u64 page_end = page_start + PAGE_SIZE - 1;
160
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900161 struct page *page;
162
163 while (index <= end_index) {
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300164 page = find_get_page(inode->vfs_inode.i_mapping, index);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900165 index++;
166 if (!page)
167 continue;
168 ClearPagePrivate2(page);
169 put_page(page);
170 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200171
172 /*
173 * In case this page belongs to the delalloc range being instantiated
174 * then skip it, since the first page of a range is going to be
175 * properly cleaned up by the caller of run_delalloc_range
176 */
177 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
178 offset += PAGE_SIZE;
179 bytes -= PAGE_SIZE;
180 }
181
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300182 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800183}
184
Eric Sandeen48a3b632013-04-25 20:41:01 +0000185static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400186
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000187static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500188 struct inode *inode, struct inode *dir,
189 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500190{
191 int err;
192
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000193 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500194 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500195 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500196 return err;
197}
198
Chris Masond352ac62008-09-29 15:18:18 -0400199/*
Chris Masonc8b97812008-10-29 14:49:59 -0400200 * this does all the hard work for inserting an inline extent into
201 * the btree. The caller should have done a btrfs_drop_extents so that
202 * no overlapping inline items exist in the btree
203 */
Chris Mason40f76582014-05-21 13:35:51 -0700204static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe Manana5893dfb2020-11-04 11:07:32 +0000205 struct btrfs_path *path, bool extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400206 struct btrfs_root *root, struct inode *inode,
207 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000208 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400209 struct page **compressed_pages)
210{
Chris Masonc8b97812008-10-29 14:49:59 -0400211 struct extent_buffer *leaf;
212 struct page *page = NULL;
213 char *kaddr;
214 unsigned long ptr;
215 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400216 int ret;
217 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400218 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400219
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800220 ASSERT((compressed_size > 0 && compressed_pages) ||
221 (compressed_size == 0 && !compressed_pages));
222
Li Zefanfe3f5662011-03-28 08:30:38 +0000223 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400224 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400225
Chris Masonc8b97812008-10-29 14:49:59 -0400226 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000227
228 if (!extent_inserted) {
229 struct btrfs_key key;
230 size_t datasize;
231
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200232 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000233 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200234 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000235
236 datasize = btrfs_file_extent_calc_inline_size(cur_size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000237 ret = btrfs_insert_empty_item(trans, root, path, &key,
238 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200239 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000240 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400241 }
242 leaf = path->nodes[0];
243 ei = btrfs_item_ptr(leaf, path->slots[0],
244 struct btrfs_file_extent_item);
245 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
246 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
247 btrfs_set_file_extent_encryption(leaf, ei, 0);
248 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
249 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
250 ptr = btrfs_file_extent_inline_start(ei);
251
Li Zefan261507a02010-12-17 14:21:50 +0800252 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400253 struct page *cpage;
254 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500255 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400256 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500257 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300258 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400259
Cong Wang7ac687d2011-11-25 23:14:28 +0800260 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400261 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800262 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400263
264 i++;
265 ptr += cur_size;
266 compressed_size -= cur_size;
267 }
268 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800269 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400270 } else {
271 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300272 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400273 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800274 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100275 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400276 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800277 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300278 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400279 }
280 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000281 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400282
Yan, Zhengc2167752009-11-12 09:34:21 +0000283 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500284 * We align size to sectorsize for inline extents just for simplicity
285 * sake.
286 */
287 size = ALIGN(size, root->fs_info->sectorsize);
288 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
289 if (ret)
290 goto fail;
291
292 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000293 * we're an inline extent, so nobody can
294 * extend the file past i_size without locking
295 * a page we already have locked.
296 *
297 * We must do any isize and inode updates
298 * before we unlock the pages. Otherwise we
299 * could end up racing with unlink.
300 */
Chris Masonc8b97812008-10-29 14:49:59 -0400301 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100302 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000303
Chris Masonc8b97812008-10-29 14:49:59 -0400304fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200305 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400306}
307
308
309/*
310 * conditionally insert an inline extent into the file. This
311 * does the checks required to make sure the data is small enough
312 * to fit as an inline extent.
313 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300314static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400315 u64 end, size_t compressed_size,
316 int compress_type,
317 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400318{
Filipe Manana5893dfb2020-11-04 11:07:32 +0000319 struct btrfs_drop_extents_args drop_args = { 0 };
Nikolay Borisova0349402020-06-03 08:55:12 +0300320 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400321 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400322 struct btrfs_trans_handle *trans;
Nikolay Borisova0349402020-06-03 08:55:12 +0300323 u64 isize = i_size_read(&inode->vfs_inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400324 u64 actual_end = min(end + 1, isize);
325 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400326 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400327 u64 data_len = inline_len;
328 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000329 struct btrfs_path *path;
Chris Masonc8b97812008-10-29 14:49:59 -0400330
331 if (compressed_size)
332 data_len = compressed_size;
333
334 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400335 actual_end > fs_info->sectorsize ||
336 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400337 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400338 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400339 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400340 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400341 return 1;
342 }
343
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000344 path = btrfs_alloc_path();
345 if (!path)
346 return -ENOMEM;
347
Josef Bacik00361582013-08-14 14:02:47 -0400348 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000349 if (IS_ERR(trans)) {
350 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400351 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000352 }
Nikolay Borisova0349402020-06-03 08:55:12 +0300353 trans->block_rsv = &inode->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400354
Filipe Manana5893dfb2020-11-04 11:07:32 +0000355 drop_args.path = path;
356 drop_args.start = start;
357 drop_args.end = aligned_end;
358 drop_args.drop_cache = true;
359 drop_args.replace_extent = true;
360
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000361 if (compressed_size && compressed_pages)
Filipe Manana5893dfb2020-11-04 11:07:32 +0000362 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000363 compressed_size);
364 else
Filipe Manana5893dfb2020-11-04 11:07:32 +0000365 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000366 inline_len);
367
Filipe Manana5893dfb2020-11-04 11:07:32 +0000368 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik00361582013-08-14 14:02:47 -0400369 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400370 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400371 goto out;
372 }
Chris Masonc8b97812008-10-29 14:49:59 -0400373
374 if (isize > actual_end)
375 inline_len = min_t(u64, isize, actual_end);
Filipe Manana5893dfb2020-11-04 11:07:32 +0000376 ret = insert_inline_extent(trans, path, drop_args.extent_inserted,
Nikolay Borisova0349402020-06-03 08:55:12 +0300377 root, &inode->vfs_inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400378 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000379 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400380 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400381 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400382 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400383 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400384 ret = 1;
385 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100386 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400387
Nikolay Borisova0349402020-06-03 08:55:12 +0300388 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
389 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400390out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800391 /*
392 * Don't forget to free the reserved space, as for inlined extent
393 * it won't count as data extent, free them directly here.
394 * And at reserve time, it's always aligned to page size, so
395 * just free one page here.
396 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300397 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000398 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400399 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400400 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400401}
402
Chris Mason771ed682008-11-06 22:02:51 -0500403struct async_extent {
404 u64 start;
405 u64 ram_size;
406 u64 compressed_size;
407 struct page **pages;
408 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800409 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500410 struct list_head list;
411};
412
Nikolay Borisov97db1202019-03-12 17:20:24 +0200413struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500414 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500415 struct page *locked_page;
416 u64 start;
417 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600418 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500419 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700420 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500421 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200422 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500423};
424
Nikolay Borisov97db1202019-03-12 17:20:24 +0200425struct async_cow {
426 /* Number of chunks in flight; must be first in the structure */
427 atomic_t num_chunks;
428 struct async_chunk chunks[];
429};
430
431static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500432 u64 start, u64 ram_size,
433 u64 compressed_size,
434 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800435 unsigned long nr_pages,
436 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500437{
438 struct async_extent *async_extent;
439
440 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100441 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500442 async_extent->start = start;
443 async_extent->ram_size = ram_size;
444 async_extent->compressed_size = compressed_size;
445 async_extent->pages = pages;
446 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800447 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500448 list_add_tail(&async_extent->list, &cow->extents);
449 return 0;
450}
451
Qu Wenruo42c16da2019-07-01 05:12:46 +0000452/*
453 * Check if the inode has flags compatible with compression
454 */
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300455static inline bool inode_can_compress(struct btrfs_inode *inode)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000456{
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300457 if (inode->flags & BTRFS_INODE_NODATACOW ||
458 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000459 return false;
460 return true;
461}
462
463/*
464 * Check if the inode needs to be submitted to compression, based on mount
465 * options, defragmentation, properties or heuristics.
466 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300467static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
468 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800469{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300470 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800471
Nikolay Borisov808a1292020-06-03 08:55:27 +0300472 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000473 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
474 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300475 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000476 return 0;
477 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800478 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400479 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800480 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200481 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300482 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200483 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800484 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300485 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800486 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400487 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300488 inode->flags & BTRFS_INODE_COMPRESS ||
489 inode->prop_compress)
490 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800491 return 0;
492}
493
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200494static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800495 u64 start, u64 end, u64 num_bytes, u64 small_write)
496{
497 /* If this is a small write inside eof, kick off a defrag */
498 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200499 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800500 btrfs_add_inode_defrag(NULL, inode);
501}
502
Chris Masonc8b97812008-10-29 14:49:59 -0400503/*
Chris Mason771ed682008-11-06 22:02:51 -0500504 * we create compressed extents in two phases. The first
505 * phase compresses a range of pages that have already been
506 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400507 *
Chris Mason771ed682008-11-06 22:02:51 -0500508 * This is done inside an ordered work queue, and the compression
509 * is spread across many cpus. The actual IO submission is step
510 * two, and the ordered work queue takes care of making sure that
511 * happens in the same order things were put onto the queue by
512 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400513 *
Chris Mason771ed682008-11-06 22:02:51 -0500514 * If this code finds it can't get good compression, it puts an
515 * entry onto the work queue to write the uncompressed bytes. This
516 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300517 * are written in the same order that the flusher thread sent them
518 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400519 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300520static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400521{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200522 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400523 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400524 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200525 u64 start = async_chunk->start;
526 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400527 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400528 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400529 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400530 struct page **pages = NULL;
531 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400532 unsigned long total_compressed = 0;
533 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400534 int i;
535 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400536 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300537 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400538 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400539
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200540 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
541 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400542
Josef Bacikd98da492019-10-11 09:03:54 -0400543 /*
544 * We need to save i_size before now because it could change in between
545 * us evaluating the size and assigning it. This is because we lock and
546 * unlock the page in truncate and fallocate, and then modify the i_size
547 * later on.
548 *
549 * The barriers are to emulate READ_ONCE, remove that once i_size_read
550 * does that for us.
551 */
552 barrier();
553 i_size = i_size_read(inode);
554 barrier();
555 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400556again:
557 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300558 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100559 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
560 nr_pages = min_t(unsigned long, nr_pages,
561 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400562
Chris Masonf03d9301f2009-02-04 09:31:06 -0500563 /*
564 * we don't want to send crud past the end of i_size through
565 * compression, that's just a waste of CPU time. So, if the
566 * end of the file is before the start of our current
567 * requested range of bytes, we bail out to the uncompressed
568 * cleanup code that can deal with all of this.
569 *
570 * It isn't really the fastest way to fix things, but this is a
571 * very uncommon corner.
572 */
573 if (actual_end <= start)
574 goto cleanup_and_bail_uncompressed;
575
Chris Masonc8b97812008-10-29 14:49:59 -0400576 total_compressed = actual_end - start;
577
Shilong Wang4bcbb332014-10-07 18:44:35 -0400578 /*
579 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400580 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400581 */
582 if (total_compressed <= blocksize &&
583 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
584 goto cleanup_and_bail_uncompressed;
585
David Sterba069eac72017-02-14 19:36:54 +0100586 total_compressed = min_t(unsigned long, total_compressed,
587 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400588 total_in = 0;
589 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400590
Chris Mason771ed682008-11-06 22:02:51 -0500591 /*
592 * we do compression for mount -o compress and when the
593 * inode has not been flagged as nocompress. This flag can
594 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400595 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300596 if (inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400597 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100598 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800599 if (!pages) {
600 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100601 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800602 goto cont;
603 }
Chris Mason179e29e2007-11-01 11:28:41 -0400604
David Sterbaeec63c62017-07-17 19:41:31 +0200605 if (BTRFS_I(inode)->defrag_compress)
606 compress_type = BTRFS_I(inode)->defrag_compress;
607 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200608 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800609
Chris Mason4adaa612013-03-26 13:07:00 -0400610 /*
611 * we need to call clear_page_dirty_for_io on each
612 * page in the range. Otherwise applications with the file
613 * mmap'd can wander in and change the page contents while
614 * we are compressing them.
615 *
616 * If the compression fails for any reason, we set the pages
617 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300618 *
619 * Note that the remaining part is redirtied, the start pointer
620 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400621 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300622 if (!redirty) {
623 extent_range_clear_dirty_for_io(inode, start, end);
624 redirty = 1;
625 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200626
627 /* Compression level is applied here and only here */
628 ret = btrfs_compress_pages(
629 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800630 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100631 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100632 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800633 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100634 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400635
636 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100637 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100638 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400639 char *kaddr;
640
641 /* zero the tail end of the last page, we might be
642 * sending it down to disk
643 */
644 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800645 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400646 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300647 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800648 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400649 }
650 will_compress = 1;
651 }
652 }
Li Zefan560f7d72011-09-08 10:22:01 +0800653cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400654 if (start == 0) {
655 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300656 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400657 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500658 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400659 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300660 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
661 0, BTRFS_COMPRESS_NONE,
662 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400663 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500664 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300665 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000666 total_compressed,
667 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400668 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100669 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400670 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400671 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
672 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100673 unsigned long page_error_op;
674
Filipe Mananae6eb4312014-10-10 10:45:12 +0100675 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400676
Chris Mason771ed682008-11-06 22:02:51 -0500677 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100678 * inline extent creation worked or returned error,
679 * we don't need to create any more async work items.
680 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400681 *
682 * We use DO_ACCOUNTING here because we need the
683 * delalloc_release_metadata to be done _after_ we drop
684 * our outstanding extent for clearing delalloc for this
685 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500686 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300687 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
688 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300689 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800690 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400691 PAGE_CLEAR_DIRTY |
692 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100693 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400694 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300695
Qu Wenruo1e6e2382020-07-28 16:39:26 +0800696 /*
697 * Ensure we only free the compressed pages if we have
698 * them allocated, as we can still reach here with
699 * inode_need_compress() == false.
700 */
701 if (pages) {
702 for (i = 0; i < nr_pages; i++) {
703 WARN_ON(pages[i]->mapping);
704 put_page(pages[i]);
705 }
706 kfree(pages);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300707 }
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300708 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400709 }
710 }
711
712 if (will_compress) {
713 /*
714 * we aren't doing an inline extent round the compressed size
715 * up to a block size boundary so the allocator does sane
716 * things
717 */
Qu Wenruofda28322013-02-26 08:10:22 +0000718 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400719
720 /*
721 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300722 * win, compare the page count read with the blocks on disk,
723 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400724 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300725 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300726 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300727 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700728
729 /*
730 * The async work queues will take care of doing actual
731 * allocation on disk for these compressed pages, and
732 * will submit them to the elevator.
733 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200734 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100735 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700736 compress_type);
737
Timofey Titovets11708622017-10-03 18:06:01 +0300738 if (start + total_in < end) {
739 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700740 pages = NULL;
741 cond_resched();
742 goto again;
743 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300744 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400745 }
746 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700747 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400748 /*
749 * the compression code ran but failed to make things smaller,
750 * free any pages it allocated and our page pointer array
751 */
David Sterba4d3a8002017-02-14 19:04:07 +0100752 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400753 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300754 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400755 }
756 kfree(pages);
757 pages = NULL;
758 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100759 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400760
761 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400762 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200763 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500764 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500765 }
Chris Masonc8b97812008-10-29 14:49:59 -0400766 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500767cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700768 /*
769 * No compression, but we still need to write the pages in the file
770 * we've been given so far. redirty the locked page if it corresponds
771 * to our extent and set things up for the async work queue to run
772 * cow_file_range to do the normal delalloc dance.
773 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700774 if (async_chunk->locked_page &&
775 (page_offset(async_chunk->locked_page) >= start &&
776 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200777 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700778 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700779 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700780
781 if (redirty)
782 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200783 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700784 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300785 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500786
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300787 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500788}
Chris Mason771ed682008-11-06 22:02:51 -0500789
Filipe Manana40ae8372014-10-06 22:14:24 +0100790static void free_async_extent_pages(struct async_extent *async_extent)
791{
792 int i;
793
794 if (!async_extent->pages)
795 return;
796
797 for (i = 0; i < async_extent->nr_pages; i++) {
798 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300799 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100800 }
801 kfree(async_extent->pages);
802 async_extent->nr_pages = 0;
803 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500804}
805
806/*
807 * phase two of compressed writeback. This is the ordered portion
808 * of the code, which only gets called in the order the work was
809 * queued. We walk all the async extents created by compress_file_range
810 * and send them down to the disk.
811 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200812static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500813{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300814 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
815 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500816 struct async_extent *async_extent;
817 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500818 struct btrfs_key ins;
819 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300820 struct btrfs_root *root = inode->root;
821 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500822 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500823
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500824again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200825 while (!list_empty(&async_chunk->extents)) {
826 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500827 struct async_extent, list);
828 list_del(&async_extent->list);
829
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500830retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200831 lock_extent(io_tree, async_extent->start,
832 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500833 /* did the compression code fall back to uncompressed IO? */
834 if (!async_extent->pages) {
835 int page_started = 0;
836 unsigned long nr_written = 0;
837
Chris Mason771ed682008-11-06 22:02:51 -0500838 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300839 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500840 async_extent->start,
841 async_extent->start +
842 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300843 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500844
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100845 /* JDM XXX */
846
Chris Mason771ed682008-11-06 22:02:51 -0500847 /*
848 * if page_started, cow_file_range inserted an
849 * inline extent and took care of all the unlocking
850 * and IO for us. Otherwise, we need to submit
851 * all those pages down to the drive.
852 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500853 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300854 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200855 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500856 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500857 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500858 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700859 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200860 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500861 kfree(async_extent);
862 cond_resched();
863 continue;
864 }
865
Wang Xiaoguang18513092016-07-25 15:51:40 +0800866 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500867 async_extent->compressed_size,
868 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800869 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500870 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100871 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500872
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400873 if (ret == -ENOSPC) {
874 unlock_extent(io_tree, async_extent->start,
875 async_extent->start +
876 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800877
878 /*
879 * we need to redirty the pages if we decide to
880 * fallback to uncompressed IO, otherwise we
881 * will not submit these pages down to lower
882 * layers.
883 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300884 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800885 async_extent->start,
886 async_extent->start +
887 async_extent->ram_size - 1);
888
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100889 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400890 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500891 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500892 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000893 /*
894 * here we're doing allocation and writeback of the
895 * compressed pages
896 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300897 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800898 async_extent->ram_size, /* len */
899 async_extent->start, /* orig_start */
900 ins.objectid, /* block_start */
901 ins.offset, /* block_len */
902 ins.offset, /* orig_block_len */
903 async_extent->ram_size, /* ram_bytes */
904 async_extent->compress_type,
905 BTRFS_ORDERED_COMPRESSED);
906 if (IS_ERR(em))
907 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500908 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800909 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500910
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300911 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800912 async_extent->start,
913 ins.objectid,
914 async_extent->ram_size,
915 ins.offset,
916 BTRFS_ORDERED_COMPRESSED,
917 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100918 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300919 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100920 async_extent->start +
921 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500922 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100923 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400924 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500925
Chris Mason771ed682008-11-06 22:02:51 -0500926 /*
927 * clear dirty, set writeback and unlock the pages.
928 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300929 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400930 async_extent->start +
931 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400932 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
933 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400934 PAGE_SET_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300935 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500936 async_extent->ram_size,
937 ins.objectid,
938 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600939 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700940 async_chunk->write_flags,
941 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100942 struct page *p = async_extent->pages[0];
943 const u64 start = async_extent->start;
944 const u64 end = start + async_extent->ram_size - 1;
945
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300946 p->mapping = inode->vfs_inode.i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200947 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200948
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100949 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300950 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100951 PAGE_END_WRITEBACK |
952 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100953 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100954 }
Chris Mason771ed682008-11-06 22:02:51 -0500955 alloc_hint = ins.objectid + ins.offset;
956 kfree(async_extent);
957 cond_resched();
958 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100959 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500960out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400961 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400962 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100963out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300964 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500965 async_extent->start +
966 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400967 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100968 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400969 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
970 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100971 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
972 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100973 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100974 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500975 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500976}
977
Nikolay Borisov43c69842020-06-03 08:55:02 +0300978static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400979 u64 num_bytes)
980{
Nikolay Borisov43c69842020-06-03 08:55:02 +0300981 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400982 struct extent_map *em;
983 u64 alloc_hint = 0;
984
985 read_lock(&em_tree->lock);
986 em = search_extent_mapping(em_tree, start, num_bytes);
987 if (em) {
988 /*
989 * if block start isn't an actual block number then find the
990 * first block in this inode and use that as a hint. If that
991 * block is also bogus then just don't worry about it.
992 */
993 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
994 free_extent_map(em);
995 em = search_extent_mapping(em_tree, 0, 0);
996 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
997 alloc_hint = em->block_start;
998 if (em)
999 free_extent_map(em);
1000 } else {
1001 alloc_hint = em->block_start;
1002 free_extent_map(em);
1003 }
1004 }
1005 read_unlock(&em_tree->lock);
1006
1007 return alloc_hint;
1008}
1009
Chris Mason771ed682008-11-06 22:02:51 -05001010/*
1011 * when extent_io.c finds a delayed allocation range in the file,
1012 * the call backs end up in this code. The basic idea is to
1013 * allocate extents on disk for the range, and create ordered data structs
1014 * in ram to track those extents.
1015 *
1016 * locked_page is the page that writepage had locked already. We use
1017 * it to make sure we don't do extra locks or unlocks.
1018 *
1019 * *page_started is set to one if we unlock locked_page and do everything
1020 * required to start IO on it. It may be clean and already done with
1021 * IO when we return.
1022 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001023static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -04001024 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001025 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001026 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -05001027{
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001028 struct btrfs_root *root = inode->root;
1029 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001030 u64 alloc_hint = 0;
1031 u64 num_bytes;
1032 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001033 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +01001034 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001035 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -05001036 struct btrfs_key ins;
1037 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +00001038 unsigned clear_bits;
1039 unsigned long page_ops;
1040 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -05001041 int ret = 0;
1042
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001043 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001044 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001045 ret = -EINVAL;
1046 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001047 }
Chris Mason771ed682008-11-06 22:02:51 -05001048
Qu Wenruofda28322013-02-26 08:10:22 +00001049 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001050 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001051 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001052
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001053 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001054
Chris Mason771ed682008-11-06 22:02:51 -05001055 if (start == 0) {
1056 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001057 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001058 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001059 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001060 /*
1061 * We use DO_ACCOUNTING here because we need the
1062 * delalloc_release_metadata to be run _after_ we drop
1063 * our outstanding extent for clearing delalloc for this
1064 * range.
1065 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001066 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001067 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001068 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1069 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001070 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1071 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001072 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001073 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001074 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001075 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001076 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001077 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001078 }
1079 }
Chris Masonc8b97812008-10-29 14:49:59 -04001080
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001081 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1082 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001083
Filipe Manana432cd2a2020-06-08 13:32:55 +01001084 /*
1085 * Relocation relies on the relocated extents to have exactly the same
1086 * size as the original extents. Normally writeback for relocation data
1087 * extents follows a NOCOW path because relocation preallocates the
1088 * extents. However, due to an operation such as scrub turning a block
1089 * group to RO mode, it may fallback to COW mode, so we must make sure
1090 * an extent allocated during COW has exactly the requested size and can
1091 * not be split into smaller extents, otherwise relocation breaks and
1092 * fails during the stage where it updates the bytenr of file extent
1093 * items.
1094 */
1095 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1096 min_alloc_size = num_bytes;
1097 else
1098 min_alloc_size = fs_info->sectorsize;
1099
Anand Jain3752d222018-02-15 12:29:38 +08001100 while (num_bytes > 0) {
1101 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001102 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001103 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001104 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001105 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001106 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001107 cur_alloc_size = ins.offset;
1108 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001109
Chris Mason771ed682008-11-06 22:02:51 -05001110 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001111 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001112 start, /* orig_start */
1113 ins.objectid, /* block_start */
1114 ins.offset, /* block_len */
1115 ins.offset, /* orig_block_len */
1116 ram_size, /* ram_bytes */
1117 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001118 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001119 if (IS_ERR(em)) {
1120 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001121 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001122 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001123 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001124
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001125 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1126 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001127 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001128 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001129
Yan Zheng17d217f2008-12-12 10:03:38 -05001130 if (root->root_key.objectid ==
1131 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001132 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001133 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001134 /*
1135 * Only drop cache here, and process as normal.
1136 *
1137 * We must not allow extent_clear_unlock_delalloc()
1138 * at out_unlock label to free meta of this ordered
1139 * extent, as its meta should be freed by
1140 * btrfs_finish_ordered_io().
1141 *
1142 * So we must continue until @start is increased to
1143 * skip current ordered extent.
1144 */
Josef Bacik00361582013-08-14 14:02:47 -04001145 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001146 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001147 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001148 }
1149
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001150 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001151
Chris Masonc8b97812008-10-29 14:49:59 -04001152 /* we're not doing compressed IO, don't unlock the first
1153 * page (which the caller expects to stay locked), don't
1154 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001155 *
1156 * Do set the Private2 bit so we know this page was properly
1157 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001158 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001159 page_ops = unlock ? PAGE_UNLOCK : 0;
1160 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001161
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001162 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001163 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001164 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001165 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001166 if (num_bytes < cur_alloc_size)
1167 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001168 else
Anand Jain3752d222018-02-15 12:29:38 +08001169 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001170 alloc_hint = ins.objectid + ins.offset;
1171 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001172 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001173
1174 /*
1175 * btrfs_reloc_clone_csums() error, since start is increased
1176 * extent_clear_unlock_delalloc() at out_unlock label won't
1177 * free metadata of current ordered extent, we're OK to exit.
1178 */
1179 if (ret)
1180 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001181 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001182out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001183 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001184
Filipe Mananad9f85962014-08-25 10:43:00 +01001185out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001186 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001187out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001188 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001189 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001190out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001191 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1192 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001193 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1194 PAGE_END_WRITEBACK;
1195 /*
1196 * If we reserved an extent for our delalloc range (or a subrange) and
1197 * failed to create the respective ordered extent, then it means that
1198 * when we reserved the extent we decremented the extent's size from
1199 * the data space_info's bytes_may_use counter and incremented the
1200 * space_info's bytes_reserved counter by the same amount. We must make
1201 * sure extent_clear_unlock_delalloc() does not try to decrement again
1202 * the data space_info's bytes_may_use counter, therefore we do not pass
1203 * it the flag EXTENT_CLEAR_DATA_RESV.
1204 */
1205 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001206 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001207 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001208 locked_page,
1209 clear_bits,
1210 page_ops);
1211 start += cur_alloc_size;
1212 if (start >= end)
1213 goto out;
1214 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001215 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001216 clear_bits | EXTENT_CLEAR_DATA_RESV,
1217 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001218 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001219}
Chris Masonc8b97812008-10-29 14:49:59 -04001220
Chris Mason771ed682008-11-06 22:02:51 -05001221/*
1222 * work queue call back to started compression on a file and pages
1223 */
1224static noinline void async_cow_start(struct btrfs_work *work)
1225{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001226 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001227 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001228
Nikolay Borisovb5326272019-03-12 17:20:25 +02001229 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001230
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001231 compressed_extents = compress_file_range(async_chunk);
1232 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001233 btrfs_add_delayed_iput(async_chunk->inode);
1234 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001235 }
Chris Mason771ed682008-11-06 22:02:51 -05001236}
1237
1238/*
1239 * work queue call back to submit previously compressed pages
1240 */
1241static noinline void async_cow_submit(struct btrfs_work *work)
1242{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001243 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1244 work);
1245 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001246 unsigned long nr_pages;
1247
Nikolay Borisovb5326272019-03-12 17:20:25 +02001248 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001249 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001250
David Sterba093258e2018-02-26 16:15:17 +01001251 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001252 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001253 5 * SZ_1M)
1254 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001255
Nikolay Borisov4546d172019-01-03 10:50:03 +02001256 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001257 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001258 * in which case we don't have anything to submit, yet we need to
1259 * always adjust ->async_delalloc_pages as its paired with the init
1260 * happening in cow_file_range_async
1261 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001262 if (async_chunk->inode)
1263 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001264}
Chris Masonc8b97812008-10-29 14:49:59 -04001265
Chris Mason771ed682008-11-06 22:02:51 -05001266static noinline void async_cow_free(struct btrfs_work *work)
1267{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001268 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001269
Nikolay Borisovb5326272019-03-12 17:20:25 +02001270 async_chunk = container_of(work, struct async_chunk, work);
1271 if (async_chunk->inode)
1272 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001273 if (async_chunk->blkcg_css)
1274 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001275 /*
1276 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001277 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001278 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001279 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001280 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001281}
1282
Nikolay Borisov751b6432020-06-03 08:55:22 +03001283static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001284 struct writeback_control *wbc,
1285 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001286 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001287 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001288{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001289 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001290 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001291 struct async_cow *ctx;
1292 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001293 unsigned long nr_pages;
1294 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001295 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1296 int i;
1297 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001298 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001299 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001300
Nikolay Borisov751b6432020-06-03 08:55:22 +03001301 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001302
Nikolay Borisov751b6432020-06-03 08:55:22 +03001303 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001304 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1305 num_chunks = 1;
1306 should_compress = false;
1307 } else {
1308 should_compress = true;
1309 }
1310
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001311 nofs_flag = memalloc_nofs_save();
1312 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1313 memalloc_nofs_restore(nofs_flag);
1314
Nikolay Borisov97db1202019-03-12 17:20:24 +02001315 if (!ctx) {
1316 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1317 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1318 EXTENT_DO_ACCOUNTING;
1319 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1320 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1321 PAGE_SET_ERROR;
1322
Nikolay Borisov751b6432020-06-03 08:55:22 +03001323 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1324 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001325 return -ENOMEM;
1326 }
1327
1328 async_chunk = ctx->chunks;
1329 atomic_set(&ctx->num_chunks, num_chunks);
1330
1331 for (i = 0; i < num_chunks; i++) {
1332 if (should_compress)
1333 cur_end = min(end, start + SZ_512K - 1);
1334 else
1335 cur_end = end;
1336
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001337 /*
1338 * igrab is called higher up in the call chain, take only the
1339 * lightweight reference for the callback lifetime
1340 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001341 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001342 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001343 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001344 async_chunk[i].start = start;
1345 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001346 async_chunk[i].write_flags = write_flags;
1347 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001348
Chris Mason1d53c9e2019-07-10 12:28:16 -07001349 /*
1350 * The locked_page comes all the way from writepage and its
1351 * the original page we were actually given. As we spread
1352 * this large delalloc region across multiple async_chunk
1353 * structs, only the first struct needs a pointer to locked_page
1354 *
1355 * This way we don't need racey decisions about who is supposed
1356 * to unlock it.
1357 */
1358 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001359 /*
1360 * Depending on the compressibility, the pages might or
1361 * might not go through async. We want all of them to
1362 * be accounted against wbc once. Let's do it here
1363 * before the paths diverge. wbc accounting is used
1364 * only for foreign writeback detection and doesn't
1365 * need full accuracy. Just account the whole thing
1366 * against the first page.
1367 */
1368 wbc_account_cgroup_owner(wbc, locked_page,
1369 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001370 async_chunk[i].locked_page = locked_page;
1371 locked_page = NULL;
1372 } else {
1373 async_chunk[i].locked_page = NULL;
1374 }
1375
Chris Masonec39f762019-07-10 12:28:17 -07001376 if (blkcg_css != blkcg_root_css) {
1377 css_get(blkcg_css);
1378 async_chunk[i].blkcg_css = blkcg_css;
1379 } else {
1380 async_chunk[i].blkcg_css = NULL;
1381 }
1382
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001383 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1384 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001385
Nikolay Borisov97db1202019-03-12 17:20:24 +02001386 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001387 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001388
Nikolay Borisov97db1202019-03-12 17:20:24 +02001389 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001390
Chris Mason771ed682008-11-06 22:02:51 -05001391 *nr_written += nr_pages;
1392 start = cur_end + 1;
1393 }
1394 *page_started = 1;
1395 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001396}
1397
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001398static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001399 u64 bytenr, u64 num_bytes)
1400{
1401 int ret;
1402 struct btrfs_ordered_sum *sums;
1403 LIST_HEAD(list);
1404
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001405 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001406 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001407 if (ret == 0 && list_empty(&list))
1408 return 0;
1409
1410 while (!list_empty(&list)) {
1411 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1412 list_del(&sums->list);
1413 kfree(sums);
1414 }
Liu Bo58113752018-01-31 17:09:13 -07001415 if (ret < 0)
1416 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001417 return 1;
1418}
1419
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001420static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001421 const u64 start, const u64 end,
1422 int *page_started, unsigned long *nr_written)
1423{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001424 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1425 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001426 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001427 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001428 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001429 u64 range_start = start;
1430 u64 count;
1431
1432 /*
1433 * If EXTENT_NORESERVE is set it means that when the buffered write was
1434 * made we had not enough available data space and therefore we did not
1435 * reserve data space for it, since we though we could do NOCOW for the
1436 * respective file range (either there is prealloc extent or the inode
1437 * has the NOCOW bit set).
1438 *
1439 * However when we need to fallback to COW mode (because for example the
1440 * block group for the corresponding extent was turned to RO mode by a
1441 * scrub or relocation) we need to do the following:
1442 *
1443 * 1) We increment the bytes_may_use counter of the data space info.
1444 * If COW succeeds, it allocates a new data extent and after doing
1445 * that it decrements the space info's bytes_may_use counter and
1446 * increments its bytes_reserved counter by the same amount (we do
1447 * this at btrfs_add_reserved_bytes()). So we need to increment the
1448 * bytes_may_use counter to compensate (when space is reserved at
1449 * buffered write time, the bytes_may_use counter is incremented);
1450 *
1451 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1452 * that if the COW path fails for any reason, it decrements (through
1453 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1454 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001455 *
1456 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001457 * space cache inode or an inode of the data relocation tree, we must
1458 * also increment bytes_may_use of the data space_info for the same
1459 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001460 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001461 * group that contains that extent to RO mode and therefore force COW
1462 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001463 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001464 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001465 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001466 if (count > 0 || is_space_ino || is_reloc_ino) {
1467 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001468 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001469 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1470
Filipe Manana6bd335b2020-06-08 13:33:05 +01001471 if (is_space_ino || is_reloc_ino)
1472 bytes = range_bytes;
1473
Filipe Manana467dc472020-05-27 11:16:07 +01001474 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001475 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001476 spin_unlock(&sinfo->lock);
1477
Filipe Manana2166e5e2020-05-27 11:16:19 +01001478 if (count > 0)
1479 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1480 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001481 }
1482
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001483 return cow_file_range(inode, locked_page, start, end, page_started,
1484 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001485}
1486
Chris Masond352ac62008-09-29 15:18:18 -04001487/*
1488 * when nowcow writeback call back. This checks for snapshots or COW copies
1489 * of the extents that exist in the file, and COWs the file as required.
1490 *
1491 * If no cow copies or snapshots exist, we write directly to the existing
1492 * blocks on disk
1493 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001494static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001495 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001496 const u64 start, const u64 end,
1497 int *page_started, int force,
1498 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001499{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001500 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1501 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001502 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001503 u64 cow_start = (u64)-1;
1504 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001505 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001506 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001507 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1508 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001509 bool nocow = false;
1510 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001511
1512 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001513 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001514 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001515 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001516 EXTENT_DO_ACCOUNTING |
1517 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001518 PAGE_CLEAR_DIRTY |
1519 PAGE_SET_WRITEBACK |
1520 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001521 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001522 }
Li Zefan82d59022011-04-20 10:33:24 +08001523
Yan Zheng80ff3852008-10-30 14:20:02 -04001524 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001525 struct btrfs_key found_key;
1526 struct btrfs_file_extent_item *fi;
1527 struct extent_buffer *leaf;
1528 u64 extent_end;
1529 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001530 u64 num_bytes = 0;
1531 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001532 u64 ram_bytes;
1533 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001534
1535 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001536
Liu Boe4c3b2d2017-01-30 12:25:28 -08001537 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001538 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001539 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001540 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001541
1542 /*
1543 * If there is no extent for our range when doing the initial
1544 * search, then go back to the previous slot as it will be the
1545 * one containing the search offset
1546 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001547 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1548 leaf = path->nodes[0];
1549 btrfs_item_key_to_cpu(leaf, &found_key,
1550 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001551 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001552 found_key.type == BTRFS_EXTENT_DATA_KEY)
1553 path->slots[0]--;
1554 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001555 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001556next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001557 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001558 leaf = path->nodes[0];
1559 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1560 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001561 if (ret < 0) {
1562 if (cow_start != (u64)-1)
1563 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001564 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001565 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001566 if (ret > 0)
1567 break;
1568 leaf = path->nodes[0];
1569 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001570
Yan Zheng80ff3852008-10-30 14:20:02 -04001571 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001572
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001573 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001574 if (found_key.objectid > ino)
1575 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001576 /*
1577 * Keep searching until we find an EXTENT_ITEM or there are no
1578 * more extents for this inode
1579 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001580 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1581 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1582 path->slots[0]++;
1583 goto next_slot;
1584 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001585
1586 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001587 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001588 found_key.offset > end)
1589 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001590
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001591 /*
1592 * If the found extent starts after requested offset, then
1593 * adjust extent_end to be right before this extent begins
1594 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001595 if (found_key.offset > cur_offset) {
1596 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001597 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001598 goto out_check;
1599 }
Chris Masonc31f8832008-01-08 15:46:31 -05001600
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001601 /*
1602 * Found extent which begins before our range and potentially
1603 * intersect it
1604 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001605 fi = btrfs_item_ptr(leaf, path->slots[0],
1606 struct btrfs_file_extent_item);
1607 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001608
Josef Bacikcc95bef2013-04-04 14:31:27 -04001609 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001610 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1611 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001612 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001613 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001614 extent_end = found_key.offset +
1615 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001616 disk_num_bytes =
1617 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001618 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001619 * If the extent we got ends before our current offset,
1620 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001621 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001622 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001623 path->slots[0]++;
1624 goto next_slot;
1625 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001626 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001627 if (disk_bytenr == 0)
1628 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001629 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001630 if (btrfs_file_extent_compression(leaf, fi) ||
1631 btrfs_file_extent_encryption(leaf, fi) ||
1632 btrfs_file_extent_other_encoding(leaf, fi))
1633 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001634 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001635 * If extent is created before the last volume's snapshot
1636 * this implies the extent is shared, hence we can't do
1637 * nocow. This is the same check as in
1638 * btrfs_cross_ref_exist but without calling
1639 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001640 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001641 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001642 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001643 btrfs_root_last_snapshot(&root->root_item))
1644 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001645 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1646 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001647 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001648 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001649 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001650 ret = btrfs_cross_ref_exist(root, ino,
1651 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001652 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001653 if (ret) {
1654 /*
1655 * ret could be -EIO if the above fails to read
1656 * metadata.
1657 */
1658 if (ret < 0) {
1659 if (cow_start != (u64)-1)
1660 cur_offset = cow_start;
1661 goto error;
1662 }
1663
Nikolay Borisov3e024842019-08-21 10:42:03 +03001664 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001665 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001666 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001667 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001668 disk_bytenr += cur_offset - found_key.offset;
1669 num_bytes = min(end + 1, extent_end) - cur_offset;
1670 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001671 * If there are pending snapshots for this root, we
1672 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001673 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001674 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001675 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001676 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001677 * force cow if csum exists in the range.
1678 * this ensure that csum for a given extent are
1679 * either valid or do not exist.
1680 */
Liu Bo58113752018-01-31 17:09:13 -07001681 ret = csum_exist_in_range(fs_info, disk_bytenr,
1682 num_bytes);
1683 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001684 /*
1685 * ret could be -EIO if the above fails to read
1686 * metadata.
1687 */
1688 if (ret < 0) {
1689 if (cow_start != (u64)-1)
1690 cur_offset = cow_start;
1691 goto error;
1692 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001693 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001694 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001695 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001696 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001697 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001698 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001699 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001700 extent_end = found_key.offset + ram_bytes;
1701 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001702 /* Skip extents outside of our requested range */
1703 if (extent_end <= start) {
1704 path->slots[0]++;
1705 goto next_slot;
1706 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001707 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001708 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001709 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001710 }
1711out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001712 /*
1713 * If nocow is false then record the beginning of the range
1714 * that needs to be COWed
1715 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001716 if (!nocow) {
1717 if (cow_start == (u64)-1)
1718 cow_start = cur_offset;
1719 cur_offset = extent_end;
1720 if (cur_offset > end)
1721 break;
1722 path->slots[0]++;
1723 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001724 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001725
David Sterbab3b4aa72011-04-21 01:20:15 +02001726 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001727
1728 /*
1729 * COW range from cow_start to found_key.offset - 1. As the key
1730 * will contain the beginning of the first extent that can be
1731 * NOCOW, following one which needs to be COW'ed
1732 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001733 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001734 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001735 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001736 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001737 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001738 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001739 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001740 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001741
Yan Zhengd899e052008-10-30 14:25:28 -04001742 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001743 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001744 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001745
Nikolay Borisov968322c2020-06-03 08:55:21 +03001746 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001747 orig_start,
1748 disk_bytenr, /* block_start */
1749 num_bytes, /* block_len */
1750 disk_num_bytes, /* orig_block_len */
1751 ram_bytes, BTRFS_COMPRESS_NONE,
1752 BTRFS_ORDERED_PREALLOC);
1753 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001754 ret = PTR_ERR(em);
1755 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001756 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001757 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001758 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001759 disk_bytenr, num_bytes,
1760 num_bytes,
1761 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001762 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001763 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001764 cur_offset + num_bytes - 1,
1765 0);
1766 goto error;
1767 }
Yan Zhengd899e052008-10-30 14:25:28 -04001768 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001769 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001770 disk_bytenr, num_bytes,
1771 num_bytes,
1772 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001773 if (ret)
1774 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001775 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001776
Filipe Mananaf78c4362016-05-09 13:15:41 +01001777 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001778 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001779 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001780
Yan, Zhengefa56462010-05-16 10:49:59 -04001781 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001782 BTRFS_DATA_RELOC_TREE_OBJECTID)
1783 /*
1784 * Error handled later, as we must prevent
1785 * extent_clear_unlock_delalloc() in error handler
1786 * from freeing metadata of created ordered extent.
1787 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001788 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001789 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001790
Nikolay Borisov968322c2020-06-03 08:55:21 +03001791 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001792 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001793 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001794 EXTENT_DELALLOC |
1795 EXTENT_CLEAR_DATA_RESV,
1796 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1797
Yan Zheng80ff3852008-10-30 14:20:02 -04001798 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001799
1800 /*
1801 * btrfs_reloc_clone_csums() error, now we're OK to call error
1802 * handler, as metadata for created ordered extent will only
1803 * be freed by btrfs_finish_ordered_io().
1804 */
1805 if (ret)
1806 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001807 if (cur_offset > end)
1808 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001809 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001810 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001811
Robbie Ko506481b2018-10-30 18:04:04 +08001812 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001813 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001814
Yan Zheng80ff3852008-10-30 14:20:02 -04001815 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001816 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001817 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1818 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001819 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001820 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001821 }
1822
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001823error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001824 if (nocow)
1825 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1826
Josef Bacik17ca04a2012-05-31 15:58:55 -04001827 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001828 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001829 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001830 EXTENT_DELALLOC | EXTENT_DEFRAG |
1831 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1832 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001833 PAGE_SET_WRITEBACK |
1834 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001835 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001836 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001837}
1838
Nikolay Borisov0c494222020-06-03 08:55:28 +03001839static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001840{
1841
Nikolay Borisov0c494222020-06-03 08:55:28 +03001842 if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1843 !(inode->flags & BTRFS_INODE_PREALLOC))
Wang Shilong47059d92014-07-03 18:22:07 +08001844 return 0;
1845
1846 /*
1847 * @defrag_bytes is a hint value, no spinlock held here,
1848 * if is not zero, it means the file is defragging.
1849 * Force cow if given extent needs to be defragged.
1850 */
Nikolay Borisov0c494222020-06-03 08:55:28 +03001851 if (inode->defrag_bytes &&
1852 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL))
Wang Shilong47059d92014-07-03 18:22:07 +08001853 return 1;
1854
1855 return 0;
1856}
1857
Chris Masond352ac62008-09-29 15:18:18 -04001858/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001859 * Function to process delayed allocation (create CoW) for ranges which are
1860 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001861 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001862int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001863 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1864 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001865{
Chris Masonbe20aa92007-12-17 20:14:01 -05001866 int ret;
Nikolay Borisov98456b92020-06-03 08:55:29 +03001867 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001868
Nikolay Borisov98456b92020-06-03 08:55:29 +03001869 if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1870 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001871 page_started, 1, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001872 } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1873 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001874 page_started, 0, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001875 } else if (!inode_can_compress(inode) ||
1876 !inode_need_compress(inode, start, end)) {
1877 ret = cow_file_range(inode, locked_page, start, end,
1878 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001879 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001880 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1881 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001882 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001883 }
Qu Wenruo524272602017-03-08 10:25:52 +08001884 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001885 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001886 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001887 return ret;
1888}
1889
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001890void btrfs_split_delalloc_extent(struct inode *inode,
1891 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001892{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001893 u64 size;
1894
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001895 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001896 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001897 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001898
Josef Bacikdcab6a32015-02-11 15:08:59 -05001899 size = orig->end - orig->start + 1;
1900 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001901 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001902 u64 new_size;
1903
1904 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001905 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001906 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001907 */
1908 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001909 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001910 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001911 num_extents += count_max_extents(new_size);
1912 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001913 return;
1914 }
1915
Josef Bacik9e0baf62011-07-15 15:16:44 +00001916 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001917 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001918 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001919}
1920
1921/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001922 * Handle merged delayed allocation extents so we can keep track of new extents
1923 * that are just merged onto old extents, such as when we are doing sequential
1924 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001925 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001926void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1927 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001928{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001929 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001930 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001931
Josef Bacik9ed74f22009-09-11 16:12:44 -04001932 /* not delalloc, ignore it */
1933 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001934 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001935
Josef Bacik8461a3d2015-03-13 15:12:08 -04001936 if (new->start > other->start)
1937 new_size = new->end - other->start + 1;
1938 else
1939 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001940
1941 /* we're not bigger than the max, unreserve the space and go */
1942 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1943 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001944 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001945 spin_unlock(&BTRFS_I(inode)->lock);
1946 return;
1947 }
1948
1949 /*
Josef Bacikba117212015-03-13 15:01:24 -04001950 * We have to add up either side to figure out how many extents were
1951 * accounted for before we merged into one big extent. If the number of
1952 * extents we accounted for is <= the amount we need for the new range
1953 * then we can return, otherwise drop. Think of it like this
1954 *
1955 * [ 4k][MAX_SIZE]
1956 *
1957 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1958 * need 2 outstanding extents, on one side we have 1 and the other side
1959 * we have 1 so they are == and we can return. But in this case
1960 *
1961 * [MAX_SIZE+4k][MAX_SIZE+4k]
1962 *
1963 * Each range on their own accounts for 2 extents, but merged together
1964 * they are only 3 extents worth of accounting, so we need to drop in
1965 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001966 */
Josef Bacikba117212015-03-13 15:01:24 -04001967 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001968 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001969 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001970 num_extents += count_max_extents(old_size);
1971 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001972 return;
1973
Josef Bacik9e0baf62011-07-15 15:16:44 +00001974 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001975 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001976 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001977}
1978
Miao Xieeb73c1b2013-05-15 07:48:22 +00001979static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1980 struct inode *inode)
1981{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001982 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1983
Miao Xieeb73c1b2013-05-15 07:48:22 +00001984 spin_lock(&root->delalloc_lock);
1985 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1986 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1987 &root->delalloc_inodes);
1988 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1989 &BTRFS_I(inode)->runtime_flags);
1990 root->nr_delalloc_inodes++;
1991 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001992 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001993 BUG_ON(!list_empty(&root->delalloc_root));
1994 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001995 &fs_info->delalloc_roots);
1996 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001997 }
1998 }
1999 spin_unlock(&root->delalloc_lock);
2000}
2001
Nikolay Borisov2b877332018-04-27 12:21:51 +03002002
2003void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2004 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00002005{
David Sterba3ffbd682018-06-29 10:56:42 +02002006 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002007
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002008 if (!list_empty(&inode->delalloc_inodes)) {
2009 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002010 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002011 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002012 root->nr_delalloc_inodes--;
2013 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03002014 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002015 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002016 BUG_ON(list_empty(&root->delalloc_root));
2017 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002018 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002019 }
2020 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03002021}
2022
2023static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2024 struct btrfs_inode *inode)
2025{
2026 spin_lock(&root->delalloc_lock);
2027 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002028 spin_unlock(&root->delalloc_lock);
2029}
2030
Chris Masond352ac62008-09-29 15:18:18 -04002031/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002032 * Properly track delayed allocation bytes in the inode and to maintain the
2033 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04002034 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002035void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2036 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002037{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002038 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2039
Wang Shilong47059d92014-07-03 18:22:07 +08002040 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2041 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002042 /*
2043 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002044 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002045 * bit, which is only set or cleared with irqs on
2046 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002047 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002048 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002049 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002050 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002051 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002052
Josef Bacik8b62f872017-10-19 14:15:55 -04002053 spin_lock(&BTRFS_I(inode)->lock);
2054 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2055 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002056
Josef Bacik6a3891c2015-03-16 17:38:52 -04002057 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002058 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002059 return;
2060
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002061 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2062 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002063 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002064 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002065 if (*bits & EXTENT_DEFRAG)
2066 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002067 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002068 &BTRFS_I(inode)->runtime_flags))
2069 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002070 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002071 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002072
2073 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2074 (*bits & EXTENT_DELALLOC_NEW)) {
2075 spin_lock(&BTRFS_I(inode)->lock);
2076 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2077 state->start;
2078 spin_unlock(&BTRFS_I(inode)->lock);
2079 }
Chris Mason291d6732008-01-29 15:55:23 -05002080}
2081
Chris Masond352ac62008-09-29 15:18:18 -04002082/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002083 * Once a range is no longer delalloc this function ensures that proper
2084 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002085 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002086void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2087 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002088{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002089 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2090 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002091 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002092 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002093
Filipe Manana4a4b9642017-07-27 19:52:55 +01002094 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2095 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002096 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002097 spin_unlock(&inode->lock);
2098 }
Wang Shilong47059d92014-07-03 18:22:07 +08002099
Chris Mason75eff682008-12-15 15:54:40 -05002100 /*
2101 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002102 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002103 * bit, which is only set or cleared with irqs on
2104 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002105 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002106 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002107 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002108
Josef Bacik8b62f872017-10-19 14:15:55 -04002109 spin_lock(&inode->lock);
2110 btrfs_mod_outstanding_extents(inode, -num_extents);
2111 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002112
Josef Bacikb6d08f02013-09-27 14:57:43 -04002113 /*
2114 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002115 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002116 * error.
2117 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002118 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002119 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002120 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002121
Josef Bacik6a3891c2015-03-16 17:38:52 -04002122 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002123 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002124 return;
2125
Filipe Mananaa315e682017-03-06 23:04:20 +00002126 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2127 do_list && !(state->state & EXTENT_NORESERVE) &&
2128 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002129 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002130
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002131 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2132 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002133 spin_lock(&inode->lock);
2134 inode->delalloc_bytes -= len;
2135 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002136 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002137 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002138 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002139 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002140 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002141
2142 if ((state->state & EXTENT_DELALLOC_NEW) &&
2143 (*bits & EXTENT_DELALLOC_NEW)) {
2144 spin_lock(&inode->lock);
2145 ASSERT(inode->new_delalloc_bytes >= len);
2146 inode->new_delalloc_bytes -= len;
2147 spin_unlock(&inode->lock);
2148 }
Chris Mason291d6732008-01-29 15:55:23 -05002149}
2150
Chris Masond352ac62008-09-29 15:18:18 -04002151/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002152 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2153 * in a chunk's stripe. This function ensures that bios do not span a
2154 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002155 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002156 * @page - The page we are about to add to the bio
2157 * @size - size we want to add to the bio
2158 * @bio - bio we want to ensure is smaller than a stripe
2159 * @bio_flags - flags of the bio
2160 *
2161 * return 1 if page cannot be added to the bio
2162 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002163 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002164 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002165int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2166 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002167{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002168 struct inode *inode = page->mapping->host;
2169 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002170 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002171 u64 length = 0;
2172 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002173 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002174 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002175
Chris Mason771ed682008-11-06 22:02:51 -05002176 if (bio_flags & EXTENT_BIO_COMPRESSED)
2177 return 0;
2178
Kent Overstreet4f024f32013-10-11 15:44:27 -07002179 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002180 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002181 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2182 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002183 if (ret < 0)
2184 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002185
2186 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002187 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002188 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002189}
2190
Chris Masond352ac62008-09-29 15:18:18 -04002191/*
2192 * in order to insert checksums into the metadata in large chunks,
2193 * we wait until bio submission time. All the pages in the bio are
2194 * checksummed and sums are attached onto the ordered extent record.
2195 *
2196 * At IO completion time the cums attached on the ordered extent record
2197 * are inserted into the btree
2198 */
Qu Wenruo8896a082020-10-21 14:24:53 +08002199static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
2200 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002201{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002202 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002203}
Chris Masone0156402008-04-16 11:15:20 -04002204
Chris Mason4a69a412008-11-06 22:03:00 -05002205/*
Chris Masoncad321a2008-12-17 14:51:42 -05002206 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002207 * on write, or reading the csums from the tree before a read.
2208 *
2209 * Rules about async/sync submit,
2210 * a) read: sync submit
2211 *
2212 * b) write without checksum: sync submit
2213 *
2214 * c) write with checksum:
2215 * c-1) if bio is issued by fsync: sync submit
2216 * (sync_writers != 0)
2217 *
2218 * c-2) if root is reloc root: sync submit
2219 * (only in case of buffered IO)
2220 *
2221 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002222 */
Nikolay Borisov908930f2020-09-18 16:34:37 +03002223blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2224 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002225
Chris Mason44b8bd72008-04-16 11:14:51 -04002226{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002227 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002228 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302229 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002230 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002231 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002232 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002233
Josef Bacik42437a62020-10-16 11:29:18 -04002234 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
2235 !fs_info->csum_root;
Chris Masoncad321a2008-12-17 14:51:42 -05002236
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002237 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302238 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002239
Mike Christie37226b22016-06-05 14:31:52 -05002240 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002241 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002242 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002243 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002244
Chris Masond20f7042008-12-08 16:58:54 -05002245 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002246 ret = btrfs_submit_compressed_read(inode, bio,
2247 mirror_num,
2248 bio_flags);
2249 goto out;
Josef Bacik334c16d2020-10-16 11:29:14 -04002250 } else {
2251 /*
2252 * Lookup bio sums does extra checks around whether we
2253 * need to csum or not, which is why we ignore skip_sum
2254 * here.
2255 */
Omar Sandovaldb72e472019-12-10 10:37:35 -08002256 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002257 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002258 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002259 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002260 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002261 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002262 /* csum items have already been cloned */
2263 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2264 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002265 /* we're doing a write, do the async checksumming */
Qu Wenruo8896a082020-10-21 14:24:53 +08002266 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2267 0, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002268 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002269 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002270 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002271 if (ret)
2272 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002273 }
2274
Chris Mason0b86a832008-03-24 15:01:56 -04002275mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002276 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002277
2278out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002279 if (ret) {
2280 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002281 bio_endio(bio);
2282 }
Stefan Behrens61891922012-11-05 18:51:52 +01002283 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002284}
Chris Mason6885f302008-02-20 16:11:05 -05002285
Chris Masond352ac62008-09-29 15:18:18 -04002286/*
2287 * given a list of ordered sums record them in the inode. This happens
2288 * at IO completion time based on sums calculated at bio submission time.
2289 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002290static int add_pending_csums(struct btrfs_trans_handle *trans,
2291 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002292{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002293 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002294 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002295
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002296 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002297 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002298 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002299 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002300 if (ret)
2301 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002302 }
2303 return 0;
2304}
2305
Filipe Mananac3347302020-11-04 11:07:31 +00002306static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2307 const u64 start,
2308 const u64 len,
2309 struct extent_state **cached_state)
2310{
2311 u64 search_start = start;
2312 const u64 end = start + len - 1;
2313
2314 while (search_start < end) {
2315 const u64 search_len = end - search_start + 1;
2316 struct extent_map *em;
2317 u64 em_len;
2318 int ret = 0;
2319
2320 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2321 if (IS_ERR(em))
2322 return PTR_ERR(em);
2323
2324 if (em->block_start != EXTENT_MAP_HOLE)
2325 goto next;
2326
2327 em_len = em->len;
2328 if (em->start < search_start)
2329 em_len -= search_start - em->start;
2330 if (em_len > search_len)
2331 em_len = search_len;
2332
2333 ret = set_extent_bit(&inode->io_tree, search_start,
2334 search_start + em_len - 1,
2335 EXTENT_DELALLOC_NEW,
2336 NULL, cached_state, GFP_NOFS);
2337next:
2338 search_start = extent_map_end(em);
2339 free_extent_map(em);
2340 if (ret)
2341 return ret;
2342 }
2343 return 0;
2344}
2345
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002346int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002347 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002348 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002349{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002350 WARN_ON(PAGE_ALIGNED(end));
Filipe Mananac3347302020-11-04 11:07:31 +00002351
2352 if (start >= i_size_read(&inode->vfs_inode) &&
2353 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2354 /*
2355 * There can't be any extents following eof in this case so just
2356 * set the delalloc new bit for the range directly.
2357 */
2358 extra_bits |= EXTENT_DELALLOC_NEW;
2359 } else {
2360 int ret;
2361
2362 ret = btrfs_find_new_delalloc_bytes(inode, start,
2363 end + 1 - start,
2364 cached_state);
2365 if (ret)
2366 return ret;
2367 }
2368
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002369 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2370 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002371}
2372
Chris Masond352ac62008-09-29 15:18:18 -04002373/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002374struct btrfs_writepage_fixup {
2375 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002376 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002377 struct btrfs_work work;
2378};
2379
Christoph Hellwigb2950862008-12-02 09:54:17 -05002380static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002381{
2382 struct btrfs_writepage_fixup *fixup;
2383 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002384 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002385 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002386 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002387 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002388 u64 page_start;
2389 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002390 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002391 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002392
2393 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2394 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002395 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002396 page_start = page_offset(page);
2397 page_end = page_offset(page) + PAGE_SIZE - 1;
2398
2399 /*
2400 * This is similar to page_mkwrite, we need to reserve the space before
2401 * we take the page lock.
2402 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002403 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2404 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002405again:
Chris Mason247e7432008-07-17 12:53:51 -04002406 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002407
Chris Mason25f3c502020-01-21 11:51:42 -05002408 /*
2409 * Before we queued this fixup, we took a reference on the page.
2410 * page->mapping may go NULL, but it shouldn't be moved to a different
2411 * address space.
2412 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002413 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2414 /*
2415 * Unfortunately this is a little tricky, either
2416 *
2417 * 1) We got here and our page had already been dealt with and
2418 * we reserved our space, thus ret == 0, so we need to just
2419 * drop our space reservation and bail. This can happen the
2420 * first time we come into the fixup worker, or could happen
2421 * while waiting for the ordered extent.
2422 * 2) Our page was already dealt with, but we happened to get an
2423 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2424 * this case we obviously don't have anything to release, but
2425 * because the page was already dealt with we don't want to
2426 * mark the page with an error, so make sure we're resetting
2427 * ret to 0. This is why we have this check _before_ the ret
2428 * check, because we do not want to have a surprise ENOSPC
2429 * when the page was already properly dealt with.
2430 */
2431 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002432 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2433 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002434 page_start, PAGE_SIZE,
2435 true);
2436 }
2437 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002438 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002439 }
Chris Mason25f3c502020-01-21 11:51:42 -05002440
2441 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002442 * We can't mess with the page state unless it is locked, so now that
2443 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002444 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002445 if (ret)
2446 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002447
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002448 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002449
2450 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002451 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002452 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002453
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002454 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002455 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002456 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2457 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002458 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002459 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002460 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002461 goto again;
2462 }
Chris Mason247e7432008-07-17 12:53:51 -04002463
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002464 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002465 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002466 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002467 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002468
Chris Mason25f3c502020-01-21 11:51:42 -05002469 /*
2470 * Everything went as planned, we're now the owner of a dirty page with
2471 * delayed allocation bits set and space reserved for our COW
2472 * destination.
2473 *
2474 * The page was dirty when we started, nothing should have cleaned it.
2475 */
2476 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002477 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002478out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002479 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002480 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002481 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2482 PAGE_SIZE, true);
2483 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002484 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002485out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002486 if (ret) {
2487 /*
2488 * We hit ENOSPC or other errors. Update the mapping and page
2489 * to reflect the errors and clean the page.
2490 */
2491 mapping_set_error(page->mapping, ret);
2492 end_extent_writepage(page, ret, page_start, page_end);
2493 clear_page_dirty_for_io(page);
2494 SetPageError(page);
2495 }
2496 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002497 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002498 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002499 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002500 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002501 /*
2502 * As a precaution, do a delayed iput in case it would be the last iput
2503 * that could need flushing space. Recursing back to fixup worker would
2504 * deadlock.
2505 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002506 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002507}
2508
2509/*
2510 * There are a few paths in the higher layers of the kernel that directly
2511 * set the page dirty bit without asking the filesystem if it is a
2512 * good idea. This causes problems because we want to make sure COW
2513 * properly happens and the data=ordered rules are followed.
2514 *
Chris Masonc8b97812008-10-29 14:49:59 -04002515 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002516 * hasn't been properly setup for IO. We kick off an async process
2517 * to fix it up. The async helper will wait for ordered extents, set
2518 * the delalloc bit and make it safe to write the page.
2519 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002520int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002521{
2522 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002523 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002524 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002525
Chris Mason8b62b722009-09-02 16:53:46 -04002526 /* this page is properly in the ordered list */
2527 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002528 return 0;
2529
Chris Mason25f3c502020-01-21 11:51:42 -05002530 /*
2531 * PageChecked is set below when we create a fixup worker for this page,
2532 * don't try to create another one if we're already PageChecked()
2533 *
2534 * The extent_io writepage code will redirty the page if we send back
2535 * EAGAIN.
2536 */
Chris Mason247e7432008-07-17 12:53:51 -04002537 if (PageChecked(page))
2538 return -EAGAIN;
2539
2540 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2541 if (!fixup)
2542 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002543
Josef Bacikf4b13632020-01-21 14:34:52 -05002544 /*
2545 * We are already holding a reference to this inode from
2546 * write_cache_pages. We need to hold it because the space reservation
2547 * takes place outside of the page lock, and we can't trust
2548 * page->mapping outside of the page lock.
2549 */
2550 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002551 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002552 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002553 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002554 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002555 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002556 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002557
2558 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002559}
2560
Yan Zhengd899e052008-10-30 14:25:28 -04002561static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002562 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002563 struct btrfs_file_extent_item *stack_fi,
2564 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002565{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002566 struct btrfs_root *root = inode->root;
Yan Zhengd899e052008-10-30 14:25:28 -04002567 struct btrfs_path *path;
2568 struct extent_buffer *leaf;
2569 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002570 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2571 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2572 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2573 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002574 struct btrfs_drop_extents_args drop_args = { 0 };
Yan Zhengd899e052008-10-30 14:25:28 -04002575 int ret;
2576
2577 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002578 if (!path)
2579 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002580
Chris Masona1ed8352009-09-11 12:27:37 -04002581 /*
2582 * we may be replacing one extent in the tree with another.
2583 * The new extent is pinned in the extent map, and we don't want
2584 * to drop it from the cache until it is completely in the btree.
2585 *
2586 * So, tell btrfs_drop_extents to leave this extent in the cache.
2587 * the caller is expected to unpin it and allow it to be merged
2588 * with the others.
2589 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002590 drop_args.path = path;
2591 drop_args.start = file_pos;
2592 drop_args.end = file_pos + num_bytes;
2593 drop_args.replace_extent = true;
2594 drop_args.extent_item_size = sizeof(*stack_fi);
2595 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002596 if (ret)
2597 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002598
Filipe Manana5893dfb2020-11-04 11:07:32 +00002599 if (!drop_args.extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002600 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002601 ins.offset = file_pos;
2602 ins.type = BTRFS_EXTENT_DATA_KEY;
2603
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002604 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002605 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002606 if (ret)
2607 goto out;
2608 }
Yan Zhengd899e052008-10-30 14:25:28 -04002609 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002610 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2611 write_extent_buffer(leaf, stack_fi,
2612 btrfs_item_ptr_offset(leaf, path->slots[0]),
2613 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002614
Yan Zhengd899e052008-10-30 14:25:28 -04002615 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002616 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002617
Nikolay Borisovc553f942020-06-03 08:55:19 +03002618 inode_add_bytes(&inode->vfs_inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002619
2620 ins.objectid = disk_bytenr;
2621 ins.offset = disk_num_bytes;
2622 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002623
Nikolay Borisovc553f942020-06-03 08:55:19 +03002624 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002625 if (ret)
2626 goto out;
2627
Nikolay Borisovc553f942020-06-03 08:55:19 +03002628 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002629 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002630out:
Yan Zhengd899e052008-10-30 14:25:28 -04002631 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002632
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002633 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002634}
2635
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002636static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002637 u64 start, u64 len)
2638{
David Sterba32da53862019-10-29 19:20:18 +01002639 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002640
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002641 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002642 ASSERT(cache);
2643
2644 spin_lock(&cache->lock);
2645 cache->delalloc_bytes -= len;
2646 spin_unlock(&cache->lock);
2647
2648 btrfs_put_block_group(cache);
2649}
2650
Qu Wenruo203f44c52020-06-10 09:04:40 +08002651static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002652 struct btrfs_ordered_extent *oe)
2653{
2654 struct btrfs_file_extent_item stack_fi;
2655 u64 logical_len;
2656
2657 memset(&stack_fi, 0, sizeof(stack_fi));
2658 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2659 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2660 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2661 oe->disk_num_bytes);
2662 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2663 logical_len = oe->truncated_len;
2664 else
2665 logical_len = oe->num_bytes;
2666 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2667 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2668 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2669 /* Encryption and other encoding is reserved and all 0 */
2670
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002671 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2672 oe->file_offset, &stack_fi,
2673 oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002674}
2675
2676/*
2677 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002678 * an ordered extent if the range of bytes in the file it covers are
2679 * fully written.
2680 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002681static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002682{
Josef Bacik5fd02042012-05-02 14:00:54 -04002683 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002684 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002685 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002686 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002687 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002688 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002689 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002690 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002691 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002692 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002693 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002694 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002695 bool range_locked = false;
2696 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002697 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002698 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002699
Omar Sandovalbffe6332019-12-02 17:34:19 -08002700 start = ordered_extent->file_offset;
2701 end = start + ordered_extent->num_bytes - 1;
2702
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002703 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2704 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2705 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2706 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002707
Nikolay Borisov8d510122019-10-08 20:43:06 +03002708 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002709
Josef Bacik5fd02042012-05-02 14:00:54 -04002710 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2711 ret = -EIO;
2712 goto out;
2713 }
2714
Omar Sandovalbffe6332019-12-02 17:34:19 -08002715 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002716
Josef Bacik77cef2e2013-08-29 13:57:21 -04002717 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2718 truncated = true;
2719 logical_len = ordered_extent->truncated_len;
2720 /* Truncated the entire extent, don't bother adding */
2721 if (!logical_len)
2722 goto out;
2723 }
2724
Yan, Zhengc2167752009-11-12 09:34:21 +00002725 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002726 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002727
Josef Bacikd923afe2020-01-17 09:02:23 -05002728 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002729 if (freespace_inode)
2730 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002731 else
2732 trans = btrfs_join_transaction(root);
2733 if (IS_ERR(trans)) {
2734 ret = PTR_ERR(trans);
2735 trans = NULL;
2736 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002737 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002738 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002739 ret = btrfs_update_inode_fallback(trans, root, inode);
2740 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002741 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002742 goto out;
2743 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002744
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002745 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002746 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002747
Nikolay Borisov8d510122019-10-08 20:43:06 +03002748 if (freespace_inode)
2749 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002750 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002751 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002752 if (IS_ERR(trans)) {
2753 ret = PTR_ERR(trans);
2754 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002755 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002756 }
Chris Masona79b7d42014-05-22 16:18:52 -07002757
Josef Bacik69fe2d72017-10-19 14:15:57 -04002758 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002759
Chris Masonc8b97812008-10-29 14:49:59 -04002760 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002761 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002762 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002763 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002764 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002765 ordered_extent->file_offset,
2766 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002767 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002768 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002769 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002770 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002771 if (!ret) {
2772 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002773 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002774 ordered_extent->disk_bytenr,
2775 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002776 }
Yan Zhengd899e052008-10-30 14:25:28 -04002777 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002778 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002779 ordered_extent->file_offset,
2780 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002781 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002782 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002783 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002784 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002785
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002786 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02002787 if (ret) {
2788 btrfs_abort_transaction(trans, ret);
2789 goto out;
2790 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002791
Josef Bacikd923afe2020-01-17 09:02:23 -05002792 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002793 ret = btrfs_update_inode_fallback(trans, root, inode);
2794 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002795 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002796 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002797 }
2798 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002799out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002800 clear_bits = EXTENT_DEFRAG;
2801 if (range_locked)
2802 clear_bits |= EXTENT_LOCKED;
2803 if (clear_new_delalloc_bytes)
2804 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002805 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2806 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002807 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002808
Miao Xiea698d0752012-09-20 01:51:59 -06002809 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002810 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002811
Josef Bacik77cef2e2013-08-29 13:57:21 -04002812 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002813 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002814
2815 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002816 unwritten_start += logical_len;
2817 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002818
2819 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002820 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002821
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002822 /*
2823 * If the ordered extent had an IOERR or something else went
2824 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002825 * back to the allocator. We only free the extent in the
2826 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002827 *
2828 * If we made it past insert_reserved_file_extent before we
2829 * errored out then we don't need to do this as the accounting
2830 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002831 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002832 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002833 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002834 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002835 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2836 /*
2837 * Discard the range before returning it back to the
2838 * free space pool
2839 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002840 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002841 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002842 ordered_extent->disk_bytenr,
2843 ordered_extent->disk_num_bytes,
2844 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002845 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002846 ordered_extent->disk_bytenr,
2847 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002848 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002849 }
2850
Josef Bacik5fd02042012-05-02 14:00:54 -04002851 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002852 * This needs to be done to make sure anybody waiting knows we are done
2853 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002854 */
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03002855 btrfs_remove_ordered_extent(BTRFS_I(inode), ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04002856
Chris Masone6dcd2d2008-07-17 12:53:50 -04002857 /* once for us */
2858 btrfs_put_ordered_extent(ordered_extent);
2859 /* once for the tree */
2860 btrfs_put_ordered_extent(ordered_extent);
2861
Josef Bacik5fd02042012-05-02 14:00:54 -04002862 return ret;
2863}
2864
2865static void finish_ordered_fn(struct btrfs_work *work)
2866{
2867 struct btrfs_ordered_extent *ordered_extent;
2868 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2869 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002870}
2871
Nikolay Borisovc6297322018-11-08 10:18:08 +02002872void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2873 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002874{
Nikolay Borisov3347c482020-08-31 14:42:44 +03002875 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
2876 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik5fd02042012-05-02 14:00:54 -04002877 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002878 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002879
liubo1abe9b82011-03-24 11:18:59 +00002880 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2881
Chris Mason8b62b722009-09-02 16:53:46 -04002882 ClearPagePrivate2(page);
Nikolay Borisov3347c482020-08-31 14:42:44 +03002883 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2884 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002885 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002886
Nikolay Borisov3347c482020-08-31 14:42:44 +03002887 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002888 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002889 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002890 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002891
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002892 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002893 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002894}
2895
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002896/*
2897 * check_data_csum - verify checksum of one sector of uncompressed data
2898 * @inode: the inode
2899 * @io_bio: btrfs_io_bio which contains the csum
2900 * @icsum: checksum index in the io_bio->csum array, size of csum_size
2901 * @page: page where is the data to be verified
2902 * @pgoff: offset inside the page
2903 *
2904 * The length of such check is always one sector size.
2905 */
Omar Sandoval47df7762020-04-16 14:46:17 -07002906static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002907 int icsum, struct page *page, int pgoff)
Miao Xiedc380ae2014-09-12 18:43:55 +08002908{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002909 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2910 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002911 char *kaddr;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002912 u32 len = fs_info->sectorsize;
David Sterba223486c2020-07-02 11:27:30 +02002913 const u32 csum_size = fs_info->csum_size;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002914 u8 *csum_expected;
2915 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002916
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002917 ASSERT(pgoff + len <= PAGE_SIZE);
2918
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002919 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002920
2921 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002922 shash->tfm = fs_info->csum_shash;
2923
Eric Biggersfd080012020-04-30 23:51:59 -07002924 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002925
2926 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002927 goto zeroit;
2928
2929 kunmap_atomic(kaddr);
2930 return 0;
2931zeroit:
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002932 btrfs_print_data_csum_error(BTRFS_I(inode), page_offset(page) + pgoff,
2933 csum, csum_expected, io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03002934 if (io_bio->device)
2935 btrfs_dev_stat_inc_and_print(io_bio->device,
2936 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08002937 memset(kaddr + pgoff, 1, len);
2938 flush_dcache_page(page);
2939 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002940 return -EIO;
2941}
2942
Chris Masond352ac62008-09-29 15:18:18 -04002943/*
Chris Masond352ac62008-09-29 15:18:18 -04002944 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002945 * if there's a match, we allow the bio to finish. If not, the code in
2946 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002947 */
Nikolay Borisov9a446d62020-09-18 16:34:33 +03002948int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u64 phy_offset,
2949 struct page *page, u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002950{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002951 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002952 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002953 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002954 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002955
Chris Masond20f7042008-12-08 16:58:54 -05002956 if (PageChecked(page)) {
2957 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002958 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002959 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002960
2961 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002962 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002963
Josef Bacik42437a62020-10-16 11:29:18 -04002964 if (!root->fs_info->csum_root)
2965 return 0;
2966
Yan Zheng17d217f2008-12-12 10:03:38 -05002967 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002968 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002969 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002970 return 0;
2971 }
2972
David Sterba265fdfa2020-07-01 21:19:09 +02002973 phy_offset >>= root->fs_info->sectorsize_bits;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002974 return check_data_csum(inode, io_bio, phy_offset, page, offset);
Chris Mason07157aa2007-08-30 08:50:51 -04002975}
Chris Masonb888db22007-08-27 16:49:44 -04002976
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002977/*
2978 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2979 *
2980 * @inode: The inode we want to perform iput on
2981 *
2982 * This function uses the generic vfs_inode::i_count to track whether we should
2983 * just decrement it (in case it's > 1) or if this is the last iput then link
2984 * the inode to the delayed iput machinery. Delayed iputs are processed at
2985 * transaction commit time/superblock commit/cleaner kthread.
2986 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002987void btrfs_add_delayed_iput(struct inode *inode)
2988{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002989 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002990 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002991
2992 if (atomic_add_unless(&inode->i_count, -1, 1))
2993 return;
2994
Josef Bacik034f7842018-12-03 11:06:52 -05002995 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002996 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002997 ASSERT(list_empty(&binode->delayed_iput));
2998 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002999 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003000 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3001 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003002}
3003
Josef Bacik63611e72019-06-18 10:59:18 -04003004static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3005 struct btrfs_inode *inode)
3006{
3007 list_del_init(&inode->delayed_iput);
3008 spin_unlock(&fs_info->delayed_iput_lock);
3009 iput(&inode->vfs_inode);
3010 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3011 wake_up(&fs_info->delayed_iputs_wait);
3012 spin_lock(&fs_info->delayed_iput_lock);
3013}
3014
3015static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3016 struct btrfs_inode *inode)
3017{
3018 if (!list_empty(&inode->delayed_iput)) {
3019 spin_lock(&fs_info->delayed_iput_lock);
3020 if (!list_empty(&inode->delayed_iput))
3021 run_delayed_iput_locked(fs_info, inode);
3022 spin_unlock(&fs_info->delayed_iput_lock);
3023 }
3024}
3025
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003026void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003027{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003028
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003029 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003030 while (!list_empty(&fs_info->delayed_iputs)) {
3031 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003032
David Sterba8089fe62015-11-19 14:15:51 +01003033 inode = list_first_entry(&fs_info->delayed_iputs,
3034 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003035 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003036 }
David Sterba8089fe62015-11-19 14:15:51 +01003037 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003038}
3039
Josef Bacik034f7842018-12-03 11:06:52 -05003040/**
3041 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3042 * @fs_info - the fs_info for this fs
3043 * @return - EINTR if we were killed, 0 if nothing's pending
3044 *
3045 * This will wait on any delayed iputs that are currently running with KILLABLE
3046 * set. Once they are all done running we will return, unless we are killed in
3047 * which case we return EINTR. This helps in user operations like fallocate etc
3048 * that might get blocked on the iputs.
3049 */
3050int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3051{
3052 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3053 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3054 if (ret)
3055 return -EINTR;
3056 return 0;
3057}
3058
Yan, Zhengd68fc572010-05-16 10:49:58 -04003059/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003060 * This creates an orphan entry for the given inode in case something goes wrong
3061 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003062 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003063int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003064 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003065{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003066 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003067
Omar Sandoval27919062018-05-11 13:13:37 -07003068 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3069 if (ret && ret != -EEXIST) {
3070 btrfs_abort_transaction(trans, ret);
3071 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003072 }
3073
Yan, Zhengd68fc572010-05-16 10:49:58 -04003074 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003075}
3076
3077/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003078 * We have done the delete so we can go ahead and remove the orphan item for
3079 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003080 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003081static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003082 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003083{
Omar Sandoval27919062018-05-11 13:13:37 -07003084 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003085}
3086
3087/*
3088 * this cleans up any orphans that may be left on the list from the last use
3089 * of this root.
3090 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003091int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003092{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003093 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003094 struct btrfs_path *path;
3095 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003096 struct btrfs_key key, found_key;
3097 struct btrfs_trans_handle *trans;
3098 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003099 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003100 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003101
Yan, Zhengd68fc572010-05-16 10:49:58 -04003102 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003103 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003104
3105 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003106 if (!path) {
3107 ret = -ENOMEM;
3108 goto out;
3109 }
David Sterbae4058b52015-11-27 16:31:35 +01003110 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003111
3112 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003113 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003114 key.offset = (u64)-1;
3115
Josef Bacik7b128762008-07-24 12:17:14 -04003116 while (1) {
3117 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003118 if (ret < 0)
3119 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003120
3121 /*
3122 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003123 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003124 * find the key and see if we have stuff that matches
3125 */
3126 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003127 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003128 if (path->slots[0] == 0)
3129 break;
3130 path->slots[0]--;
3131 }
3132
3133 /* pull out the item */
3134 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003135 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3136
3137 /* make sure the item matches what we want */
3138 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3139 break;
David Sterba962a2982014-06-04 18:41:45 +02003140 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003141 break;
3142
3143 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003144 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003145
3146 /*
3147 * this is where we are basically btrfs_lookup, without the
3148 * crossing root thing. we store the inode number in the
3149 * offset of the orphan item.
3150 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003151
3152 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003153 btrfs_err(fs_info,
3154 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003155 ret = -EINVAL;
3156 goto out;
3157 }
3158
3159 last_objectid = found_key.offset;
3160
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003161 found_key.objectid = found_key.offset;
3162 found_key.type = BTRFS_INODE_ITEM_KEY;
3163 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003164 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303165 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003166 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003167 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003168
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003169 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003170 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003171 int is_dead_root = 0;
3172
3173 /*
3174 * this is an orphan in the tree root. Currently these
3175 * could come from 2 sources:
3176 * a) a snapshot deletion in progress
3177 * b) a free space cache inode
3178 * We need to distinguish those two, as the snapshot
3179 * orphan must not get deleted.
3180 * find_dead_roots already ran before us, so if this
3181 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003182 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003183 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003184
3185 spin_lock(&fs_info->fs_roots_radix_lock);
3186 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3187 (unsigned long)found_key.objectid);
3188 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3189 is_dead_root = 1;
3190 spin_unlock(&fs_info->fs_roots_radix_lock);
3191
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003192 if (is_dead_root) {
3193 /* prevent this orphan from being found again */
3194 key.offset = found_key.objectid - 1;
3195 continue;
3196 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003197
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003198 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003199
Josef Bacika8c9e572011-09-21 16:55:59 -04003200 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003201 * If we have an inode with links, there are a couple of
3202 * possibilities. Old kernels (before v3.12) used to create an
3203 * orphan item for truncate indicating that there were possibly
3204 * extent items past i_size that needed to be deleted. In v3.12,
3205 * truncate was changed to update i_size in sync with the extent
3206 * items, but the (useless) orphan item was still created. Since
3207 * v4.18, we don't create the orphan item for truncate at all.
3208 *
3209 * So, this item could mean that we need to do a truncate, but
3210 * only if this filesystem was last used on a pre-v3.12 kernel
3211 * and was not cleanly unmounted. The odds of that are quite
3212 * slim, and it's a pain to do the truncate now, so just delete
3213 * the orphan item.
3214 *
3215 * It's also possible that this orphan item was supposed to be
3216 * deleted but wasn't. The inode number may have been reused,
3217 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003218 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003219 if (ret == -ENOENT || inode->i_nlink) {
3220 if (!ret)
3221 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003222 trans = btrfs_start_transaction(root, 1);
3223 if (IS_ERR(trans)) {
3224 ret = PTR_ERR(trans);
3225 goto out;
3226 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003227 btrfs_debug(fs_info, "auto deleting %Lu",
3228 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003229 ret = btrfs_del_orphan_item(trans, root,
3230 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003231 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003232 if (ret)
3233 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003234 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003235 }
Josef Bacik7b128762008-07-24 12:17:14 -04003236
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003237 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003238
3239 /* this will do delete_inode and everything for us */
3240 iput(inode);
3241 }
Miao Xie3254c872011-11-10 20:45:05 -05003242 /* release the path since we're done with it */
3243 btrfs_release_path(path);
3244
Yan, Zhengd68fc572010-05-16 10:49:58 -04003245 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3246
Omar Sandovala575cee2018-05-11 13:13:38 -07003247 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003248 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003249 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003250 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003251 }
Josef Bacik7b128762008-07-24 12:17:14 -04003252
3253 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003254 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003255
3256out:
3257 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003258 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003259 btrfs_free_path(path);
3260 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003261}
3262
Chris Masond352ac62008-09-29 15:18:18 -04003263/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003264 * very simple check to peek ahead in the leaf looking for xattrs. If we
3265 * don't find any xattrs, we know there can't be any acls.
3266 *
3267 * slot is the slot the inode is in, objectid is the objectid of the inode
3268 */
3269static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003270 int slot, u64 objectid,
3271 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003272{
3273 u32 nritems = btrfs_header_nritems(leaf);
3274 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003275 static u64 xattr_access = 0;
3276 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003277 int scanned = 0;
3278
Josef Bacikf23b5a52013-06-19 10:16:26 -04003279 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003280 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3281 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3282 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3283 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003284 }
3285
Chris Mason46a53cc2009-04-27 11:47:50 -04003286 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003287 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003288 while (slot < nritems) {
3289 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3290
3291 /* we found a different objectid, there must not be acls */
3292 if (found_key.objectid != objectid)
3293 return 0;
3294
3295 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003296 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003297 if (*first_xattr_slot == -1)
3298 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003299 if (found_key.offset == xattr_access ||
3300 found_key.offset == xattr_default)
3301 return 1;
3302 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003303
3304 /*
3305 * we found a key greater than an xattr key, there can't
3306 * be any acls later on
3307 */
3308 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3309 return 0;
3310
3311 slot++;
3312 scanned++;
3313
3314 /*
3315 * it goes inode, inode backrefs, xattrs, extents,
3316 * so if there are a ton of hard links to an inode there can
3317 * be a lot of backrefs. Don't waste time searching too hard,
3318 * this is just an optimization
3319 */
3320 if (scanned >= 8)
3321 break;
3322 }
3323 /* we hit the end of the leaf before we found an xattr or
3324 * something larger than an xattr. We have to assume the inode
3325 * has acls
3326 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003327 if (*first_xattr_slot == -1)
3328 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003329 return 1;
3330}
3331
3332/*
Chris Masond352ac62008-09-29 15:18:18 -04003333 * read an inode from the btree into the in-memory inode
3334 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003335static int btrfs_read_locked_inode(struct inode *inode,
3336 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003337{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003338 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003339 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003340 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003341 struct btrfs_inode_item *inode_item;
3342 struct btrfs_root *root = BTRFS_I(inode)->root;
3343 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003344 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003345 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003346 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003347 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003348 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003349 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003350
3351 ret = btrfs_fill_inode(inode, &rdev);
3352 if (!ret)
3353 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003354
Filipe Manana4222ea72018-10-24 10:13:03 +01003355 if (!path) {
3356 path = btrfs_alloc_path();
3357 if (!path)
3358 return -ENOMEM;
3359 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003360
Chris Mason39279cc2007-06-12 06:35:45 -04003361 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003362
Chris Mason39279cc2007-06-12 06:35:45 -04003363 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003364 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003365 if (path != in_path)
3366 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003367 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003368 }
Chris Mason39279cc2007-06-12 06:35:45 -04003369
Chris Mason5f39d392007-10-15 16:14:19 -04003370 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003371
3372 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003373 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003374
Chris Mason5f39d392007-10-15 16:14:19 -04003375 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3376 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003377 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003378 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003379 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3380 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003381 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003382 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3383 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003384
David Sterbaa937b972014-12-12 17:39:12 +01003385 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3386 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003387
David Sterbaa937b972014-12-12 17:39:12 +01003388 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3389 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003390
David Sterbaa937b972014-12-12 17:39:12 +01003391 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3392 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003393
chandan r9cc97d62012-07-04 12:48:07 +05303394 BTRFS_I(inode)->i_otime.tv_sec =
3395 btrfs_timespec_sec(leaf, &inode_item->otime);
3396 BTRFS_I(inode)->i_otime.tv_nsec =
3397 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003398
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003399 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003400 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003401 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3402
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003403 inode_set_iversion_queried(inode,
3404 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003405 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003406 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003407 rdev = btrfs_inode_rdev(leaf, inode_item);
3408
Josef Bacikaec74772008-07-24 12:12:38 -04003409 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003410 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003411
3412cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003413 /*
3414 * If we were modified in the current generation and evicted from memory
3415 * and then re-read we need to do a full sync since we don't have any
3416 * idea about which extents were modified before we were evicted from
3417 * cache.
3418 *
3419 * This is required for both inode re-read from disk and delayed inode
3420 * in delayed_nodes_tree.
3421 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003422 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003423 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3424 &BTRFS_I(inode)->runtime_flags);
3425
Filipe Mananabde6c242015-07-24 00:00:19 +01003426 /*
3427 * We don't persist the id of the transaction where an unlink operation
3428 * against the inode was last made. So here we assume the inode might
3429 * have been evicted, and therefore the exact value of last_unlink_trans
3430 * lost, and set it to last_trans to avoid metadata inconsistencies
3431 * between the inode and its parent if the inode is fsync'ed and the log
3432 * replayed. For example, in the scenario:
3433 *
3434 * touch mydir/foo
3435 * ln mydir/foo mydir/bar
3436 * sync
3437 * unlink mydir/bar
3438 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3439 * xfs_io -c fsync mydir/foo
3440 * <power failure>
3441 * mount fs, triggers fsync log replay
3442 *
3443 * We must make sure that when we fsync our inode foo we also log its
3444 * parent inode, otherwise after log replay the parent still has the
3445 * dentry with the "bar" name but our inode foo has a link count of 1
3446 * and doesn't have an inode ref with the name "bar" anymore.
3447 *
3448 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003449 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003450 * transaction commits on fsync if our inode is a directory, or if our
3451 * inode is not a directory, logging its parent unnecessarily.
3452 */
3453 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3454
Filipe Manana3ebac172020-07-15 12:30:43 +01003455 /*
3456 * Same logic as for last_unlink_trans. We don't persist the generation
3457 * of the last transaction where this inode was used for a reflink
3458 * operation, so after eviction and reloading the inode we must be
3459 * pessimistic and assume the last transaction that modified the inode.
3460 */
3461 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3462
Miao Xie67de1172013-12-26 13:07:06 +08003463 path->slots[0]++;
3464 if (inode->i_nlink != 1 ||
3465 path->slots[0] >= btrfs_header_nritems(leaf))
3466 goto cache_acl;
3467
3468 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003469 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003470 goto cache_acl;
3471
3472 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3473 if (location.type == BTRFS_INODE_REF_KEY) {
3474 struct btrfs_inode_ref *ref;
3475
3476 ref = (struct btrfs_inode_ref *)ptr;
3477 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3478 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3479 struct btrfs_inode_extref *extref;
3480
3481 extref = (struct btrfs_inode_extref *)ptr;
3482 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3483 extref);
3484 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003485cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003486 /*
3487 * try to precache a NULL acl entry for files that don't have
3488 * any xattrs or acls
3489 */
Li Zefan33345d012011-04-20 10:31:50 +08003490 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003491 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003492 if (first_xattr_slot != -1) {
3493 path->slots[0] = first_xattr_slot;
3494 ret = btrfs_load_inode_props(inode, path);
3495 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003496 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003497 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003498 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003499 root->root_key.objectid, ret);
3500 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003501 if (path != in_path)
3502 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003503
Al Viro72c04902009-06-24 16:58:48 -04003504 if (!maybe_acls)
3505 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003506
Chris Mason39279cc2007-06-12 06:35:45 -04003507 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003508 case S_IFREG:
3509 inode->i_mapping->a_ops = &btrfs_aops;
3510 inode->i_fop = &btrfs_file_operations;
3511 inode->i_op = &btrfs_file_inode_operations;
3512 break;
3513 case S_IFDIR:
3514 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003515 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003516 break;
3517 case S_IFLNK:
3518 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003519 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003520 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003521 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003522 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003523 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003524 init_special_inode(inode, inode->i_mode, rdev);
3525 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003526 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003527
David Sterba7b6a2212018-03-26 18:40:21 +02003528 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003529 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003530}
3531
Chris Masond352ac62008-09-29 15:18:18 -04003532/*
3533 * given a leaf and an inode, copy the inode fields into the leaf
3534 */
Chris Masone02119d2008-09-05 16:13:11 -04003535static void fill_inode_item(struct btrfs_trans_handle *trans,
3536 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003537 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003538 struct inode *inode)
3539{
Liu Bo51fab692012-12-27 09:01:21 +00003540 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003541
David Sterbac82f8232019-08-09 17:48:21 +02003542 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003543
David Sterbacc4c13d2020-04-29 02:15:56 +02003544 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3545 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3546 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3547 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3548 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003549
David Sterbacc4c13d2020-04-29 02:15:56 +02003550 btrfs_set_token_timespec_sec(&token, &item->atime,
3551 inode->i_atime.tv_sec);
3552 btrfs_set_token_timespec_nsec(&token, &item->atime,
3553 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003554
David Sterbacc4c13d2020-04-29 02:15:56 +02003555 btrfs_set_token_timespec_sec(&token, &item->mtime,
3556 inode->i_mtime.tv_sec);
3557 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3558 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003559
David Sterbacc4c13d2020-04-29 02:15:56 +02003560 btrfs_set_token_timespec_sec(&token, &item->ctime,
3561 inode->i_ctime.tv_sec);
3562 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3563 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003564
David Sterbacc4c13d2020-04-29 02:15:56 +02003565 btrfs_set_token_timespec_sec(&token, &item->otime,
3566 BTRFS_I(inode)->i_otime.tv_sec);
3567 btrfs_set_token_timespec_nsec(&token, &item->otime,
3568 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303569
David Sterbacc4c13d2020-04-29 02:15:56 +02003570 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3571 btrfs_set_token_inode_generation(&token, item,
3572 BTRFS_I(inode)->generation);
3573 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3574 btrfs_set_token_inode_transid(&token, item, trans->transid);
3575 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3576 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3577 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003578}
3579
Chris Masond352ac62008-09-29 15:18:18 -04003580/*
3581 * copy everything in the in-memory inode into the btree.
3582 */
Chris Mason21151332011-11-10 20:39:08 -05003583static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003584 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003585{
3586 struct btrfs_inode_item *inode_item;
3587 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003588 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003589 int ret;
3590
3591 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003592 if (!path)
3593 return -ENOMEM;
3594
Miao Xie16cdcec2011-04-22 18:12:22 +08003595 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3596 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003597 if (ret) {
3598 if (ret > 0)
3599 ret = -ENOENT;
3600 goto failed;
3601 }
3602
Chris Mason5f39d392007-10-15 16:14:19 -04003603 leaf = path->nodes[0];
3604 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003605 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003606
Chris Masone02119d2008-09-05 16:13:11 -04003607 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003608 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovd9094412020-06-05 10:41:13 +03003609 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04003610 ret = 0;
3611failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003612 btrfs_free_path(path);
3613 return ret;
3614}
3615
Chris Masond352ac62008-09-29 15:18:18 -04003616/*
Chris Mason21151332011-11-10 20:39:08 -05003617 * copy everything in the in-memory inode into the btree.
3618 */
3619noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3620 struct btrfs_root *root, struct inode *inode)
3621{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003622 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003623 int ret;
3624
3625 /*
3626 * If the inode is a free space inode, we can deadlock during commit
3627 * if we put it into the delayed code.
3628 *
3629 * The data relocation inode should also be directly updated
3630 * without delay
3631 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003632 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003633 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003634 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003635 btrfs_update_root_times(trans, root);
3636
Chris Mason21151332011-11-10 20:39:08 -05003637 ret = btrfs_delayed_update_inode(trans, root, inode);
3638 if (!ret)
Nikolay Borisovd9094412020-06-05 10:41:13 +03003639 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
Chris Mason21151332011-11-10 20:39:08 -05003640 return ret;
3641 }
3642
3643 return btrfs_update_inode_item(trans, root, inode);
3644}
3645
Josef Bacikbe6aef62012-10-22 15:43:12 -04003646noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3647 struct btrfs_root *root,
3648 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003649{
3650 int ret;
3651
3652 ret = btrfs_update_inode(trans, root, inode);
3653 if (ret == -ENOSPC)
3654 return btrfs_update_inode_item(trans, root, inode);
3655 return ret;
3656}
3657
3658/*
Chris Masond352ac62008-09-29 15:18:18 -04003659 * unlink helper that gets used here in inode.c and in the tree logging
3660 * recovery code. It remove a link in a directory with a given name, and
3661 * also drops the back refs in the inode to the directory
3662 */
Al Viro92986792011-03-04 17:14:37 +00003663static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3664 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003665 struct btrfs_inode *dir,
3666 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003667 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003668{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003669 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003670 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003671 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003672 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003673 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003674 u64 ino = btrfs_ino(inode);
3675 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003676
3677 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003678 if (!path) {
3679 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003680 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003681 }
3682
Li Zefan33345d012011-04-20 10:31:50 +08003683 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003684 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003685 if (IS_ERR_OR_NULL(di)) {
3686 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003687 goto err;
3688 }
Chris Mason39279cc2007-06-12 06:35:45 -04003689 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003690 if (ret)
3691 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003692 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003693
Miao Xie67de1172013-12-26 13:07:06 +08003694 /*
3695 * If we don't have dir index, we have to get it by looking up
3696 * the inode ref, since we get the inode ref, remove it directly,
3697 * it is unnecessary to do delayed deletion.
3698 *
3699 * But if we have dir index, needn't search inode ref to get it.
3700 * Since the inode ref is close to the inode item, it is better
3701 * that we delay to delete it, and just do this deletion when
3702 * we update the inode item.
3703 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003704 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003705 ret = btrfs_delayed_delete_inode_ref(inode);
3706 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003707 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003708 goto skip_backref;
3709 }
3710 }
3711
Li Zefan33345d012011-04-20 10:31:50 +08003712 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3713 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003714 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003715 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003716 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003717 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003718 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003719 goto err;
3720 }
Miao Xie67de1172013-12-26 13:07:06 +08003721skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003722 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003723 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003724 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003725 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003726 }
Chris Mason39279cc2007-06-12 06:35:45 -04003727
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003728 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3729 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003730 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003731 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003732 goto err;
3733 }
Chris Masone02119d2008-09-05 16:13:11 -04003734
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003735 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3736 index);
Chris Mason6418c962010-10-30 07:34:24 -04003737 if (ret == -ENOENT)
3738 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003739 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003740 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003741
3742 /*
3743 * If we have a pending delayed iput we could end up with the final iput
3744 * being run in btrfs-cleaner context. If we have enough of these built
3745 * up we can end up burning a lot of time in btrfs-cleaner without any
3746 * way to throttle the unlinks. Since we're currently holding a ref on
3747 * the inode we can run the delayed iput here without any issues as the
3748 * final iput won't be done until after we drop the ref we're currently
3749 * holding.
3750 */
3751 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003752err:
3753 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003754 if (ret)
3755 goto out;
3756
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003757 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003758 inode_inc_iversion(&inode->vfs_inode);
3759 inode_inc_iversion(&dir->vfs_inode);
3760 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3761 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3762 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003763out:
Chris Mason39279cc2007-06-12 06:35:45 -04003764 return ret;
3765}
3766
Al Viro92986792011-03-04 17:14:37 +00003767int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3768 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003769 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003770 const char *name, int name_len)
3771{
3772 int ret;
3773 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3774 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003775 drop_nlink(&inode->vfs_inode);
3776 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003777 }
3778 return ret;
3779}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003780
3781/*
3782 * helper to start transaction for unlink and rmdir.
3783 *
Josef Bacikd52be812013-05-29 14:54:47 -04003784 * unlink and rmdir are special in btrfs, they do not always free space, so
3785 * if we cannot make our reservations the normal way try and see if there is
3786 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3787 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003788 */
Josef Bacikd52be812013-05-29 14:54:47 -04003789static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003790{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003791 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003792
Josef Bacike70bea52011-10-11 14:18:24 -04003793 /*
3794 * 1 for the possible orphan item
3795 * 1 for the dir item
3796 * 1 for the dir index
3797 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003798 * 1 for the inode
3799 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003800 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003801}
3802
Chris Mason39279cc2007-06-12 06:35:45 -04003803static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3804{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003805 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003806 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003807 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003808 int ret;
3809
Josef Bacikd52be812013-05-29 14:54:47 -04003810 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003811 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003812 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003813
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003814 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3815 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003816
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003817 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3818 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3819 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003820 if (ret)
3821 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003822
Yan, Zhenga22285a2010-05-16 10:48:46 -04003823 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003824 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003825 if (ret)
3826 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003827 }
Josef Bacik7b128762008-07-24 12:17:14 -04003828
Tsutomu Itohb5324022011-07-19 07:27:20 +00003829out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003830 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003831 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003832 return ret;
3833}
3834
Misono Tomohirof60a2362018-04-18 11:34:52 +09003835static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003836 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003837{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003838 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003839 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003840 struct btrfs_path *path;
3841 struct extent_buffer *leaf;
3842 struct btrfs_dir_item *di;
3843 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003844 const char *name = dentry->d_name.name;
3845 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003846 u64 index;
3847 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003848 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003849 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003850
Josef Bacik045d3962019-12-18 17:20:27 -05003851 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3852 objectid = inode->root->root_key.objectid;
3853 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3854 objectid = inode->location.objectid;
3855 } else {
3856 WARN_ON(1);
3857 return -EINVAL;
3858 }
3859
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003860 path = btrfs_alloc_path();
3861 if (!path)
3862 return -ENOMEM;
3863
Li Zefan33345d012011-04-20 10:31:50 +08003864 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003865 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003866 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003867 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003868 goto out;
3869 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003870
3871 leaf = path->nodes[0];
3872 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3873 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3874 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003875 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003876 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003877 goto out;
3878 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003879 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003880
Josef Bacikd49d3282019-12-18 17:20:28 -05003881 /*
3882 * This is a placeholder inode for a subvolume we didn't have a
3883 * reference to at the time of the snapshot creation. In the meantime
3884 * we could have renamed the real subvol link into our snapshot, so
3885 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3886 * Instead simply lookup the dir_index_item for this entry so we can
3887 * remove it. Otherwise we know we have a ref to the root and we can
3888 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3889 */
3890 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003891 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003892 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003893 if (IS_ERR_OR_NULL(di)) {
3894 if (!di)
3895 ret = -ENOENT;
3896 else
3897 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003898 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003899 goto out;
3900 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003901
3902 leaf = path->nodes[0];
3903 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003904 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003905 btrfs_release_path(path);
3906 } else {
3907 ret = btrfs_del_root_ref(trans, objectid,
3908 root->root_key.objectid, dir_ino,
3909 &index, name, name_len);
3910 if (ret) {
3911 btrfs_abort_transaction(trans, ret);
3912 goto out;
3913 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003914 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003915
Lu Fengqi9add2942018-08-01 11:32:26 +08003916 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003917 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003918 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003919 goto out;
3920 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003921
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003922 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003923 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003924 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003925 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003926 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003927 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003928out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003929 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003930 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003931}
3932
Misono Tomohiroec42f162018-04-18 11:34:13 +09003933/*
3934 * Helper to check if the subvolume references other subvolumes or if it's
3935 * default.
3936 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003937static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003938{
3939 struct btrfs_fs_info *fs_info = root->fs_info;
3940 struct btrfs_path *path;
3941 struct btrfs_dir_item *di;
3942 struct btrfs_key key;
3943 u64 dir_id;
3944 int ret;
3945
3946 path = btrfs_alloc_path();
3947 if (!path)
3948 return -ENOMEM;
3949
3950 /* Make sure this root isn't set as the default subvol */
3951 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3952 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3953 dir_id, "default", 7, 0);
3954 if (di && !IS_ERR(di)) {
3955 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3956 if (key.objectid == root->root_key.objectid) {
3957 ret = -EPERM;
3958 btrfs_err(fs_info,
3959 "deleting default subvolume %llu is not allowed",
3960 key.objectid);
3961 goto out;
3962 }
3963 btrfs_release_path(path);
3964 }
3965
3966 key.objectid = root->root_key.objectid;
3967 key.type = BTRFS_ROOT_REF_KEY;
3968 key.offset = (u64)-1;
3969
3970 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3971 if (ret < 0)
3972 goto out;
3973 BUG_ON(ret == 0);
3974
3975 ret = 0;
3976 if (path->slots[0] > 0) {
3977 path->slots[0]--;
3978 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3979 if (key.objectid == root->root_key.objectid &&
3980 key.type == BTRFS_ROOT_REF_KEY)
3981 ret = -ENOTEMPTY;
3982 }
3983out:
3984 btrfs_free_path(path);
3985 return ret;
3986}
3987
Nikolay Borisov20a68002018-04-27 14:36:24 +03003988/* Delete all dentries for inodes belonging to the root */
3989static void btrfs_prune_dentries(struct btrfs_root *root)
3990{
3991 struct btrfs_fs_info *fs_info = root->fs_info;
3992 struct rb_node *node;
3993 struct rb_node *prev;
3994 struct btrfs_inode *entry;
3995 struct inode *inode;
3996 u64 objectid = 0;
3997
3998 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3999 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4000
4001 spin_lock(&root->inode_lock);
4002again:
4003 node = root->inode_tree.rb_node;
4004 prev = NULL;
4005 while (node) {
4006 prev = node;
4007 entry = rb_entry(node, struct btrfs_inode, rb_node);
4008
David Sterba37508512018-06-29 10:56:40 +02004009 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004010 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004011 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004012 node = node->rb_right;
4013 else
4014 break;
4015 }
4016 if (!node) {
4017 while (prev) {
4018 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004019 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004020 node = prev;
4021 break;
4022 }
4023 prev = rb_next(prev);
4024 }
4025 }
4026 while (node) {
4027 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004028 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004029 inode = igrab(&entry->vfs_inode);
4030 if (inode) {
4031 spin_unlock(&root->inode_lock);
4032 if (atomic_read(&inode->i_count) > 1)
4033 d_prune_aliases(inode);
4034 /*
4035 * btrfs_drop_inode will have it removed from the inode
4036 * cache when its usage count hits zero.
4037 */
4038 iput(inode);
4039 cond_resched();
4040 spin_lock(&root->inode_lock);
4041 goto again;
4042 }
4043
4044 if (cond_resched_lock(&root->inode_lock))
4045 goto again;
4046
4047 node = rb_next(node);
4048 }
4049 spin_unlock(&root->inode_lock);
4050}
4051
Misono Tomohirof60a2362018-04-18 11:34:52 +09004052int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4053{
4054 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4055 struct btrfs_root *root = BTRFS_I(dir)->root;
4056 struct inode *inode = d_inode(dentry);
4057 struct btrfs_root *dest = BTRFS_I(inode)->root;
4058 struct btrfs_trans_handle *trans;
4059 struct btrfs_block_rsv block_rsv;
4060 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004061 int ret;
4062 int err;
4063
4064 /*
4065 * Don't allow to delete a subvolume with send in progress. This is
4066 * inside the inode lock so the error handling that has to drop the bit
4067 * again is not run concurrently.
4068 */
4069 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004070 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004071 spin_unlock(&dest->root_item_lock);
4072 btrfs_warn(fs_info,
4073 "attempt to delete subvolume %llu during send",
4074 dest->root_key.objectid);
4075 return -EPERM;
4076 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004077 root_flags = btrfs_root_flags(&dest->root_item);
4078 btrfs_set_root_flags(&dest->root_item,
4079 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4080 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004081
4082 down_write(&fs_info->subvol_sem);
4083
4084 err = may_destroy_subvol(dest);
4085 if (err)
4086 goto out_up_write;
4087
4088 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4089 /*
4090 * One for dir inode,
4091 * two for dir entries,
4092 * two for root ref/backref.
4093 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004094 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004095 if (err)
4096 goto out_up_write;
4097
4098 trans = btrfs_start_transaction(root, 0);
4099 if (IS_ERR(trans)) {
4100 err = PTR_ERR(trans);
4101 goto out_release;
4102 }
4103 trans->block_rsv = &block_rsv;
4104 trans->bytes_reserved = block_rsv.size;
4105
4106 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4107
Josef Bacik045d3962019-12-18 17:20:27 -05004108 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004109 if (ret) {
4110 err = ret;
4111 btrfs_abort_transaction(trans, ret);
4112 goto out_end_trans;
4113 }
4114
4115 btrfs_record_root_in_trans(trans, dest);
4116
4117 memset(&dest->root_item.drop_progress, 0,
4118 sizeof(dest->root_item.drop_progress));
David Sterbac8422682020-09-15 21:44:52 +02004119 btrfs_set_root_drop_level(&dest->root_item, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004120 btrfs_set_root_refs(&dest->root_item, 0);
4121
4122 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4123 ret = btrfs_insert_orphan_item(trans,
4124 fs_info->tree_root,
4125 dest->root_key.objectid);
4126 if (ret) {
4127 btrfs_abort_transaction(trans, ret);
4128 err = ret;
4129 goto out_end_trans;
4130 }
4131 }
4132
Lu Fengqid1957792018-05-29 15:01:54 +08004133 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004134 BTRFS_UUID_KEY_SUBVOL,
4135 dest->root_key.objectid);
4136 if (ret && ret != -ENOENT) {
4137 btrfs_abort_transaction(trans, ret);
4138 err = ret;
4139 goto out_end_trans;
4140 }
4141 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004142 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004143 dest->root_item.received_uuid,
4144 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4145 dest->root_key.objectid);
4146 if (ret && ret != -ENOENT) {
4147 btrfs_abort_transaction(trans, ret);
4148 err = ret;
4149 goto out_end_trans;
4150 }
4151 }
4152
Qu Wenruo082b6c92020-06-16 10:17:37 +08004153 free_anon_bdev(dest->anon_dev);
4154 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004155out_end_trans:
4156 trans->block_rsv = NULL;
4157 trans->bytes_reserved = 0;
4158 ret = btrfs_end_transaction(trans);
4159 if (ret && !err)
4160 err = ret;
4161 inode->i_flags |= S_DEAD;
4162out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004163 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004164out_up_write:
4165 up_write(&fs_info->subvol_sem);
4166 if (err) {
4167 spin_lock(&dest->root_item_lock);
4168 root_flags = btrfs_root_flags(&dest->root_item);
4169 btrfs_set_root_flags(&dest->root_item,
4170 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4171 spin_unlock(&dest->root_item_lock);
4172 } else {
4173 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004174 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004175 ASSERT(dest->send_in_progress == 0);
4176
4177 /* the last ref */
4178 if (dest->ino_cache_inode) {
4179 iput(dest->ino_cache_inode);
4180 dest->ino_cache_inode = NULL;
4181 }
4182 }
4183
4184 return err;
4185}
4186
Chris Mason39279cc2007-06-12 06:35:45 -04004187static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4188{
David Howells2b0143b2015-03-17 22:25:59 +00004189 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004190 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004191 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004192 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004193 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004194
David Sterbab3ae2442012-09-13 16:04:34 -06004195 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004196 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004197 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004198 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004199
Josef Bacikd52be812013-05-29 14:54:47 -04004200 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004201 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004202 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004203
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004204 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004205 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004206 goto out;
4207 }
4208
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004209 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004210 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004211 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004212
Filipe Manana44f714d2016-06-06 16:11:13 +01004213 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4214
Chris Mason39279cc2007-06-12 06:35:45 -04004215 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004216 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4217 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4218 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004219 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004220 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004221 /*
4222 * Propagate the last_unlink_trans value of the deleted dir to
4223 * its parent directory. This is to prevent an unrecoverable
4224 * log tree in the case we do something like this:
4225 * 1) create dir foo
4226 * 2) create snapshot under dir foo
4227 * 3) delete the snapshot
4228 * 4) rmdir foo
4229 * 5) mkdir foo
4230 * 6) fsync foo or some file inside foo
4231 */
4232 if (last_unlink_trans >= trans->transid)
4233 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4234 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004235out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004236 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004237 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004238
Chris Mason39279cc2007-06-12 06:35:45 -04004239 return err;
4240}
4241
Josef Bacikddfae632017-10-19 14:16:02 -04004242/*
4243 * Return this if we need to call truncate_block for the last bit of the
4244 * truncate.
4245 */
4246#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004247
Chris Mason323ac952008-10-01 19:05:46 -04004248/*
Chris Mason39279cc2007-06-12 06:35:45 -04004249 * this can truncate away extent items, csum items and directory items.
4250 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004251 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004252 *
4253 * csum items that cross the new i_size are truncated to the new size
4254 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004255 *
4256 * min_type is the minimum key type to truncate down to. If set to 0, this
4257 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004258 */
Yan, Zheng80825102009-11-12 09:35:36 +00004259int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4260 struct btrfs_root *root,
4261 struct inode *inode,
4262 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004263{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004264 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004265 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004266 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004267 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004268 struct btrfs_key key;
4269 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004270 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004271 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004272 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004273 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004274 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004275 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004276 int found_extent;
4277 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004278 int pending_del_nr = 0;
4279 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004280 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004281 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004282 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004283 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004284 bool be_nice = false;
4285 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004286 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4287 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004288
4289 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004290
Chris Mason28ed1342014-12-17 09:41:04 -08004291 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004292 * For non-free space inodes and non-shareable roots, we want to back
4293 * off from time to time. This means all inodes in subvolume roots,
4294 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004295 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004296 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004297 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004298 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004299
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004300 path = btrfs_alloc_path();
4301 if (!path)
4302 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004303 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004304
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004305 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004306 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4307 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004308
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004309 /*
4310 * We want to drop from the next block forward in case this
4311 * new size is not block aligned since we will be keeping the
4312 * last block of the extent just the way it is.
4313 */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004314 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004315 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004316 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004317 }
Yan, Zheng80825102009-11-12 09:35:36 +00004318
Miao Xie16cdcec2011-04-22 18:12:22 +08004319 /*
4320 * This function is also used to drop the items in the log tree before
4321 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004322 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004323 * items.
4324 */
4325 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004326 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004327
Li Zefan33345d012011-04-20 10:31:50 +08004328 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004329 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004330 key.type = (u8)-1;
4331
Chris Mason85e21ba2008-01-29 15:11:36 -05004332search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004333 /*
4334 * with a 16K leaf size and 128MB extents, you can actually queue
4335 * up a huge file in a single leaf. Most of the time that
4336 * bytes_deleted is > 0, it will be huge by the time we get here
4337 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004338 if (be_nice && bytes_deleted > SZ_32M &&
4339 btrfs_should_end_transaction(trans)) {
4340 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004341 goto out;
4342 }
Chris Masond3977122009-01-05 21:25:51 -05004343
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004344 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4345 if (ret < 0)
4346 goto out;
4347
Chris Mason85e21ba2008-01-29 15:11:36 -05004348 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004349 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004350 /* there are no items in the tree for us to truncate, we're
4351 * done
4352 */
Yan, Zheng80825102009-11-12 09:35:36 +00004353 if (path->slots[0] == 0)
4354 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004355 path->slots[0]--;
4356 }
4357
Chris Masond3977122009-01-05 21:25:51 -05004358 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004359 u64 clear_start = 0, clear_len = 0;
4360
Chris Mason39279cc2007-06-12 06:35:45 -04004361 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004362 leaf = path->nodes[0];
4363 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004364 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004365
Li Zefan33345d012011-04-20 10:31:50 +08004366 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004367 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004368
Chris Mason85e21ba2008-01-29 15:11:36 -05004369 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004370 break;
4371
Chris Mason5f39d392007-10-15 16:14:19 -04004372 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004373 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004374 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004375 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004376 extent_type = btrfs_file_extent_type(leaf, fi);
4377 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004378 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004379 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004380
4381 trace_btrfs_truncate_show_fi_regular(
4382 BTRFS_I(inode), leaf, fi,
4383 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004384 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004385 item_end += btrfs_file_extent_ram_bytes(leaf,
4386 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004387
4388 trace_btrfs_truncate_show_fi_inline(
4389 BTRFS_I(inode), leaf, fi, path->slots[0],
4390 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004391 }
Yan008630c2007-11-07 13:31:09 -05004392 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004393 }
Yan, Zheng80825102009-11-12 09:35:36 +00004394 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004395 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004396 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004397 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004398 break;
4399 if (found_key.offset >= new_size)
4400 del_item = 1;
4401 else
4402 del_item = 0;
4403 }
Chris Mason39279cc2007-06-12 06:35:45 -04004404 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004405 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004406 if (found_type != BTRFS_EXTENT_DATA_KEY)
4407 goto delete;
4408
4409 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004410 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004411
4412 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004413 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004414 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004415 u64 orig_num_bytes =
4416 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004417 extent_num_bytes = ALIGN(new_size -
4418 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004419 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004420 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004421 btrfs_set_file_extent_num_bytes(leaf, fi,
4422 extent_num_bytes);
4423 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004424 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004425 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004426 &root->state) &&
4427 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004428 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004429 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004430 } else {
Chris Masondb945352007-10-15 16:15:53 -04004431 extent_num_bytes =
4432 btrfs_file_extent_disk_num_bytes(leaf,
4433 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004434 extent_offset = found_key.offset -
4435 btrfs_file_extent_offset(leaf, fi);
4436
Chris Mason39279cc2007-06-12 06:35:45 -04004437 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004438 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004439 if (extent_start != 0) {
4440 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004441 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004442 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004443 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004444 }
4445 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004446 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004447 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004448 /*
4449 * we can't truncate inline items that have had
4450 * special encodings
4451 */
4452 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004453 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004454 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4455 btrfs_file_extent_compression(leaf, fi) == 0) {
4456 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004457
Josef Bacikddfae632017-10-19 14:16:02 -04004458 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4459 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004460 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004461 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004462 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004463 * We have to bail so the last_size is set to
4464 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004465 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004466 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004467 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004468 } else {
4469 /*
4470 * Inline extents are special, we just treat
4471 * them as a full sector worth in the file
4472 * extent tree just for simplicity sake.
4473 */
4474 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004475 }
Josef Bacikddfae632017-10-19 14:16:02 -04004476
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004477 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Josef Bacikddfae632017-10-19 14:16:02 -04004478 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004479 }
Chris Mason179e29e2007-11-01 11:28:41 -04004480delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004481 /*
4482 * We use btrfs_truncate_inode_items() to clean up log trees for
4483 * multiple fsyncs, and in this case we don't want to clear the
4484 * file extent range because it's just the log.
4485 */
4486 if (root == BTRFS_I(inode)->root) {
4487 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4488 clear_start, clear_len);
4489 if (ret) {
4490 btrfs_abort_transaction(trans, ret);
4491 break;
4492 }
4493 }
4494
Josef Bacikddfae632017-10-19 14:16:02 -04004495 if (del_item)
4496 last_size = found_key.offset;
4497 else
4498 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004499 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004500 if (!pending_del_nr) {
4501 /* no pending yet, add ourselves */
4502 pending_del_slot = path->slots[0];
4503 pending_del_nr = 1;
4504 } else if (pending_del_nr &&
4505 path->slots[0] + 1 == pending_del_slot) {
4506 /* hop on the pending chunk */
4507 pending_del_nr++;
4508 pending_del_slot = path->slots[0];
4509 } else {
Chris Masond3977122009-01-05 21:25:51 -05004510 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004511 }
Chris Mason39279cc2007-06-12 06:35:45 -04004512 } else {
4513 break;
4514 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004515 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004516
Miao Xie27cdeb72014-04-02 19:51:05 +08004517 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004518 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004519 struct btrfs_ref ref = { 0 };
4520
Chris Mason28ed1342014-12-17 09:41:04 -08004521 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004522
4523 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4524 extent_start, extent_num_bytes, 0);
4525 ref.real_root = root->root_key.objectid;
4526 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4527 ino, extent_offset);
4528 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004529 if (ret) {
4530 btrfs_abort_transaction(trans, ret);
4531 break;
4532 }
Chris Mason28f75a02015-02-04 06:59:29 -08004533 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004534 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004535 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004536 }
Chris Mason39279cc2007-06-12 06:35:45 -04004537 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004538
Yan, Zheng80825102009-11-12 09:35:36 +00004539 if (found_type == BTRFS_INODE_ITEM_KEY)
4540 break;
4541
4542 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004543 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004544 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004545 if (pending_del_nr) {
4546 ret = btrfs_del_items(trans, root, path,
4547 pending_del_slot,
4548 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004549 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004550 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004551 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004552 }
Yan, Zheng80825102009-11-12 09:35:36 +00004553 pending_del_nr = 0;
4554 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004555 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004556
Chris Mason28f75a02015-02-04 06:59:29 -08004557 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004558 * We can generate a lot of delayed refs, so we need to
4559 * throttle every once and a while and make sure we're
4560 * adding enough space to keep up with the work we are
4561 * generating. Since we hold a transaction here we
4562 * can't flush, and we don't want to FLUSH_LIMIT because
4563 * we could have generated too many delayed refs to
4564 * actually allocate, so just bail if we're short and
4565 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004566 */
Josef Bacik28bad212018-12-03 10:20:38 -05004567 if (should_throttle) {
4568 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4569 BTRFS_RESERVE_NO_FLUSH);
4570 if (ret) {
4571 ret = -EAGAIN;
4572 break;
4573 }
Chris Mason28f75a02015-02-04 06:59:29 -08004574 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004575 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004576 } else {
4577 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004578 }
Chris Mason39279cc2007-06-12 06:35:45 -04004579 }
Yan, Zheng80825102009-11-12 09:35:36 +00004580out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004581 if (ret >= 0 && pending_del_nr) {
4582 int err;
4583
4584 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004585 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004586 if (err) {
4587 btrfs_abort_transaction(trans, err);
4588 ret = err;
4589 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004590 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004591 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4592 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004593 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004594 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004595 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004596 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4597 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004598 }
Chris Mason28ed1342014-12-17 09:41:04 -08004599
Chris Mason39279cc2007-06-12 06:35:45 -04004600 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004601 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004602}
4603
Chris Masona52d9a82007-08-27 16:49:44 -04004604/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304605 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004606 * @inode - inode that we're zeroing
4607 * @from - the offset to start zeroing
4608 * @len - the length to zero, 0 to zero the entire range respective to the
4609 * offset
4610 * @front - zero up to the offset instead of from the offset on
4611 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304612 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004613 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004614 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304615int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004616 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004617{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004618 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004619 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004620 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4621 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004622 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004623 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004624 char *kaddr;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004625 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004626 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004627 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304628 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004629 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004630 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004631 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004632 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304633 u64 block_start;
4634 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004635
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004636 if (IS_ALIGNED(offset, blocksize) &&
4637 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004638 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304639
Josef Bacik8b62f872017-10-19 14:15:55 -04004640 block_start = round_down(from, blocksize);
4641 block_end = block_start + blocksize - 1;
4642
Nikolay Borisov36ea6f32020-06-03 08:55:41 +03004643 ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved,
4644 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004645 if (ret < 0) {
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004646 if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start,
4647 &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004648 /* For nocow case, no need to reserve data space */
4649 only_release_metadata = true;
4650 } else {
4651 goto out;
4652 }
4653 }
4654 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize);
4655 if (ret < 0) {
4656 if (!only_release_metadata)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03004657 btrfs_free_reserved_data_space(BTRFS_I(inode),
4658 data_reserved, block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004659 goto out;
4660 }
Chris Mason211c17f2008-05-15 09:13:45 -04004661again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004662 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004663 if (!page) {
Nikolay Borisov86d52922020-06-03 08:55:40 +03004664 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004665 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004666 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004667 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004668 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004669 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004670
Chris Masona52d9a82007-08-27 16:49:44 -04004671 if (!PageUptodate(page)) {
4672 ret = btrfs_readpage(NULL, page);
4673 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004674 if (page->mapping != mapping) {
4675 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004676 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004677 goto again;
4678 }
Chris Masona52d9a82007-08-27 16:49:44 -04004679 if (!PageUptodate(page)) {
4680 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004681 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004682 }
4683 }
Chris Mason211c17f2008-05-15 09:13:45 -04004684 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004685
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304686 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004687 set_page_extent_mapped(page);
4688
Nikolay Borisovc3504372020-06-03 08:55:03 +03004689 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004690 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304691 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004692 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004693 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004694 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03004695 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004696 btrfs_put_ordered_extent(ordered);
4697 goto again;
4698 }
4699
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304700 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004701 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4702 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004703
Nikolay Borisovc2566f22020-06-03 08:55:35 +03004704 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004705 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004706 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304707 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004708 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004709 goto out_unlock;
4710 }
4711
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304712 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004713 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304714 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004715 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004716 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304717 memset(kaddr + (block_start - page_offset(page)),
4718 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004719 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304720 memset(kaddr + (block_start - page_offset(page)) + offset,
4721 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004722 flush_dcache_page(page);
4723 kunmap(page);
4724 }
Chris Mason247e7432008-07-17 12:53:51 -04004725 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004726 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004727 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004728
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004729 if (only_release_metadata)
4730 set_extent_bit(&BTRFS_I(inode)->io_tree, block_start,
Qu Wenruo3f6bb4a2020-10-21 14:24:51 +08004731 block_end, EXTENT_NORESERVE, NULL, GFP_NOFS);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004732
Chris Mason89642222008-07-24 09:41:53 -04004733out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004734 if (ret) {
4735 if (only_release_metadata)
4736 btrfs_delalloc_release_metadata(BTRFS_I(inode),
4737 blocksize, true);
4738 else
Nikolay Borisov86d52922020-06-03 08:55:40 +03004739 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004740 block_start, blocksize, true);
4741 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08004742 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004743 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004744 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004745out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004746 if (only_release_metadata)
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004747 btrfs_check_nocow_unlock(BTRFS_I(inode));
Qu Wenruo364ecf32017-02-27 15:10:38 +08004748 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004749 return ret;
4750}
4751
Josef Bacik16e75492013-10-22 12:18:51 -04004752static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4753 u64 offset, u64 len)
4754{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004755 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004756 struct btrfs_trans_handle *trans;
Filipe Manana5893dfb2020-11-04 11:07:32 +00004757 struct btrfs_drop_extents_args drop_args = { 0 };
Josef Bacik16e75492013-10-22 12:18:51 -04004758 int ret;
4759
4760 /*
4761 * Still need to make sure the inode looks like it's been updated so
4762 * that any holes get logged if we fsync.
4763 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004764 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4765 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004766 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4767 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4768 return 0;
4769 }
4770
4771 /*
4772 * 1 - for the one we're dropping
4773 * 1 - for the one we're adding
4774 * 1 - for updating the inode.
4775 */
4776 trans = btrfs_start_transaction(root, 3);
4777 if (IS_ERR(trans))
4778 return PTR_ERR(trans);
4779
Filipe Manana5893dfb2020-11-04 11:07:32 +00004780 drop_args.start = offset;
4781 drop_args.end = offset + len;
4782 drop_args.drop_cache = true;
4783
4784 ret = btrfs_drop_extents(trans, root, BTRFS_I(inode), &drop_args);
Josef Bacik16e75492013-10-22 12:18:51 -04004785 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004786 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004787 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004788 return ret;
4789 }
4790
David Sterbaf85b7372017-01-20 14:54:07 +01004791 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4792 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004793 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004794 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004795 else
4796 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004797 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004798 return ret;
4799}
4800
Josef Bacik695a0d02011-03-04 15:46:53 -05004801/*
4802 * This function puts in dummy file extents for the area we're creating a hole
4803 * for. So if we are truncating this file to a larger size we need to insert
4804 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4805 * the range between oldsize and size
4806 */
Josef Bacika41ad392011-01-31 15:30:16 -05004807int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004808{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004809 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004810 struct btrfs_root *root = BTRFS_I(inode)->root;
4811 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004812 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004813 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004814 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004815 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4816 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004817 u64 last_byte;
4818 u64 cur_offset;
4819 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004820 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004821
Josef Bacika71754f2013-06-17 17:14:39 -04004822 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304823 * If our size started in the middle of a block we need to zero out the
4824 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004825 * expose stale data.
4826 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304827 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004828 if (err)
4829 return err;
4830
Yan Zheng9036c102008-10-30 14:19:41 -04004831 if (size <= hole_start)
4832 return 0;
4833
David Sterbab272ae22020-02-05 19:09:33 +01004834 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004835 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004836 cur_offset = hole_start;
4837 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004838 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004839 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004840 if (IS_ERR(em)) {
4841 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004842 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004843 break;
4844 }
Yan Zheng9036c102008-10-30 14:19:41 -04004845 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004846 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004847 hole_size = last_byte - cur_offset;
4848
Yan, Zheng80825102009-11-12 09:35:36 +00004849 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004850 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004851
Josef Bacik16e75492013-10-22 12:18:51 -04004852 err = maybe_insert_hole(root, inode, cur_offset,
4853 hole_size);
4854 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004855 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004856
4857 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4858 cur_offset, hole_size);
4859 if (err)
4860 break;
4861
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004862 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004863 cur_offset + hole_size - 1, 0);
4864 hole_em = alloc_extent_map();
4865 if (!hole_em) {
4866 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4867 &BTRFS_I(inode)->runtime_flags);
4868 goto next;
4869 }
4870 hole_em->start = cur_offset;
4871 hole_em->len = hole_size;
4872 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004873
Josef Bacik5dc562c2012-08-17 13:14:17 -04004874 hole_em->block_start = EXTENT_MAP_HOLE;
4875 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004876 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004877 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004878 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004879 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004880
4881 while (1) {
4882 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004883 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004884 write_unlock(&em_tree->lock);
4885 if (err != -EEXIST)
4886 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004887 btrfs_drop_extent_cache(BTRFS_I(inode),
4888 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004889 cur_offset +
4890 hole_size - 1, 0);
4891 }
4892 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004893 } else {
4894 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4895 cur_offset, hole_size);
4896 if (err)
4897 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004898 }
Josef Bacik16e75492013-10-22 12:18:51 -04004899next:
Yan Zheng9036c102008-10-30 14:19:41 -04004900 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004901 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004902 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004903 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004904 break;
4905 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004906 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004907 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004908 return err;
4909}
4910
Eric Sandeen3972f262013-01-12 02:57:22 +00004911static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004912{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004913 struct btrfs_root *root = BTRFS_I(inode)->root;
4914 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004915 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004916 loff_t newsize = attr->ia_size;
4917 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004918 int ret;
4919
Eric Sandeen3972f262013-01-12 02:57:22 +00004920 /*
4921 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4922 * special case where we need to update the times despite not having
4923 * these flags set. For all other operations the VFS set these flags
4924 * explicitly if it wants a timestamp update.
4925 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004926 if (newsize != oldsize) {
4927 inode_inc_iversion(inode);
4928 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4929 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004930 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004931 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004932
Josef Bacika41ad392011-01-31 15:30:16 -05004933 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004934 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004935 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004936 * This is to ensure the snapshot captures a fully consistent
4937 * state of this file - if the snapshot captures this expanding
4938 * truncation, it must capture all writes that happened before
4939 * this truncation.
4940 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004941 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004942 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004943 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004944 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004945 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004946 }
Yan, Zheng80825102009-11-12 09:35:36 +00004947
Miao Xief4a2f4c2011-12-14 20:12:01 -05004948 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004949 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004950 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004951 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004952 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004953
4954 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004955 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304956 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004957 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004958 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004959 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004960 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004961
Josef Bacika41ad392011-01-31 15:30:16 -05004962 /*
4963 * We're truncating a file that used to have good data down to
Nikolay Borisov1fd40332020-10-01 09:40:39 +03004964 * zero. Make sure any new writes to the file get on disk
4965 * on close.
Josef Bacika41ad392011-01-31 15:30:16 -05004966 */
4967 if (newsize == 0)
Nikolay Borisov1fd40332020-10-01 09:40:39 +03004968 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Josef Bacik72ac3c02012-05-23 14:13:11 -04004969 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004970
Josef Bacika41ad392011-01-31 15:30:16 -05004971 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004972
Miao Xie2e60a512013-02-08 07:01:08 +00004973 inode_dio_wait(inode);
Miao Xie2e60a512013-02-08 07:01:08 +00004974
Filipe Manana213e8c52018-02-06 20:40:31 +00004975 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004976 if (ret && inode->i_nlink) {
4977 int err;
4978
4979 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004980 * Truncate failed, so fix up the in-memory size. We
4981 * adjusted disk_i_size down as we removed extents, so
4982 * wait for disk_i_size to be stable and then update the
4983 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004984 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004985 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004986 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004987 return err;
4988 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004989 }
Yan, Zheng80825102009-11-12 09:35:36 +00004990 }
4991
Josef Bacika41ad392011-01-31 15:30:16 -05004992 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004993}
4994
Chris Mason39279cc2007-06-12 06:35:45 -04004995static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4996{
David Howells2b0143b2015-03-17 22:25:59 +00004997 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004998 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004999 int err;
5000
Li Zefanb83cc962010-12-20 16:04:08 +08005001 if (btrfs_root_readonly(root))
5002 return -EROFS;
5003
Jan Kara31051c82016-05-26 16:55:18 +02005004 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005005 if (err)
5006 return err;
5007
Chris Mason5a3f23d2009-03-31 13:27:11 -04005008 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005009 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005010 if (err)
5011 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005012 }
Yan Zheng9036c102008-10-30 14:19:41 -04005013
Christoph Hellwig10257742010-06-04 11:30:02 +02005014 if (attr->ia_valid) {
5015 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005016 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005017 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005018
Josef Bacik22c44fe2011-11-30 10:45:38 -05005019 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005020 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005021 }
5022
Chris Mason39279cc2007-06-12 06:35:45 -04005023 return err;
5024}
Chris Mason61295eb2008-01-14 16:24:38 -05005025
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005026/*
5027 * While truncating the inode pages during eviction, we get the VFS calling
5028 * btrfs_invalidatepage() against each page of the inode. This is slow because
5029 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5030 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5031 * extent_state structures over and over, wasting lots of time.
5032 *
5033 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5034 * those expensive operations on a per page basis and do only the ordered io
5035 * finishing, while we release here the extent_map and extent_state structures,
5036 * without the excessive merging and splitting.
5037 */
5038static void evict_inode_truncate_pages(struct inode *inode)
5039{
5040 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5041 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5042 struct rb_node *node;
5043
5044 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005045 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005046
5047 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005048 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005049 struct extent_map *em;
5050
Liu Bo07e1ce02018-08-23 03:51:52 +08005051 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005052 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005053 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5054 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005055 remove_extent_mapping(map_tree, em);
5056 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005057 if (need_resched()) {
5058 write_unlock(&map_tree->lock);
5059 cond_resched();
5060 write_lock(&map_tree->lock);
5061 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005062 }
5063 write_unlock(&map_tree->lock);
5064
Filipe Manana6ca07092015-05-26 00:55:42 +01005065 /*
5066 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005067 * We can have ongoing bios started by readahead that have
5068 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005069 * still in progress (unlocked the pages in the bio but did not yet
5070 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005071 * ranges can still be locked and eviction started because before
5072 * submitting those bios, which are executed by a separate task (work
5073 * queue kthread), inode references (inode->i_count) were not taken
5074 * (which would be dropped in the end io callback of each bio).
5075 * Therefore here we effectively end up waiting for those bios and
5076 * anyone else holding locked ranges without having bumped the inode's
5077 * reference count - if we don't do it, when they access the inode's
5078 * io_tree to unlock a range it may be too late, leading to an
5079 * use-after-free issue.
5080 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005081 spin_lock(&io_tree->lock);
5082 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5083 struct extent_state *state;
5084 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005085 u64 start;
5086 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005087 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005088
5089 node = rb_first(&io_tree->state);
5090 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005091 start = state->start;
5092 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005093 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005094 spin_unlock(&io_tree->lock);
5095
David Sterbaff13db42015-12-03 14:30:40 +01005096 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005097
5098 /*
5099 * If still has DELALLOC flag, the extent didn't reach disk,
5100 * and its reserved space won't be freed by delayed_ref.
5101 * So we need to free its reserved space here.
5102 * (Refer to comment in btrfs_invalidatepage, case 2)
5103 *
5104 * Note, end is the bytenr of last byte, so we need + 1 here.
5105 */
Filipe Manana421f0922018-10-12 13:02:48 +01005106 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005107 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5108 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005109
Filipe Manana6ca07092015-05-26 00:55:42 +01005110 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005111 EXTENT_LOCKED | EXTENT_DELALLOC |
5112 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5113 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005114
Filipe Manana7064dd52014-08-08 02:47:05 +01005115 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005116 spin_lock(&io_tree->lock);
5117 }
5118 spin_unlock(&io_tree->lock);
5119}
5120
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005121static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005122 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005123{
5124 struct btrfs_fs_info *fs_info = root->fs_info;
5125 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005126 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005127 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005128 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005129
Josef Bacikd3984c92019-08-01 18:19:37 -04005130 /*
5131 * Eviction should be taking place at some place safe because of our
5132 * delayed iputs. However the normal flushing code will run delayed
5133 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5134 *
5135 * We reserve the delayed_refs_extra here again because we can't use
5136 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5137 * above. We reserve our extra bit here because we generate a ton of
5138 * delayed refs activity by truncating.
5139 *
5140 * If we cannot make our reservation we'll attempt to steal from the
5141 * global reserve, because we really want to be able to free up space.
5142 */
5143 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5144 BTRFS_RESERVE_FLUSH_EVICT);
5145 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005146 /*
5147 * Try to steal from the global reserve if there is space for
5148 * it.
5149 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005150 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5151 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5152 btrfs_warn(fs_info,
5153 "could not allocate space for delete; will truncate on mount");
5154 return ERR_PTR(-ENOSPC);
5155 }
5156 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005157 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005158
5159 trans = btrfs_join_transaction(root);
5160 if (IS_ERR(trans))
5161 return trans;
5162
5163 if (delayed_refs_extra) {
5164 trans->block_rsv = &fs_info->trans_block_rsv;
5165 trans->bytes_reserved = delayed_refs_extra;
5166 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5167 delayed_refs_extra, 1);
5168 }
5169 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005170}
5171
Al Virobd555972010-06-07 11:35:40 -04005172void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005173{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005174 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005175 struct btrfs_trans_handle *trans;
5176 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005177 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005178 int ret;
5179
liubo1abe9b82011-03-24 11:18:59 +00005180 trace_btrfs_inode_evict(inode);
5181
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005182 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005183 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005184 return;
5185 }
5186
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005187 evict_inode_truncate_pages(inode);
5188
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005189 if (inode->i_nlink &&
5190 ((btrfs_root_refs(&root->root_item) != 0 &&
5191 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005192 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005193 goto no_delete;
5194
Omar Sandoval27919062018-05-11 13:13:37 -07005195 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005196 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005197
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005198 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005199
Omar Sandoval7b40b692018-05-11 13:13:33 -07005200 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005201 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005202
Yan, Zheng76dda932009-09-21 16:00:26 -04005203 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005204 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5205 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005206 goto no_delete;
5207 }
5208
Nikolay Borisovaa790212017-01-10 20:35:40 +02005209 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005210 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005211 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005212
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005213 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005214 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005215 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005216 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005217 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005218
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005219 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005220
Yan, Zheng80825102009-11-12 09:35:36 +00005221 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005222 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005223 if (IS_ERR(trans))
5224 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005225
5226 trans->block_rsv = rsv;
5227
Yan, Zhengd68fc572010-05-16 10:49:58 -04005228 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005229 trans->block_rsv = &fs_info->trans_block_rsv;
5230 btrfs_end_transaction(trans);
5231 btrfs_btree_balance_dirty(fs_info);
5232 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5233 goto free_rsv;
5234 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005235 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005236 }
5237
Josef Bacik4ef31a42013-08-13 14:10:08 -04005238 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005239 * Errors here aren't a big deal, it just means we leave orphan items in
5240 * the tree. They will be cleaned up on the next mount. If the inode
5241 * number gets reused, cleanup deletes the orphan item without doing
5242 * anything, and unlink reuses the existing orphan item.
5243 *
5244 * If it turns out that we are dropping too many of these, we might want
5245 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005246 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005247 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005248 if (!IS_ERR(trans)) {
5249 trans->block_rsv = rsv;
5250 btrfs_orphan_del(trans, BTRFS_I(inode));
5251 trans->block_rsv = &fs_info->trans_block_rsv;
5252 btrfs_end_transaction(trans);
5253 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005254
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005255 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005256 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005257 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005258
Omar Sandoval27919062018-05-11 13:13:37 -07005259free_rsv:
5260 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005261no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005262 /*
5263 * If we didn't successfully delete, the orphan item will still be in
5264 * the tree and we'll retry on the next mount. Again, we might also want
5265 * to retry these periodically in the future.
5266 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005267 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005268 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005269}
5270
5271/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005272 * Return the key found in the dir entry in the location pointer, fill @type
5273 * with BTRFS_FT_*, and return 0.
5274 *
Su Yue005d6712018-03-05 17:13:37 +08005275 * If no dir entries were found, returns -ENOENT.
5276 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005277 */
5278static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005279 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005280{
5281 const char *name = dentry->d_name.name;
5282 int namelen = dentry->d_name.len;
5283 struct btrfs_dir_item *di;
5284 struct btrfs_path *path;
5285 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005286 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005287
5288 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005289 if (!path)
5290 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005291
David Sterbaf85b7372017-01-20 14:54:07 +01005292 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5293 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005294 if (IS_ERR_OR_NULL(di)) {
5295 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005296 goto out;
5297 }
Chris Masond3977122009-01-05 21:25:51 -05005298
Chris Mason5f39d392007-10-15 16:14:19 -04005299 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005300 if (location->type != BTRFS_INODE_ITEM_KEY &&
5301 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005302 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005303 btrfs_warn(root->fs_info,
5304"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5305 __func__, name, btrfs_ino(BTRFS_I(dir)),
5306 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005307 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005308 if (!ret)
5309 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005310out:
Chris Mason39279cc2007-06-12 06:35:45 -04005311 btrfs_free_path(path);
5312 return ret;
5313}
5314
5315/*
5316 * when we hit a tree root in a directory, the btrfs part of the inode
5317 * needs to be changed to reflect the root directory of the tree root. This
5318 * is kind of like crossing a mount point.
5319 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005320static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005321 struct inode *dir,
5322 struct dentry *dentry,
5323 struct btrfs_key *location,
5324 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005325{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005326 struct btrfs_path *path;
5327 struct btrfs_root *new_root;
5328 struct btrfs_root_ref *ref;
5329 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005330 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005331 int ret;
5332 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005333
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005334 path = btrfs_alloc_path();
5335 if (!path) {
5336 err = -ENOMEM;
5337 goto out;
5338 }
Chris Mason39279cc2007-06-12 06:35:45 -04005339
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005340 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005341 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5342 key.type = BTRFS_ROOT_REF_KEY;
5343 key.offset = location->objectid;
5344
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005345 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005346 if (ret) {
5347 if (ret < 0)
5348 err = ret;
5349 goto out;
5350 }
Chris Mason39279cc2007-06-12 06:35:45 -04005351
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005352 leaf = path->nodes[0];
5353 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005354 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005355 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5356 goto out;
5357
5358 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5359 (unsigned long)(ref + 1),
5360 dentry->d_name.len);
5361 if (ret)
5362 goto out;
5363
David Sterbab3b4aa72011-04-21 01:20:15 +02005364 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005365
David Sterba56e93572020-05-15 19:35:55 +02005366 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005367 if (IS_ERR(new_root)) {
5368 err = PTR_ERR(new_root);
5369 goto out;
5370 }
5371
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005372 *sub_root = new_root;
5373 location->objectid = btrfs_root_dirid(&new_root->root_item);
5374 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005375 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005376 err = 0;
5377out:
5378 btrfs_free_path(path);
5379 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005380}
5381
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005382static void inode_tree_add(struct inode *inode)
5383{
5384 struct btrfs_root *root = BTRFS_I(inode)->root;
5385 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005386 struct rb_node **p;
5387 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005388 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005389 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005390
Al Viro1d3382cb2010-10-23 15:19:20 -04005391 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005392 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005393 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005394 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005395 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005396 while (*p) {
5397 parent = *p;
5398 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5399
David Sterba37508512018-06-29 10:56:40 +02005400 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005401 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005402 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005403 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005404 else {
5405 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005406 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005407 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005408 RB_CLEAR_NODE(parent);
5409 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005410 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005411 }
5412 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005413 rb_link_node(new, parent, p);
5414 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005415 spin_unlock(&root->inode_lock);
5416}
5417
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005418static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005419{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005420 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005421 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005422
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005423 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005424 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5425 rb_erase(&inode->rb_node, &root->inode_tree);
5426 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005427 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005428 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005429 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005430
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005431 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005432 spin_lock(&root->inode_lock);
5433 empty = RB_EMPTY_ROOT(&root->inode_tree);
5434 spin_unlock(&root->inode_lock);
5435 if (empty)
5436 btrfs_add_dead_root(root);
5437 }
5438}
5439
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005440
Chris Masone02119d2008-09-05 16:13:11 -04005441static int btrfs_init_locked_inode(struct inode *inode, void *p)
5442{
5443 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005444
5445 inode->i_ino = args->ino;
5446 BTRFS_I(inode)->location.objectid = args->ino;
5447 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5448 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005449 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5450 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005451 return 0;
5452}
5453
5454static int btrfs_find_actor(struct inode *inode, void *opaque)
5455{
5456 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005457
5458 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005459 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005460}
5461
David Sterba0202e832020-05-15 19:35:59 +02005462static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005463 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005464{
5465 struct inode *inode;
5466 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005467 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005468
David Sterba0202e832020-05-15 19:35:59 +02005469 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005470 args.root = root;
5471
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005472 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005473 btrfs_init_locked_inode,
5474 (void *)&args);
5475 return inode;
5476}
5477
David Sterba4c66e0d2019-10-03 19:09:35 +02005478/*
David Sterba0202e832020-05-15 19:35:59 +02005479 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005480 * Path can be preallocated to prevent recursing back to iget through
5481 * allocator. NULL is also valid but may require an additional allocation
5482 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305483 */
David Sterba0202e832020-05-15 19:35:59 +02005484struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005485 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305486{
5487 struct inode *inode;
5488
David Sterba0202e832020-05-15 19:35:59 +02005489 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305490 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005491 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305492
5493 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005494 int ret;
5495
Filipe Manana4222ea72018-10-24 10:13:03 +01005496 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005497 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005498 inode_tree_add(inode);
5499 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005500 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005501 iget_failed(inode);
5502 /*
5503 * ret > 0 can come from btrfs_search_slot called by
5504 * btrfs_read_locked_inode, this means the inode item
5505 * was not found.
5506 */
5507 if (ret > 0)
5508 ret = -ENOENT;
5509 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005510 }
5511 }
5512
Balaji Rao1a54ef82008-07-21 02:01:04 +05305513 return inode;
5514}
5515
David Sterba0202e832020-05-15 19:35:59 +02005516struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005517{
David Sterba0202e832020-05-15 19:35:59 +02005518 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005519}
5520
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005521static struct inode *new_simple_dir(struct super_block *s,
5522 struct btrfs_key *key,
5523 struct btrfs_root *root)
5524{
5525 struct inode *inode = new_inode(s);
5526
5527 if (!inode)
5528 return ERR_PTR(-ENOMEM);
5529
Josef Bacik5c8fd992020-02-14 16:11:43 -05005530 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005531 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005532 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005533
5534 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005535 /*
5536 * We only need lookup, the rest is read-only and there's no inode
5537 * associated with the dentry
5538 */
5539 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005540 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005541 inode->i_fop = &simple_dir_operations;
5542 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005543 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305544 inode->i_atime = inode->i_mtime;
5545 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005546 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005547
5548 return inode;
5549}
5550
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005551static inline u8 btrfs_inode_type(struct inode *inode)
5552{
5553 /*
5554 * Compile-time asserts that generic FT_* types still match
5555 * BTRFS_FT_* types
5556 */
5557 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5558 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5559 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5560 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5561 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5562 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5563 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5564 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5565
5566 return fs_umode_to_ftype(inode->i_mode);
5567}
5568
Chris Mason3de45862008-11-17 21:02:50 -05005569struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005570{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005571 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005572 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005573 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005574 struct btrfs_root *sub_root = root;
5575 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005576 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005577 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005578
5579 if (dentry->d_name.len > BTRFS_NAME_LEN)
5580 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005581
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005582 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005583 if (ret < 0)
5584 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005585
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005586 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005587 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005588 if (IS_ERR(inode))
5589 return inode;
5590
5591 /* Do extra check against inode mode with di_type */
5592 if (btrfs_inode_type(inode) != di_type) {
5593 btrfs_crit(fs_info,
5594"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5595 inode->i_mode, btrfs_inode_type(inode),
5596 di_type);
5597 iput(inode);
5598 return ERR_PTR(-EUCLEAN);
5599 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005600 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005601 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005602
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005603 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005604 &location, &sub_root);
5605 if (ret < 0) {
5606 if (ret != -ENOENT)
5607 inode = ERR_PTR(ret);
5608 else
5609 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5610 } else {
David Sterba0202e832020-05-15 19:35:59 +02005611 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005612 }
Josef Bacik87270022020-01-24 09:32:31 -05005613 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005614 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005615
Julia Lawall34d19ba2011-01-24 19:55:19 +00005616 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005617 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005618 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005619 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005620 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005621 if (ret) {
5622 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005623 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005624 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005625 }
5626
Chris Mason3de45862008-11-17 21:02:50 -05005627 return inode;
5628}
5629
Nick Pigginfe15ce42011-01-07 17:49:23 +11005630static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005631{
5632 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005633 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005634
Li Zefan848cce02012-02-21 17:04:28 +08005635 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005636 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005637
Li Zefan848cce02012-02-21 17:04:28 +08005638 if (inode) {
5639 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005640 if (btrfs_root_refs(&root->root_item) == 0)
5641 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005642
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005643 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005644 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005645 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005646 return 0;
5647}
5648
Chris Mason3de45862008-11-17 21:02:50 -05005649static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005650 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005651{
Al Viro3837d202018-10-10 16:38:27 -04005652 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005653
Al Viro3837d202018-10-10 16:38:27 -04005654 if (inode == ERR_PTR(-ENOENT))
5655 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005656 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005657}
5658
Josef Bacik23b5ec72017-07-24 15:14:25 -04005659/*
5660 * All this infrastructure exists because dir_emit can fault, and we are holding
5661 * the tree lock when doing readdir. For now just allocate a buffer and copy
5662 * our information into that, and then dir_emit from the buffer. This is
5663 * similar to what NFS does, only we don't keep the buffer around in pagecache
5664 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5665 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5666 * tree lock.
5667 */
5668static int btrfs_opendir(struct inode *inode, struct file *file)
5669{
5670 struct btrfs_file_private *private;
5671
5672 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5673 if (!private)
5674 return -ENOMEM;
5675 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5676 if (!private->filldir_buf) {
5677 kfree(private);
5678 return -ENOMEM;
5679 }
5680 file->private_data = private;
5681 return 0;
5682}
5683
5684struct dir_entry {
5685 u64 ino;
5686 u64 offset;
5687 unsigned type;
5688 int name_len;
5689};
5690
5691static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5692{
5693 while (entries--) {
5694 struct dir_entry *entry = addr;
5695 char *name = (char *)(entry + 1);
5696
David Sterba92d32172018-04-16 21:10:14 +02005697 ctx->pos = get_unaligned(&entry->offset);
5698 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5699 get_unaligned(&entry->ino),
5700 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005701 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005702 addr += sizeof(struct dir_entry) +
5703 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005704 ctx->pos++;
5705 }
5706 return 0;
5707}
5708
Al Viro9cdda8d2013-05-22 16:48:09 -04005709static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005710{
Al Viro9cdda8d2013-05-22 16:48:09 -04005711 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005712 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005713 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005714 struct btrfs_dir_item *di;
5715 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005716 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005717 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005718 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005719 struct list_head ins_list;
5720 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005721 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005722 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005723 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005724 char *name_ptr;
5725 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005726 int entries = 0;
5727 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005728 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005729 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005730
Al Viro9cdda8d2013-05-22 16:48:09 -04005731 if (!dir_emit_dots(file, ctx))
5732 return 0;
5733
David Woodhouse49593bf2008-08-17 17:08:36 +01005734 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005735 if (!path)
5736 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005737
Josef Bacik23b5ec72017-07-24 15:14:25 -04005738 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005739 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005740
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005741 INIT_LIST_HEAD(&ins_list);
5742 INIT_LIST_HEAD(&del_list);
5743 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005744
Josef Bacik23b5ec72017-07-24 15:14:25 -04005745again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005746 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005747 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005748 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005749
Chris Mason39279cc2007-06-12 06:35:45 -04005750 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5751 if (ret < 0)
5752 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005753
5754 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005755 struct dir_entry *entry;
5756
Chris Mason5f39d392007-10-15 16:14:19 -04005757 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005758 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005759 if (slot >= btrfs_header_nritems(leaf)) {
5760 ret = btrfs_next_leaf(root, path);
5761 if (ret < 0)
5762 goto err;
5763 else if (ret > 0)
5764 break;
5765 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005766 }
Chris Mason3de45862008-11-17 21:02:50 -05005767
Chris Mason5f39d392007-10-15 16:14:19 -04005768 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5769
5770 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005771 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005772 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005773 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005774 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005775 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005776 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005777 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005778 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005779 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005780 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5781 PAGE_SIZE) {
5782 btrfs_release_path(path);
5783 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5784 if (ret)
5785 goto nopos;
5786 addr = private->filldir_buf;
5787 entries = 0;
5788 total_len = 0;
5789 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005790 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005791
5792 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005793 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005794 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005795 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5796 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005797 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005798 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005799 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005800 put_unaligned(location.objectid, &entry->ino);
5801 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005802 entries++;
5803 addr += sizeof(struct dir_entry) + name_len;
5804 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005805next:
5806 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005807 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005808 btrfs_release_path(path);
5809
5810 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5811 if (ret)
5812 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005813
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005814 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005815 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005816 goto nopos;
5817
Zach Browndb62efb2013-07-11 16:19:42 -07005818 /*
5819 * Stop new entries from being returned after we return the last
5820 * entry.
5821 *
5822 * New directory entries are assigned a strictly increasing
5823 * offset. This means that new entries created during readdir
5824 * are *guaranteed* to be seen in the future by that readdir.
5825 * This has broken buggy programs which operate on names as
5826 * they're returned by readdir. Until we re-use freed offsets
5827 * we have this hack to stop new entries from being returned
5828 * under the assumption that they'll never reach this huge
5829 * offset.
5830 *
5831 * This is being careful not to overflow 32bit loff_t unless the
5832 * last entry requires it because doing so has broken 32bit apps
5833 * in the past.
5834 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005835 if (ctx->pos >= INT_MAX)
5836 ctx->pos = LLONG_MAX;
5837 else
5838 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005839nopos:
5840 ret = 0;
5841err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005842 if (put)
5843 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005844 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005845 return ret;
5846}
5847
Chris Mason39279cc2007-06-12 06:35:45 -04005848/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005849 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005850 * inode changes. But, it is most likely to find the inode in cache.
5851 * FIXME, needs more benchmarking...there are no reasons other than performance
5852 * to keep or drop this code.
5853 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005854static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005855{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005856 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005857 struct btrfs_root *root = BTRFS_I(inode)->root;
5858 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005859 int ret;
5860
Josef Bacik72ac3c02012-05-23 14:13:11 -04005861 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005862 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005863
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005864 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005865 if (IS_ERR(trans))
5866 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005867
5868 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005869 if (ret && ret == -ENOSPC) {
5870 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005871 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005872 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005873 if (IS_ERR(trans))
5874 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005875
Chris Mason94b60442010-05-26 11:02:00 -04005876 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005877 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005878 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005879 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005880 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005881
5882 return ret;
5883}
5884
5885/*
5886 * This is a copy of file_update_time. We need this so we can return error on
5887 * ENOSPC for updating the inode in the case of file write and mmap writes.
5888 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005889static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005890 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005891{
Alexander Block2bc5565282012-06-15 09:49:33 +02005892 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005893 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005894
5895 if (btrfs_root_readonly(root))
5896 return -EROFS;
5897
Josef Bacike41f9412012-03-26 09:46:47 -04005898 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005899 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005900 if (flags & S_CTIME)
5901 inode->i_ctime = *now;
5902 if (flags & S_MTIME)
5903 inode->i_mtime = *now;
5904 if (flags & S_ATIME)
5905 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005906 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005907}
5908
Chris Masond352ac62008-09-29 15:18:18 -04005909/*
5910 * find the highest existing sequence number in a directory
5911 * and then set the in-memory index_cnt variable to reflect
5912 * free sequence numbers
5913 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005914static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005915{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005916 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005917 struct btrfs_key key, found_key;
5918 struct btrfs_path *path;
5919 struct extent_buffer *leaf;
5920 int ret;
5921
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005922 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005923 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005924 key.offset = (u64)-1;
5925
5926 path = btrfs_alloc_path();
5927 if (!path)
5928 return -ENOMEM;
5929
5930 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5931 if (ret < 0)
5932 goto out;
5933 /* FIXME: we should be able to handle this */
5934 if (ret == 0)
5935 goto out;
5936 ret = 0;
5937
5938 /*
5939 * MAGIC NUMBER EXPLANATION:
5940 * since we search a directory based on f_pos we have to start at 2
5941 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5942 * else has to start at 2
5943 */
5944 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005945 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005946 goto out;
5947 }
5948
5949 path->slots[0]--;
5950
5951 leaf = path->nodes[0];
5952 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5953
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005954 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005955 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005956 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005957 goto out;
5958 }
5959
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005960 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005961out:
5962 btrfs_free_path(path);
5963 return ret;
5964}
5965
Chris Masond352ac62008-09-29 15:18:18 -04005966/*
5967 * helper to find a free sequence number in a given directory. This current
5968 * code is very simple, later versions will do smarter things in the btree
5969 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005970int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005971{
5972 int ret = 0;
5973
Nikolay Borisov877574e2017-02-20 13:50:33 +02005974 if (dir->index_cnt == (u64)-1) {
5975 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005976 if (ret) {
5977 ret = btrfs_set_inode_index_count(dir);
5978 if (ret)
5979 return ret;
5980 }
Josef Bacikaec74772008-07-24 12:12:38 -04005981 }
5982
Nikolay Borisov877574e2017-02-20 13:50:33 +02005983 *index = dir->index_cnt;
5984 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005985
5986 return ret;
5987}
5988
Chris Masonb0d5d102014-09-08 13:08:51 -07005989static int btrfs_insert_inode_locked(struct inode *inode)
5990{
5991 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005992
5993 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07005994 args.root = BTRFS_I(inode)->root;
5995
5996 return insert_inode_locked4(inode,
5997 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5998 btrfs_find_actor, &args);
5999}
6000
Anand Jain19aee8d2017-07-18 17:37:05 +08006001/*
6002 * Inherit flags from the parent inode.
6003 *
6004 * Currently only the compression flags and the cow flags are inherited.
6005 */
6006static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6007{
6008 unsigned int flags;
6009
6010 if (!dir)
6011 return;
6012
6013 flags = BTRFS_I(dir)->flags;
6014
6015 if (flags & BTRFS_INODE_NOCOMPRESS) {
6016 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6017 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6018 } else if (flags & BTRFS_INODE_COMPRESS) {
6019 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6020 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6021 }
6022
6023 if (flags & BTRFS_INODE_NODATACOW) {
6024 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6025 if (S_ISREG(inode->i_mode))
6026 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6027 }
6028
David Sterba7b6a2212018-03-26 18:40:21 +02006029 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006030}
6031
Chris Mason39279cc2007-06-12 06:35:45 -04006032static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6033 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006034 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006035 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006036 u64 ref_objectid, u64 objectid,
6037 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006038{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006039 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006040 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006041 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006042 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006043 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006044 struct btrfs_inode_ref *ref;
6045 struct btrfs_key key[2];
6046 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006047 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006048 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006049 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006050 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006051
Chris Mason5f39d392007-10-15 16:14:19 -04006052 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006053 if (!path)
6054 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006055
Josef Bacik11a19a92019-09-09 10:12:04 -04006056 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006057 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006058 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006059 if (!inode) {
6060 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006061 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006062 }
Chris Mason39279cc2007-06-12 06:35:45 -04006063
Li Zefan581bb052011-04-20 10:06:11 +08006064 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006065 * O_TMPFILE, set link count to 0, so that after this point,
6066 * we fill in an inode item with the correct link count.
6067 */
6068 if (!name)
6069 set_nlink(inode, 0);
6070
6071 /*
Li Zefan581bb052011-04-20 10:06:11 +08006072 * we have to initialize this early, so we can reclaim the inode
6073 * number if we fail afterwards in this function.
6074 */
6075 inode->i_ino = objectid;
6076
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006077 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006078 trace_btrfs_inode_request(dir);
6079
Nikolay Borisov877574e2017-02-20 13:50:33 +02006080 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006081 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006082 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006083 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006084 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006085 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006086 } else if (dir) {
6087 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006088 }
6089 /*
6090 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006091 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006092 * number
6093 */
6094 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006095 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006096 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006097 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006098 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006099
Josef Bacik5dc562c2012-08-17 13:14:17 -04006100 /*
6101 * We could have gotten an inode number from somebody who was fsynced
6102 * and then removed in this same transaction, so let's just set full
6103 * sync since it will be a full sync anyway and this will blow away the
6104 * old info in the log.
6105 */
6106 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6107
Chris Mason9c583092008-01-29 15:15:18 -05006108 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006109 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006110 key[0].offset = 0;
6111
Chris Mason9c583092008-01-29 15:15:18 -05006112 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006113
6114 if (name) {
6115 /*
6116 * Start new inodes with an inode_ref. This is slightly more
6117 * efficient for small numbers of hard links since they will
6118 * be packed into one item. Extended refs will kick in if we
6119 * add more hard links than can fit in the ref item.
6120 */
6121 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006122 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006123 key[1].offset = ref_objectid;
6124
6125 sizes[1] = name_len + sizeof(*ref);
6126 }
Chris Mason9c583092008-01-29 15:15:18 -05006127
Chris Masonb0d5d102014-09-08 13:08:51 -07006128 location = &BTRFS_I(inode)->location;
6129 location->objectid = objectid;
6130 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006131 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006132
6133 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006134 if (ret < 0) {
6135 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006136 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006137 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006138
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006139 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006140 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006141 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006142
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006143 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006144 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306145
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006146 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306147 inode->i_atime = inode->i_mtime;
6148 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006149 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306150
Chris Mason5f39d392007-10-15 16:14:19 -04006151 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6152 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006153 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006154 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006155 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006156
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006157 if (name) {
6158 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6159 struct btrfs_inode_ref);
6160 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6161 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6162 ptr = (unsigned long)(ref + 1);
6163 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6164 }
Chris Mason9c583092008-01-29 15:15:18 -05006165
Chris Mason5f39d392007-10-15 16:14:19 -04006166 btrfs_mark_buffer_dirty(path->nodes[0]);
6167 btrfs_free_path(path);
6168
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006169 btrfs_inherit_iflags(inode, dir);
6170
Al Viro569254b2011-07-24 17:08:40 -04006171 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006172 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006173 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006174 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006175 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6176 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006177 }
6178
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006179 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006180
6181 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006182 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006183
Alexander Block8ea05e32012-07-25 17:35:53 +02006184 btrfs_update_root_times(trans, root);
6185
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006186 ret = btrfs_inode_inherit_props(trans, inode, dir);
6187 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006188 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006189 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006190 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006191
Chris Mason39279cc2007-06-12 06:35:45 -04006192 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006193
6194fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006195 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006196fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006197 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006198 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006199 btrfs_free_path(path);
6200 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006201}
6202
Chris Masond352ac62008-09-29 15:18:18 -04006203/*
6204 * utility function to add 'inode' into 'parent_inode' with
6205 * a give name and a given sequence number.
6206 * if 'add_backref' is true, also insert a backref from the
6207 * inode to the parent directory.
6208 */
Chris Masone02119d2008-09-05 16:13:11 -04006209int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006210 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006211 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006212{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006213 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006214 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006215 struct btrfs_root *root = parent_inode->root;
6216 u64 ino = btrfs_ino(inode);
6217 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006218
Li Zefan33345d012011-04-20 10:31:50 +08006219 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006220 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006221 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006222 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006223 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006224 key.offset = 0;
6225 }
Chris Mason39279cc2007-06-12 06:35:45 -04006226
Li Zefan33345d012011-04-20 10:31:50 +08006227 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006228 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006229 root->root_key.objectid, parent_ino,
6230 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006231 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006232 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6233 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006234 }
6235
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006236 /* Nothing to clean up yet */
6237 if (ret)
6238 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006239
Lu Fengqi684572d2018-08-04 21:10:57 +08006240 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006241 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006242 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006243 goto fail_dir_item;
6244 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006245 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006246 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006247 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006248
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006249 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006250 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006251 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006252 /*
6253 * If we are replaying a log tree, we do not want to update the mtime
6254 * and ctime of the parent directory with the current time, since the
6255 * log replay procedure is responsible for setting them to their correct
6256 * values (the ones it had when the fsync was done).
6257 */
6258 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6259 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6260
6261 parent_inode->vfs_inode.i_mtime = now;
6262 parent_inode->vfs_inode.i_ctime = now;
6263 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006264 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006265 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006266 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006267 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006268
6269fail_dir_item:
6270 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6271 u64 local_index;
6272 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006273 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006274 root->root_key.objectid, parent_ino,
6275 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006276 if (err)
6277 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006278 } else if (add_backref) {
6279 u64 local_index;
6280 int err;
6281
6282 err = btrfs_del_inode_ref(trans, root, name, name_len,
6283 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006284 if (err)
6285 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006286 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006287
6288 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006289 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006290}
6291
6292static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006293 struct btrfs_inode *dir, struct dentry *dentry,
6294 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006295{
Josef Bacika1b075d2010-11-19 20:36:11 +00006296 int err = btrfs_add_link(trans, dir, inode,
6297 dentry->d_name.name, dentry->d_name.len,
6298 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006299 if (err > 0)
6300 err = -EEXIST;
6301 return err;
6302}
6303
Josef Bacik618e21d2007-07-11 10:18:17 -04006304static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006305 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006306{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006307 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006308 struct btrfs_trans_handle *trans;
6309 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006310 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006311 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006312 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006313 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006314
Josef Bacik9ed74f22009-09-11 16:12:44 -04006315 /*
6316 * 2 for inode item and ref
6317 * 2 for dir items
6318 * 1 for xattr if selinux is on
6319 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006320 trans = btrfs_start_transaction(root, 5);
6321 if (IS_ERR(trans))
6322 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006323
Li Zefan581bb052011-04-20 10:06:11 +08006324 err = btrfs_find_free_ino(root, &objectid);
6325 if (err)
6326 goto out_unlock;
6327
Josef Bacikaec74772008-07-24 12:12:38 -04006328 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006329 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6330 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006331 if (IS_ERR(inode)) {
6332 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006333 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006334 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006335 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006336
Casey Schauflerad19db72011-12-15 10:09:07 -05006337 /*
6338 * If the active LSM wants to access the inode during
6339 * d_instantiate it needs these. Smack checks to see
6340 * if the filesystem supports xattrs by looking at the
6341 * ops vector.
6342 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006343 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006344 init_special_inode(inode, inode->i_mode, rdev);
6345
6346 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006347 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006348 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006349
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006350 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6351 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006352 if (err)
6353 goto out_unlock;
6354
6355 btrfs_update_inode(trans, root, inode);
6356 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006357
Josef Bacik618e21d2007-07-11 10:18:17 -04006358out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006359 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006360 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006361 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006362 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006363 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006364 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006365 return err;
6366}
6367
Chris Mason39279cc2007-06-12 06:35:45 -04006368static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006369 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006370{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006371 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006372 struct btrfs_trans_handle *trans;
6373 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006374 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006375 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006376 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006377 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006378
Josef Bacik9ed74f22009-09-11 16:12:44 -04006379 /*
6380 * 2 for inode item and ref
6381 * 2 for dir items
6382 * 1 for xattr if selinux is on
6383 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006384 trans = btrfs_start_transaction(root, 5);
6385 if (IS_ERR(trans))
6386 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006387
Li Zefan581bb052011-04-20 10:06:11 +08006388 err = btrfs_find_free_ino(root, &objectid);
6389 if (err)
6390 goto out_unlock;
6391
Josef Bacikaec74772008-07-24 12:12:38 -04006392 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006393 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6394 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006395 if (IS_ERR(inode)) {
6396 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006397 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006398 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006399 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006400 /*
6401 * If the active LSM wants to access the inode during
6402 * d_instantiate it needs these. Smack checks to see
6403 * if the filesystem supports xattrs by looking at the
6404 * ops vector.
6405 */
6406 inode->i_fop = &btrfs_file_operations;
6407 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006408 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006409
6410 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6411 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006412 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006413
6414 err = btrfs_update_inode(trans, root, inode);
6415 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006416 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006417
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006418 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6419 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006420 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006421 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006422
Al Viro1e2e5472018-05-04 08:23:01 -04006423 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006424
Chris Mason39279cc2007-06-12 06:35:45 -04006425out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006426 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006427 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006428 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006429 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006430 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006431 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006432 return err;
6433}
6434
6435static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6436 struct dentry *dentry)
6437{
Filipe Manana271dba452016-01-05 16:24:05 +00006438 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006439 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006440 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006441 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006442 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006443 int err;
6444 int drop_inode = 0;
6445
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006446 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006447 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006448 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006449
Mark Fashehf1863732012-08-08 11:32:27 -07006450 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006451 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006452
Nikolay Borisov877574e2017-02-20 13:50:33 +02006453 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006454 if (err)
6455 goto fail;
6456
Yan, Zhenga22285a2010-05-16 10:48:46 -04006457 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006458 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006459 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006460 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006461 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006462 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006463 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006464 if (IS_ERR(trans)) {
6465 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006466 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006467 goto fail;
6468 }
Chris Mason5f39d392007-10-15 16:14:19 -04006469
Miao Xie67de1172013-12-26 13:07:06 +08006470 /* There are several dir indexes for this inode, clear the cache. */
6471 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006472 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006473 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006474 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006475 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006476 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006477
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006478 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6479 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006480
Yan, Zhenga5719522009-09-24 09:17:31 -04006481 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006482 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006483 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006484 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006485
Yan, Zhenga5719522009-09-24 09:17:31 -04006486 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006487 if (err)
6488 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006489 if (inode->i_nlink == 1) {
6490 /*
6491 * If new hard link count is 1, it's a file created
6492 * with open(2) O_TMPFILE flag.
6493 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006494 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006495 if (err)
6496 goto fail;
6497 }
Al Viro08c422c2011-12-23 07:58:13 -05006498 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006499 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006500 }
Chris Mason39279cc2007-06-12 06:35:45 -04006501
Chris Mason1832a6d2007-12-21 16:27:21 -05006502fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006503 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006504 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006505 if (drop_inode) {
6506 inode_dec_link_count(inode);
6507 iput(inode);
6508 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006509 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006510 return err;
6511}
6512
Al Viro18bb1db2011-07-26 01:41:39 -04006513static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006514{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006515 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006516 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006517 struct btrfs_trans_handle *trans;
6518 struct btrfs_root *root = BTRFS_I(dir)->root;
6519 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006520 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006521 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006522
Josef Bacik9ed74f22009-09-11 16:12:44 -04006523 /*
6524 * 2 items for inode and ref
6525 * 2 items for dir items
6526 * 1 for xattr if selinux is on
6527 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006528 trans = btrfs_start_transaction(root, 5);
6529 if (IS_ERR(trans))
6530 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006531
Li Zefan581bb052011-04-20 10:06:11 +08006532 err = btrfs_find_free_ino(root, &objectid);
6533 if (err)
6534 goto out_fail;
6535
Josef Bacikaec74772008-07-24 12:12:38 -04006536 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006537 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6538 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006539 if (IS_ERR(inode)) {
6540 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006541 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006542 goto out_fail;
6543 }
Chris Mason5f39d392007-10-15 16:14:19 -04006544
Chris Masonb0d5d102014-09-08 13:08:51 -07006545 /* these must be set before we unlock the inode */
6546 inode->i_op = &btrfs_dir_inode_operations;
6547 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006548
Eric Paris2a7dba32011-02-01 11:05:39 -05006549 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006550 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006551 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006552
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006553 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006554 err = btrfs_update_inode(trans, root, inode);
6555 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006556 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006557
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006558 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6559 dentry->d_name.name,
6560 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006561 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006562 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006563
Al Viro1e2e5472018-05-04 08:23:01 -04006564 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006565
6566out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006567 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006568 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006569 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006570 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006571 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006572 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006573 return err;
6574}
6575
Chris Masonc8b97812008-10-29 14:49:59 -04006576static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006577 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006578 size_t pg_offset, u64 extent_offset,
6579 struct btrfs_file_extent_item *item)
6580{
6581 int ret;
6582 struct extent_buffer *leaf = path->nodes[0];
6583 char *tmp;
6584 size_t max_size;
6585 unsigned long inline_size;
6586 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006587 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006588
6589 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006590 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006591 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6592 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006593 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006594 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006595 if (!tmp)
6596 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006597 ptr = btrfs_file_extent_inline_start(item);
6598
6599 read_extent_buffer(leaf, tmp, ptr, inline_size);
6600
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006601 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006602 ret = btrfs_decompress(compress_type, tmp, page,
6603 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006604
6605 /*
6606 * decompression code contains a memset to fill in any space between the end
6607 * of the uncompressed data and the end of max_size in case the decompressed
6608 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6609 * the end of an inline extent and the beginning of the next block, so we
6610 * cover that region here.
6611 */
6612
6613 if (max_size + pg_offset < PAGE_SIZE) {
6614 char *map = kmap(page);
6615 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6616 kunmap(page);
6617 }
Chris Masonc8b97812008-10-29 14:49:59 -04006618 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006619 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006620}
6621
Omar Sandoval39b07b52019-12-02 17:34:23 -08006622/**
6623 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6624 * @inode: file to search in
6625 * @page: page to read extent data into if the extent is inline
6626 * @pg_offset: offset into @page to copy to
6627 * @start: file offset
6628 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006629 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006630 * This returns the first &struct extent_map which overlaps with the given
6631 * range, reading it from the B-tree and caching it if necessary. Note that
6632 * there may be more extents which overlap the given range after the returned
6633 * extent_map.
6634 *
6635 * If @page is not NULL and the extent is inline, this also reads the extent
6636 * data directly into the page and marks the extent up to date in the io_tree.
6637 *
6638 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006639 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006640struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006641 struct page *page, size_t pg_offset,
6642 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006643{
David Sterba3ffbd682018-06-29 10:56:42 +02006644 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006645 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006646 u64 extent_start = 0;
6647 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006648 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006649 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006650 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006651 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006652 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006653 struct extent_buffer *leaf;
6654 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006655 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006656 struct extent_map_tree *em_tree = &inode->extent_tree;
6657 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006658
Chris Mason890871b2009-09-02 16:24:52 -04006659 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006660 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006661 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006662
Chris Masona52d9a82007-08-27 16:49:44 -04006663 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006664 if (em->start > start || em->start + em->len <= start)
6665 free_extent_map(em);
6666 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006667 free_extent_map(em);
6668 else
6669 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006670 }
David Sterba172ddd62011-04-21 00:48:27 +02006671 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006672 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006673 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05006674 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006675 }
Chris Masond1310b22008-01-24 16:13:08 -05006676 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006677 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006678 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006679 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006680
Liu Bobee6ec82018-08-17 05:05:28 +08006681 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006682 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006683 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08006684 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006685 }
6686
Liu Bobee6ec82018-08-17 05:05:28 +08006687 /* Chances are we'll be called again, so go ahead and do readahead */
6688 path->reada = READA_FORWARD;
Josef Bacik4d7240f2020-10-23 09:58:09 -04006689
6690 /*
6691 * The same explanation in load_free_space_cache applies here as well,
6692 * we only read when we're loading the free space cache, and at that
6693 * point the commit_root has everything we need.
6694 */
6695 if (btrfs_is_free_space_inode(inode)) {
6696 path->search_commit_root = 1;
6697 path->skip_locking = 1;
6698 }
Josef Bacik51899412020-08-20 11:46:01 -04006699
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006700 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006701 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006702 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006703 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006704 if (path->slots[0] == 0)
6705 goto not_found;
6706 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006707 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006708 }
6709
Chris Mason5f39d392007-10-15 16:14:19 -04006710 leaf = path->nodes[0];
6711 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006712 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006713 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006714 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006715 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006716 /*
6717 * If we backup past the first extent we want to move forward
6718 * and see if there is an extent in front of us, otherwise we'll
6719 * say there is a hole for our whole search range which can
6720 * cause problems.
6721 */
6722 extent_end = start;
6723 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006724 }
6725
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006726 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006727 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006728 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006729 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6730 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006731 /* Only regular file could have regular/prealloc extent */
6732 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006733 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006734 btrfs_crit(fs_info,
6735 "regular/prealloc extent found for non-regular inode %llu",
6736 btrfs_ino(inode));
6737 goto out;
6738 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006739 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6740 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006741 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006742 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6743 path->slots[0],
6744 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006745 }
Josef Bacik25a50342013-10-14 12:08:38 -04006746next:
Yan Zheng9036c102008-10-30 14:19:41 -04006747 if (start >= extent_end) {
6748 path->slots[0]++;
6749 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6750 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006751 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006752 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006753 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006754 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006755
Yan Zheng9036c102008-10-30 14:19:41 -04006756 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006757 }
Yan Zheng9036c102008-10-30 14:19:41 -04006758 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6759 if (found_key.objectid != objectid ||
6760 found_key.type != BTRFS_EXTENT_DATA_KEY)
6761 goto not_found;
6762 if (start + len <= found_key.offset)
6763 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006764 if (start > found_key.offset)
6765 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006766
6767 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006768 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006769 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006770 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006771 em->block_start = EXTENT_MAP_HOLE;
6772 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006773 }
6774
Omar Sandoval39b07b52019-12-02 17:34:23 -08006775 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006776
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006777 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6778 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006779 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006780 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006781 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006782 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006783 size_t size;
6784 size_t extent_offset;
6785 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006786
Omar Sandoval39b07b52019-12-02 17:34:23 -08006787 if (!page)
Yan689f9342007-10-29 11:41:07 -04006788 goto out;
Yan689f9342007-10-29 11:41:07 -04006789
Qu Wenruoe41ca582018-06-06 15:41:49 +08006790 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006791 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006792 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6793 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006794 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006795 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006796 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006797 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006798 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006799
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006800 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006801 if (btrfs_file_extent_compression(leaf, item) !=
6802 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006803 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006804 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006805 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04006806 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04006807 } else {
6808 map = kmap(page);
6809 read_extent_buffer(leaf, map + pg_offset, ptr,
6810 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006811 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006812 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006813 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006814 copy_size);
6815 }
Chris Masonc8b97812008-10-29 14:49:59 -04006816 kunmap(page);
6817 }
Chris Mason179e29e2007-11-01 11:28:41 -04006818 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006819 }
Chris Masond1310b22008-01-24 16:13:08 -05006820 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006821 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006822 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006823 }
6824not_found:
6825 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006826 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006827 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006828 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006829insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006830 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02006831 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006832 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006833 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006834 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6835 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006836 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04006837 goto out;
6838 }
Chris Masond1310b22008-01-24 16:13:08 -05006839
Chris Mason890871b2009-09-02 16:24:52 -04006840 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006841 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006842 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006843out:
Liu Boc6414282018-08-23 07:36:17 +08006844 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006845
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006846 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006847
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006848 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04006849 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006850 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04006851 }
6852 return em;
6853}
6854
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006855struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006856 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006857{
6858 struct extent_map *em;
6859 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006860 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006861 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006862 u64 delalloc_len;
6863 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006864 int err = 0;
6865
Omar Sandoval39b07b52019-12-02 17:34:23 -08006866 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006867 if (IS_ERR(em))
6868 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006869 /*
6870 * If our em maps to:
6871 * - a hole or
6872 * - a pre-alloc extent,
6873 * there might actually be delalloc bytes behind it.
6874 */
6875 if (em->block_start != EXTENT_MAP_HOLE &&
6876 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6877 return em;
6878 else
6879 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006880
6881 /* check to see if we've wrapped (len == -1 or similar) */
6882 end = start + len;
6883 if (end < start)
6884 end = (u64)-1;
6885 else
6886 end -= 1;
6887
6888 em = NULL;
6889
6890 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006891 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006892 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006893 delalloc_end = delalloc_start + delalloc_len;
6894 if (delalloc_end < delalloc_start)
6895 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006896
6897 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006898 * We didn't find anything useful, return the original results from
6899 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006900 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006901 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006902 em = hole_em;
6903 hole_em = NULL;
6904 goto out;
6905 }
6906
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006907 /*
6908 * Adjust the delalloc_start to make sure it doesn't go backwards from
6909 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006910 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006911 delalloc_start = max(start, delalloc_start);
6912 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006913
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006914 if (delalloc_len > 0) {
6915 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006916 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006917 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006918
David Sterba172ddd62011-04-21 00:48:27 +02006919 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006920 if (!em) {
6921 err = -ENOMEM;
6922 goto out;
6923 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006924
6925 ASSERT(hole_em);
6926 /*
6927 * When btrfs_get_extent can't find anything it returns one
6928 * huge hole
6929 *
6930 * Make sure what it found really fits our range, and adjust to
6931 * make sure it is based on the start from the caller
6932 */
6933 if (hole_end <= start || hole_em->start > end) {
6934 free_extent_map(hole_em);
6935 hole_em = NULL;
6936 } else {
6937 hole_start = max(hole_em->start, start);
6938 hole_len = hole_end - hole_start;
6939 }
6940
6941 if (hole_em && delalloc_start > hole_start) {
6942 /*
6943 * Our hole starts before our delalloc, so we have to
6944 * return just the parts of the hole that go until the
6945 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006946 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006947 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006948 em->start = hole_start;
6949 em->orig_start = hole_start;
6950 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006951 * Don't adjust block start at all, it is fixed at
6952 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006953 */
6954 em->block_start = hole_em->block_start;
6955 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006956 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6957 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006958 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006959 /*
6960 * Hole is out of passed range or it starts after
6961 * delalloc range
6962 */
6963 em->start = delalloc_start;
6964 em->len = delalloc_len;
6965 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006966 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006967 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006968 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006969 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006970 return hole_em;
6971 }
6972out:
6973
6974 free_extent_map(hole_em);
6975 if (err) {
6976 free_extent_map(em);
6977 return ERR_PTR(err);
6978 }
6979 return em;
6980}
6981
Nikolay Borisov64f54182020-06-03 08:55:31 +03006982static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006983 const u64 start,
6984 const u64 len,
6985 const u64 orig_start,
6986 const u64 block_start,
6987 const u64 block_len,
6988 const u64 orig_block_len,
6989 const u64 ram_bytes,
6990 const int type)
6991{
6992 struct extent_map *em = NULL;
6993 int ret;
6994
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006995 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03006996 em = create_io_em(inode, start, len, orig_start, block_start,
6997 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08006998 BTRFS_COMPRESS_NONE, /* compress_type */
6999 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007000 if (IS_ERR(em))
7001 goto out;
7002 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03007003 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7004 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007005 if (ret) {
7006 if (em) {
7007 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03007008 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007009 }
7010 em = ERR_PTR(ret);
7011 }
7012 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007013
7014 return em;
7015}
7016
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007017static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007018 u64 start, u64 len)
7019{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007020 struct btrfs_root *root = inode->root;
7021 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04007022 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007023 struct btrfs_key ins;
7024 u64 alloc_hint;
7025 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007026
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007027 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007028 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007029 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007030 if (ret)
7031 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007032
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007033 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007034 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007035 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007036 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007037 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007038 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7039 1);
Josef Bacik00361582013-08-14 14:02:47 -04007040
Josef Bacik4b46fce2010-05-23 11:00:55 -04007041 return em;
7042}
7043
Chris Mason46bfbb52010-05-26 11:04:10 -04007044/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007045 * Check if we can do nocow write into the range [@offset, @offset + @len)
7046 *
7047 * @offset: File offset
7048 * @len: The length to write, will be updated to the nocow writeable
7049 * range
7050 * @orig_start: (optional) Return the original file offset of the file extent
7051 * @orig_len: (optional) Return the original on-disk length of the file extent
7052 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007053 * @strict: if true, omit optimizations that might force us into unnecessary
7054 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007055 *
7056 * This function will flush ordered extents in the range to ensure proper
7057 * nocow checks for (nowait == false) case.
7058 *
7059 * Return:
7060 * >0 and update @len if we can do nocow write
7061 * 0 if we can't do nocow write
7062 * <0 if error happened
7063 *
7064 * NOTE: This only checks the file extents, caller is responsible to wait for
7065 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007066 */
Josef Bacik00361582013-08-14 14:02:47 -04007067noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007068 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007069 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007070{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007071 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007072 struct btrfs_path *path;
7073 int ret;
7074 struct extent_buffer *leaf;
7075 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007076 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007077 struct btrfs_file_extent_item *fi;
7078 struct btrfs_key key;
7079 u64 disk_bytenr;
7080 u64 backref_offset;
7081 u64 extent_end;
7082 u64 num_bytes;
7083 int slot;
7084 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007085 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007086
Chris Mason46bfbb52010-05-26 11:04:10 -04007087 path = btrfs_alloc_path();
7088 if (!path)
7089 return -ENOMEM;
7090
David Sterbaf85b7372017-01-20 14:54:07 +01007091 ret = btrfs_lookup_file_extent(NULL, root, path,
7092 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007093 if (ret < 0)
7094 goto out;
7095
7096 slot = path->slots[0];
7097 if (ret == 1) {
7098 if (slot == 0) {
7099 /* can't find the item, must cow */
7100 ret = 0;
7101 goto out;
7102 }
7103 slot--;
7104 }
7105 ret = 0;
7106 leaf = path->nodes[0];
7107 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007108 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007109 key.type != BTRFS_EXTENT_DATA_KEY) {
7110 /* not our file or wrong item type, must cow */
7111 goto out;
7112 }
7113
7114 if (key.offset > offset) {
7115 /* Wrong offset, must cow */
7116 goto out;
7117 }
7118
7119 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7120 found_type = btrfs_file_extent_type(leaf, fi);
7121 if (found_type != BTRFS_FILE_EXTENT_REG &&
7122 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7123 /* not a regular extent, must cow */
7124 goto out;
7125 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007126
7127 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7128 goto out;
7129
Miao Xiee77751a2013-12-27 21:11:50 +08007130 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7131 if (extent_end <= offset)
7132 goto out;
7133
Chris Mason46bfbb52010-05-26 11:04:10 -04007134 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007135 if (disk_bytenr == 0)
7136 goto out;
7137
7138 if (btrfs_file_extent_compression(leaf, fi) ||
7139 btrfs_file_extent_encryption(leaf, fi) ||
7140 btrfs_file_extent_other_encoding(leaf, fi))
7141 goto out;
7142
Ethan Lien78d42952018-05-17 14:58:29 +08007143 /*
7144 * Do the same check as in btrfs_cross_ref_exist but without the
7145 * unnecessary search.
7146 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007147 if (!strict &&
7148 (btrfs_file_extent_generation(leaf, fi) <=
7149 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007150 goto out;
7151
Chris Mason46bfbb52010-05-26 11:04:10 -04007152 backref_offset = btrfs_file_extent_offset(leaf, fi);
7153
Josef Bacik7ee9e442013-06-21 16:37:03 -04007154 if (orig_start) {
7155 *orig_start = key.offset - backref_offset;
7156 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7157 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7158 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007159
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007160 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007161 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007162
7163 num_bytes = min(offset + *len, extent_end) - offset;
7164 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7165 u64 range_end;
7166
Jeff Mahoneyda170662016-06-15 09:22:56 -04007167 range_end = round_up(offset + num_bytes,
7168 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007169 ret = test_range_bit(io_tree, offset, range_end,
7170 EXTENT_DELALLOC, 0, NULL);
7171 if (ret) {
7172 ret = -EAGAIN;
7173 goto out;
7174 }
7175 }
7176
Josef Bacik1bda19e2013-10-18 12:10:36 -04007177 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007178
7179 /*
7180 * look for other files referencing this extent, if we
7181 * find any we must cow
7182 */
Josef Bacik00361582013-08-14 14:02:47 -04007183
Liu Boe4c3b2d2017-01-30 12:25:28 -08007184 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007185 key.offset - backref_offset, disk_bytenr,
7186 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007187 if (ret) {
7188 ret = 0;
7189 goto out;
7190 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007191
7192 /*
7193 * adjust disk_bytenr and num_bytes to cover just the bytes
7194 * in this extent we are about to write. If there
7195 * are any csums in that range we have to cow in order
7196 * to keep the csums correct
7197 */
7198 disk_bytenr += backref_offset;
7199 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007200 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7201 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007202 /*
7203 * all of the above have passed, it is safe to overwrite this extent
7204 * without cow
7205 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007206 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007207 ret = 1;
7208out:
7209 btrfs_free_path(path);
7210 return ret;
7211}
7212
Josef Bacikeb838e72012-07-31 16:28:48 -04007213static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007214 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007215{
7216 struct btrfs_ordered_extent *ordered;
7217 int ret = 0;
7218
7219 while (1) {
7220 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007221 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007222 /*
7223 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007224 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007225 * extents in this range.
7226 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007227 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007228 lockend - lockstart + 1);
7229
7230 /*
7231 * We need to make sure there are no buffered pages in this
7232 * range either, we could have raced between the invalidate in
7233 * generic_file_direct_write and locking the extent. The
7234 * invalidate needs to happen so that reads after a write do not
7235 * get stale data.
7236 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007237 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007238 (!writing || !filemap_range_has_page(inode->i_mapping,
7239 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007240 break;
7241
7242 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007243 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007244
7245 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007246 /*
7247 * If we are doing a DIO read and the ordered extent we
7248 * found is for a buffered write, we can not wait for it
7249 * to complete and retry, because if we do so we can
7250 * deadlock with concurrent buffered writes on page
7251 * locks. This happens only if our DIO read covers more
7252 * than one extent map, if at this point has already
7253 * created an ordered extent for a previous extent map
7254 * and locked its range in the inode's io tree, and a
7255 * concurrent write against that previous extent map's
7256 * range and this range started (we unlock the ranges
7257 * in the io tree only when the bios complete and
7258 * buffered writes always lock pages before attempting
7259 * to lock range in the io tree).
7260 */
7261 if (writing ||
7262 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007263 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007264 else
7265 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007266 btrfs_put_ordered_extent(ordered);
7267 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007268 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007269 * We could trigger writeback for this range (and wait
7270 * for it to complete) and then invalidate the pages for
7271 * this range (through invalidate_inode_pages2_range()),
7272 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007273 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007274 * triggered a readahead) on a page lock due to an
7275 * ordered dio extent we created before but did not have
7276 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007277 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007278 * ordered extent to complete while holding a lock on
7279 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007280 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007281 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007282 }
7283
Filipe Mananaade77022016-02-18 14:28:55 +00007284 if (ret)
7285 break;
7286
Josef Bacikeb838e72012-07-31 16:28:48 -04007287 cond_resched();
7288 }
7289
7290 return ret;
7291}
7292
Liu Bo6f9994d2017-01-31 07:50:22 -08007293/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007294static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7295 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007296 u64 block_len, u64 orig_block_len,
7297 u64 ram_bytes, int compress_type,
7298 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007299{
7300 struct extent_map_tree *em_tree;
7301 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007302 int ret;
7303
Liu Bo6f9994d2017-01-31 07:50:22 -08007304 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7305 type == BTRFS_ORDERED_COMPRESSED ||
7306 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007307 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007308
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007309 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007310 em = alloc_extent_map();
7311 if (!em)
7312 return ERR_PTR(-ENOMEM);
7313
7314 em->start = start;
7315 em->orig_start = orig_start;
7316 em->len = len;
7317 em->block_len = block_len;
7318 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007319 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007320 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007321 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007322 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007323 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007324 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007325 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007326 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7327 em->compress_type = compress_type;
7328 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007329
7330 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007331 btrfs_drop_extent_cache(inode, em->start,
7332 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007333 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007334 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007335 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007336 /*
7337 * The caller has taken lock_extent(), who could race with us
7338 * to add em?
7339 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007340 } while (ret == -EEXIST);
7341
7342 if (ret) {
7343 free_extent_map(em);
7344 return ERR_PTR(ret);
7345 }
7346
Liu Bo6f9994d2017-01-31 07:50:22 -08007347 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007348 return em;
7349}
7350
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007351
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007352static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007353 struct inode *inode,
7354 struct btrfs_dio_data *dio_data,
7355 u64 start, u64 len)
7356{
7357 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7358 struct extent_map *em = *map;
7359 int ret = 0;
7360
7361 /*
7362 * We don't allocate a new extent in the following cases
7363 *
7364 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7365 * existing extent.
7366 * 2) The extent is marked as PREALLOC. We're good to go here and can
7367 * just use the extent.
7368 *
7369 */
7370 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7371 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7372 em->block_start != EXTENT_MAP_HOLE)) {
7373 int type;
7374 u64 block_start, orig_start, orig_block_len, ram_bytes;
7375
7376 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7377 type = BTRFS_ORDERED_PREALLOC;
7378 else
7379 type = BTRFS_ORDERED_NOCOW;
7380 len = min(len, em->len - (start - em->start));
7381 block_start = em->block_start + (start - em->start);
7382
7383 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007384 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007385 btrfs_inc_nocow_writers(fs_info, block_start)) {
7386 struct extent_map *em2;
7387
Nikolay Borisov64f54182020-06-03 08:55:31 +03007388 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007389 orig_start, block_start,
7390 len, orig_block_len,
7391 ram_bytes, type);
7392 btrfs_dec_nocow_writers(fs_info, block_start);
7393 if (type == BTRFS_ORDERED_PREALLOC) {
7394 free_extent_map(em);
7395 *map = em = em2;
7396 }
7397
7398 if (em2 && IS_ERR(em2)) {
7399 ret = PTR_ERR(em2);
7400 goto out;
7401 }
7402 /*
7403 * For inode marked NODATACOW or extent marked PREALLOC,
7404 * use the existing or preallocated extent, so does not
7405 * need to adjust btrfs_space_info's bytes_may_use.
7406 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007407 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007408 goto skip_cow;
7409 }
7410 }
7411
7412 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007413 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007414 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007415 if (IS_ERR(em)) {
7416 ret = PTR_ERR(em);
7417 goto out;
7418 }
7419
7420 len = min(len, em->len - (start - em->start));
7421
7422skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007423 /*
7424 * Need to update the i_size under the extent lock so buffered
7425 * readers will get the updated i_size when we unlock.
7426 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007427 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007428 i_size_write(inode, start + len);
7429
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007430 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007431out:
7432 return ret;
7433}
7434
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007435static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7436 loff_t length, unsigned int flags, struct iomap *iomap,
7437 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007438{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007439 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007440 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007441 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307442 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007443 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007444 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007445 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007446 u64 len = length;
7447 bool unlock_extents = false;
Josef Bacikeb838e72012-07-31 16:28:48 -04007448
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007449 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007450 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007451
Josef Bacikc3298612012-08-03 16:49:19 -04007452 lockstart = start;
7453 lockend = start + len - 1;
7454
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007455 /*
7456 * The generic stuff only does filemap_write_and_wait_range, which
7457 * isn't enough if we've written compressed pages to this area, so we
7458 * need to flush the dirty pages again to make absolutely sure that any
7459 * outstanding dirty pages are on disk.
7460 */
7461 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7462 &BTRFS_I(inode)->runtime_flags)) {
7463 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7464 start + length - 1);
7465 if (ret)
7466 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007467 }
7468
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007469 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7470 if (!dio_data)
7471 return -ENOMEM;
7472
7473 dio_data->length = length;
7474 if (write) {
7475 dio_data->reserve = round_up(length, fs_info->sectorsize);
7476 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7477 &dio_data->data_reserved,
7478 start, dio_data->reserve);
7479 if (ret) {
7480 extent_changeset_free(dio_data->data_reserved);
7481 kfree(dio_data);
7482 return ret;
7483 }
7484 }
7485 iomap->private = dio_data;
7486
7487
Josef Bacikeb838e72012-07-31 16:28:48 -04007488 /*
7489 * If this errors out it's because we couldn't invalidate pagecache for
7490 * this range and we need to fallback to buffered.
7491 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007492 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007493 ret = -ENOTBLK;
7494 goto err;
7495 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007496
Omar Sandoval39b07b52019-12-02 17:34:23 -08007497 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007498 if (IS_ERR(em)) {
7499 ret = PTR_ERR(em);
7500 goto unlock_err;
7501 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007502
7503 /*
7504 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7505 * io. INLINE is special, and we could probably kludge it in here, but
7506 * it's still buffered so for safety lets just fall back to the generic
7507 * buffered path.
7508 *
7509 * For COMPRESSED we _have_ to read the entire extent in so we can
7510 * decompress it, so there will be buffering required no matter what we
7511 * do, so go ahead and fallback to buffered.
7512 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007513 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007514 * to buffered IO. Don't blame me, this is the price we pay for using
7515 * the generic code.
7516 */
7517 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7518 em->block_start == EXTENT_MAP_INLINE) {
7519 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007520 ret = -ENOTBLK;
7521 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007522 }
7523
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007524 len = min(len, em->len - (start - em->start));
7525 if (write) {
7526 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7527 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007528 if (ret < 0)
7529 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007530 unlock_extents = true;
7531 /* Recalc len in case the new em is smaller than requested */
7532 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007533 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007534 /*
7535 * We need to unlock only the end area that we aren't using.
7536 * The rest is going to be unlocked by the endio routine.
7537 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007538 lockstart = start + len;
7539 if (lockstart < lockend)
7540 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007541 }
7542
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007543 if (unlock_extents)
7544 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7545 lockstart, lockend, &cached_state);
7546 else
7547 free_extent_state(cached_state);
7548
7549 /*
7550 * Translate extent map information to iomap.
7551 * We trim the extents (and move the addr) even though iomap code does
7552 * that, since we have locked only the parts we are performing I/O in.
7553 */
7554 if ((em->block_start == EXTENT_MAP_HOLE) ||
7555 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7556 iomap->addr = IOMAP_NULL_ADDR;
7557 iomap->type = IOMAP_HOLE;
7558 } else {
7559 iomap->addr = em->block_start + (start - em->start);
7560 iomap->type = IOMAP_MAPPED;
7561 }
7562 iomap->offset = start;
7563 iomap->bdev = fs_info->fs_devices->latest_bdev;
7564 iomap->length = len;
7565
Josef Bacik4b46fce2010-05-23 11:00:55 -04007566 free_extent_map(em);
7567
7568 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007569
7570unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007571 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7572 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007573err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007574 if (dio_data) {
7575 btrfs_delalloc_release_space(BTRFS_I(inode),
7576 dio_data->data_reserved, start,
7577 dio_data->reserve, true);
7578 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7579 extent_changeset_free(dio_data->data_reserved);
7580 kfree(dio_data);
7581 }
7582 return ret;
7583}
7584
7585static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7586 ssize_t written, unsigned int flags, struct iomap *iomap)
7587{
7588 int ret = 0;
7589 struct btrfs_dio_data *dio_data = iomap->private;
7590 size_t submitted = dio_data->submitted;
7591 const bool write = !!(flags & IOMAP_WRITE);
7592
7593 if (!write && (iomap->type == IOMAP_HOLE)) {
7594 /* If reading from a hole, unlock and return */
7595 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7596 goto out;
7597 }
7598
7599 if (submitted < length) {
7600 pos += submitted;
7601 length -= submitted;
7602 if (write)
7603 __endio_write_update_ordered(BTRFS_I(inode), pos,
7604 length, false);
7605 else
7606 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7607 pos + length - 1);
7608 ret = -ENOTBLK;
7609 }
7610
7611 if (write) {
7612 if (dio_data->reserve)
7613 btrfs_delalloc_release_space(BTRFS_I(inode),
7614 dio_data->data_reserved, pos,
7615 dio_data->reserve, true);
7616 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
7617 extent_changeset_free(dio_data->data_reserved);
7618 }
7619out:
7620 kfree(dio_data);
7621 iomap->private = NULL;
7622
Josef Bacikeb838e72012-07-31 16:28:48 -04007623 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007624}
7625
Omar Sandoval769b4f22020-04-16 14:46:22 -07007626static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7627{
7628 /*
7629 * This implies a barrier so that stores to dio_bio->bi_status before
7630 * this and loads of dio_bio->bi_status after this are fully ordered.
7631 */
7632 if (!refcount_dec_and_test(&dip->refs))
7633 return;
7634
7635 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007636 __endio_write_update_ordered(BTRFS_I(dip->inode),
7637 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007638 dip->bytes,
7639 !dip->dio_bio->bi_status);
7640 } else {
7641 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7642 dip->logical_offset,
7643 dip->logical_offset + dip->bytes - 1);
7644 }
7645
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007646 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007647 kfree(dip);
7648}
7649
Omar Sandoval77d5d682020-04-16 14:46:25 -07007650static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7651 int mirror_num,
7652 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007653{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007654 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007655 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007656 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007657
Mike Christie37226b22016-06-05 14:31:52 -05007658 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007659
Omar Sandoval5c047a62020-04-16 14:46:24 -07007660 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007661 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007662 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007663
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007664 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007665 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007666 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007667 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007668 return ret;
7669}
7670
Omar Sandoval769b4f22020-04-16 14:46:22 -07007671static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7672 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007673 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007674{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007675 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7676 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007677 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007678 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7679 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007680 struct bio_vec bvec;
7681 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007682 u64 start = io_bio->logical;
7683 int icsum = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007684 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007685
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007686 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7687 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307688
Liu Bo17347ce2017-05-15 15:33:27 -07007689 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7690 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007691 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007692 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007693 if (uptodate &&
7694 (!csum || !check_data_csum(inode, io_bio, icsum,
Qu Wenruo265d4ac2020-10-21 14:24:54 +08007695 bvec.bv_page, pgoff))) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007696 clean_io_failure(fs_info, failure_tree, io_tree,
7697 start, bvec.bv_page,
7698 btrfs_ino(BTRFS_I(inode)),
7699 pgoff);
7700 } else {
7701 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007702
Omar Sandoval77d5d682020-04-16 14:46:25 -07007703 status = btrfs_submit_read_repair(inode,
7704 &io_bio->bio,
7705 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007706 bvec.bv_page, pgoff,
7707 start,
7708 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007709 io_bio->mirror_num,
7710 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007711 if (status)
7712 err = status;
7713 }
7714 start += sectorsize;
7715 icsum++;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307716 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307717 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007718 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007719 return err;
7720}
7721
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007722static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007723 const u64 offset, const u64 bytes,
7724 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007725{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007726 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007727 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007728 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007729 u64 ordered_offset = offset;
7730 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007731 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007732
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007733 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007734 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007735 else
Qu Wenruo524272602017-03-08 10:25:52 +08007736 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007737
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007738 while (ordered_offset < offset + bytes) {
7739 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007740 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007741 &ordered_offset,
7742 ordered_bytes,
7743 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007744 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7745 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007746 btrfs_queue_work(wq, &ordered->work);
7747 }
7748 /*
7749 * If btrfs_dec_test_ordered_pending does not find any ordered
7750 * extent in the range, we can exit.
7751 */
7752 if (ordered_offset == last_offset)
7753 return;
7754 /*
7755 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007756 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007757 */
7758 if (ordered_offset < offset + bytes) {
7759 ordered_bytes = offset + bytes - ordered_offset;
7760 ordered = NULL;
7761 }
Chris Mason163cf092010-11-28 19:56:33 -05007762 }
Filipe Manana14543772015-11-24 16:23:54 +00007763}
7764
Qu Wenruo8896a082020-10-21 14:24:53 +08007765static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
7766 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007767{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09007768 return btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04007769}
7770
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007771static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007772{
7773 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007774 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007775
Miao Xie8b110e32014-09-12 18:44:03 +08007776 if (err)
7777 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007778 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007779 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7780 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007781 (unsigned long long)bio->bi_iter.bi_sector,
7782 bio->bi_iter.bi_size, err);
7783
Omar Sandoval769b4f22020-04-16 14:46:22 -07007784 if (bio_op(bio) == REQ_OP_READ) {
7785 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007786 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007787 }
7788
Omar Sandoval769b4f22020-04-16 14:46:22 -07007789 if (err)
7790 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007791
Miao Xiee65e1532010-11-22 03:04:43 +00007792 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007793 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007794}
7795
David Sterbad0ee3932018-03-08 14:35:48 +01007796static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7797 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007798{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007799 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007800 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007801 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007802 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007803
Liu Bo4c274bc2017-11-01 17:19:27 -06007804 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007805 if (async_submit)
7806 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7807
Josef Bacik5fd02042012-05-02 14:00:54 -04007808 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007809 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007810 if (ret)
7811 goto err;
7812 }
Miao Xiee65e1532010-11-22 03:04:43 +00007813
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007814 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007815 goto map;
7816
7817 if (write && async_submit) {
Qu Wenruo8896a082020-10-21 14:24:53 +08007818 ret = btrfs_wq_submit_bio(inode, bio, 0, 0,
7819 file_offset,
David Sterbae288c082018-07-18 17:36:24 +02007820 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007821 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007822 } else if (write) {
7823 /*
7824 * If we aren't doing async submit, calculate the csum of the
7825 * bio now.
7826 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007827 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007828 if (ret)
7829 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007830 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007831 u64 csum_offset;
7832
7833 csum_offset = file_offset - dip->logical_offset;
David Sterba265fdfa2020-07-01 21:19:09 +02007834 csum_offset >>= fs_info->sectorsize_bits;
David Sterba55fc29b2020-06-30 02:01:31 +02007835 csum_offset *= fs_info->csum_size;
Omar Sandoval85879572020-04-16 14:46:21 -07007836 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007837 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007838map:
Chris Mason08635ba2019-07-10 12:28:14 -07007839 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007840err:
Miao Xiee65e1532010-11-22 03:04:43 +00007841 return ret;
7842}
7843
Omar Sandovalc36cac22020-04-16 14:46:13 -07007844/*
7845 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7846 * or ordered extents whether or not we submit any bios.
7847 */
7848static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7849 struct inode *inode,
7850 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007851{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007852 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007853 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7854 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007855 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007856
Omar Sandoval85879572020-04-16 14:46:21 -07007857 dip_size = sizeof(*dip);
7858 if (!write && csum) {
7859 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval85879572020-04-16 14:46:21 -07007860 size_t nblocks;
7861
David Sterba265fdfa2020-07-01 21:19:09 +02007862 nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
David Sterba223486c2020-07-02 11:27:30 +02007863 dip_size += fs_info->csum_size * nblocks;
Omar Sandoval85879572020-04-16 14:46:21 -07007864 }
7865
7866 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007867 if (!dip)
7868 return NULL;
7869
Omar Sandovalc36cac22020-04-16 14:46:13 -07007870 dip->inode = inode;
7871 dip->logical_offset = file_offset;
7872 dip->bytes = dio_bio->bi_iter.bi_size;
7873 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007874 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007875 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007876 return dip;
7877}
7878
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007879static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
7880 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007881{
7882 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007883 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007884 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7885 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007886 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007887 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007888 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007889 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007890 u64 submit_len;
7891 int clone_offset = 0;
7892 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307893 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007894 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007895 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007896 struct btrfs_dio_data *dio_data = iomap->private;
Miao Xiee65e1532010-11-22 03:04:43 +00007897
Omar Sandovalc36cac22020-04-16 14:46:13 -07007898 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7899 if (!dip) {
7900 if (!write) {
7901 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7902 file_offset + dio_bio->bi_iter.bi_size - 1);
7903 }
7904 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007905 bio_endio(dio_bio);
7906 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04007907 }
7908
Josef Bacik334c16d2020-10-16 11:29:14 -04007909 if (!write) {
Omar Sandoval85879572020-04-16 14:46:21 -07007910 /*
7911 * Load the csums up front to reduce csum tree searches and
7912 * contention when submitting bios.
Josef Bacik334c16d2020-10-16 11:29:14 -04007913 *
7914 * If we have csums disabled this will do nothing.
Omar Sandoval85879572020-04-16 14:46:21 -07007915 */
7916 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7917 dip->csums);
7918 if (status != BLK_STS_OK)
7919 goto out_err;
7920 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007921
Omar Sandoval769b4f22020-04-16 14:46:22 -07007922 start_sector = dio_bio->bi_iter.bi_sector;
7923 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007924
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007925 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007926 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7927 start_sector << 9, submit_len,
7928 &geom);
7929 if (ret) {
7930 status = errno_to_blk_status(ret);
7931 goto out_err;
7932 }
7933 ASSERT(geom.len <= INT_MAX);
7934
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007935 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007936
Liu Bo725130b2017-05-16 09:51:39 -07007937 /*
7938 * This will never fail as it's passing GPF_NOFS and
7939 * the allocation is backed by btrfs_bioset.
7940 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007941 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007942 bio->bi_private = dip;
7943 bio->bi_end_io = btrfs_end_dio_bio;
7944 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007945
Liu Bo725130b2017-05-16 09:51:39 -07007946 ASSERT(submit_len >= clone_len);
7947 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007948
Liu Bo725130b2017-05-16 09:51:39 -07007949 /*
7950 * Increase the count before we submit the bio so we know
7951 * the end IO handler won't happen before we increase the
7952 * count. Otherwise, the dip might get freed before we're
7953 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007954 *
7955 * We transfer the initial reference to the last bio, so we
7956 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007957 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007958 if (submit_len > 0) {
7959 refcount_inc(&dip->refs);
7960 /*
7961 * If we are submitting more than one bio, submit them
7962 * all asynchronously. The exception is RAID 5 or 6, as
7963 * asynchronous checksums make it difficult to collect
7964 * full stripe writes.
7965 */
7966 if (!raid56)
7967 async_submit = 1;
7968 }
Miao Xiee65e1532010-11-22 03:04:43 +00007969
David Sterbad0ee3932018-03-08 14:35:48 +01007970 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007971 async_submit);
7972 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007973 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007974 if (submit_len > 0)
7975 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007976 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007977 }
Liu Bo725130b2017-05-16 09:51:39 -07007978
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007979 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07007980 clone_offset += clone_len;
7981 start_sector += clone_len >> 9;
7982 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007983 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007984 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00007985
Miao Xiee65e1532010-11-22 03:04:43 +00007986out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007987 dip->dio_bio->bi_status = status;
7988 btrfs_dio_private_put(dip);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007989 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00007990}
7991
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05007992const struct iomap_ops btrfs_dio_iomap_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007993 .iomap_begin = btrfs_dio_iomap_begin,
7994 .iomap_end = btrfs_dio_iomap_end,
7995};
7996
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05007997const struct iomap_dio_ops btrfs_dio_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007998 .submit_io = btrfs_submit_direct,
7999};
8000
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008001static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008002 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008003{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008004 int ret;
8005
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008006 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008007 if (ret)
8008 return ret;
8009
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008010 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008011}
8012
Chris Mason9ebefb182007-06-15 13:50:00 -04008013int btrfs_readpage(struct file *file, struct page *page)
8014{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008015 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8016 u64 start = page_offset(page);
8017 u64 end = start + PAGE_SIZE - 1;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008018 unsigned long bio_flags = 0;
Nikolay Borisov0f208812020-09-14 14:39:16 +03008019 struct bio *bio = NULL;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008020 int ret;
8021
Nikolay Borisov0f208812020-09-14 14:39:16 +03008022 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8023
8024 ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008025 if (bio)
8026 ret = submit_one_bio(bio, 0, bio_flags);
8027 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008028}
Chris Mason1832a6d2007-12-21 16:27:21 -05008029
Chris Mason39279cc2007-06-12 06:35:45 -04008030static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8031{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008032 struct inode *inode = page->mapping->host;
8033 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008034
8035 if (current->flags & PF_MEMALLOC) {
8036 redirty_page_for_writepage(wbc, page);
8037 unlock_page(page);
8038 return 0;
8039 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008040
8041 /*
8042 * If we are under memory pressure we will call this directly from the
8043 * VM, we need to make sure we have the inode referenced for the ordered
8044 * extent. If not just return like we didn't do anything.
8045 */
8046 if (!igrab(inode)) {
8047 redirty_page_for_writepage(wbc, page);
8048 return AOP_WRITEPAGE_ACTIVATE;
8049 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008050 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008051 btrfs_add_delayed_iput(inode);
8052 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008053}
Chris Mason39279cc2007-06-12 06:35:45 -04008054
Eric Sandeen48a3b632013-04-25 20:41:01 +00008055static int btrfs_writepages(struct address_space *mapping,
8056 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008057{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008058 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008059}
8060
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008061static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008062{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008063 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008064}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008065
Chris Masone6dcd2d2008-07-17 12:53:50 -04008066static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008067{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008068 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008069 if (ret == 1)
8070 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008071 return ret;
8072}
Chris Mason39279cc2007-06-12 06:35:45 -04008073
Chris Masone6dcd2d2008-07-17 12:53:50 -04008074static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8075{
Chris Mason98509cf2008-09-11 15:51:43 -04008076 if (PageWriteback(page) || PageDirty(page))
8077 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008078 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008079}
8080
Roman Gushchinf8e66082020-03-04 16:57:35 -08008081#ifdef CONFIG_MIGRATION
8082static int btrfs_migratepage(struct address_space *mapping,
8083 struct page *newpage, struct page *page,
8084 enum migrate_mode mode)
8085{
8086 int ret;
8087
8088 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8089 if (ret != MIGRATEPAGE_SUCCESS)
8090 return ret;
8091
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008092 if (page_has_private(page))
8093 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008094
8095 if (PagePrivate2(page)) {
8096 ClearPagePrivate2(page);
8097 SetPagePrivate2(newpage);
8098 }
8099
8100 if (mode != MIGRATE_SYNC_NO_COPY)
8101 migrate_page_copy(newpage, page);
8102 else
8103 migrate_page_states(newpage, page);
8104 return MIGRATEPAGE_SUCCESS;
8105}
8106#endif
8107
Lukas Czernerd47992f2013-05-21 23:17:23 -04008108static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8109 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008110{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008111 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8112 struct extent_io_tree *tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008113 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008114 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008115 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008116 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308117 u64 start;
8118 u64 end;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008119 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008120
Chris Mason8b62b722009-09-02 16:53:46 -04008121 /*
8122 * we have the page locked, so new writeback can't start,
8123 * and the dirty bit won't be cleared while we are here.
8124 *
8125 * Wait for IO on this page so that we can safely clear
8126 * the PagePrivate2 bit and do ordered accounting
8127 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008128 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008129
Chris Masone6dcd2d2008-07-17 12:53:50 -04008130 if (offset) {
8131 btrfs_releasepage(page, GFP_NOFS);
8132 return;
8133 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008134
8135 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008136 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308137again:
8138 start = page_start;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008139 ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008140 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008141 end = min(page_end,
8142 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008143 /*
8144 * IO on this page will never be started, so we need
8145 * to account for any ordered extents now
8146 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008147 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308148 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008149 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008150 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008151 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008152 /*
8153 * whoever cleared the private bit is responsible
8154 * for the finish_ordered_io
8155 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008156 if (TestClearPagePrivate2(page)) {
8157 struct btrfs_ordered_inode_tree *tree;
8158 u64 new_len;
8159
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008160 tree = &inode->ordered_tree;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008161
8162 spin_lock_irq(&tree->lock);
8163 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308164 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008165 if (new_len < ordered->truncated_len)
8166 ordered->truncated_len = new_len;
8167 spin_unlock_irq(&tree->lock);
8168
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008169 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8170 start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308171 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008172 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008173 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008174 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008175 if (!inode_evicting) {
8176 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308177 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008178 &cached_state);
8179 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308180
8181 start = end + 1;
8182 if (start < page_end)
8183 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008184 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008185
Qu Wenruob9d0b382015-09-29 10:35:16 +08008186 /*
8187 * Qgroup reserved space handler
8188 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008189 * 1) Already written to disk or ordered extent already submitted
8190 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8191 * Qgroup will be handled by its qgroup_record then.
8192 * btrfs_qgroup_free_data() call will do nothing here.
8193 *
8194 * 2) Not written to disk yet
8195 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8196 * bit of its io_tree, and free the qgroup reserved data space.
8197 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008198 */
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008199 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008200 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008201 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008202 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8203 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008204 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008205
8206 __btrfs_releasepage(page, GFP_NOFS);
8207 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008208
Chris Mason4a096752008-07-21 10:29:44 -04008209 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008210 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008211}
8212
Chris Mason9ebefb182007-06-15 13:50:00 -04008213/*
8214 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8215 * called from a page fault handler when a page is first dirtied. Hence we must
8216 * be careful to check for EOF conditions here. We set the page up correctly
8217 * for a written page which means we get ENOSPC checking when writing into
8218 * holes and correct delalloc and unwritten extent mapping on filesystems that
8219 * support these features.
8220 *
8221 * We are not allowed to take the i_mutex here so we have to play games to
8222 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008223 * truncate_setsize() writes the inode size before removing pages, once we have
8224 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008225 * beyond EOF, then the page is guaranteed safe against truncation until we
8226 * unlock the page.
8227 */
Souptick Joardera528a242018-06-06 19:54:44 +05308228vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008229{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008230 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008231 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008232 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008233 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8234 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008235 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008236 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008237 char *kaddr;
8238 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008239 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308240 vm_fault_t ret;
8241 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008242 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308243 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008244 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008245 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308246 u64 end;
8247
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008248 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008249
Jan Karab2b5ef52012-06-12 16:20:45 +02008250 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008251 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008252 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308253 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008254
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308255 /*
8256 * Reserving delalloc space after obtaining the page lock can lead to
8257 * deadlock. For example, if a dirty page is locked by this function
8258 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8259 * dirty page write out, then the btrfs_writepage() function could
8260 * end up waiting indefinitely to get a lock on the page currently
8261 * being processed by btrfs_page_mkwrite() function.
8262 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008263 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8264 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308265 if (!ret2) {
8266 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008267 reserved = 1;
8268 }
Souptick Joardera528a242018-06-06 19:54:44 +05308269 if (ret2) {
8270 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008271 if (reserved)
8272 goto out;
8273 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008274 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008275
Nick Piggin56a76f82009-03-31 15:23:23 -07008276 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008277again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008278 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008279 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008280
Chris Mason9ebefb182007-06-15 13:50:00 -04008281 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008282 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008283 /* page got truncated out from underneath us */
8284 goto out_unlock;
8285 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008286 wait_on_page_writeback(page);
8287
David Sterbaff13db42015-12-03 14:30:40 +01008288 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008289 set_page_extent_mapped(page);
8290
Chris Masoneb84ae02008-07-17 13:53:27 -04008291 /*
8292 * we can't set the delalloc bits if there are pending ordered
8293 * extents. Drop our locks and wait for them to finish
8294 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008295 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8296 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008297 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008298 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008299 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008300 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008301 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008302 btrfs_put_ordered_extent(ordered);
8303 goto again;
8304 }
8305
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008306 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008307 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008308 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008309 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308310 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008311 btrfs_delalloc_release_space(BTRFS_I(inode),
8312 data_reserved, page_start,
8313 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308314 }
8315 }
8316
Josef Bacikfbf19082009-10-01 17:10:23 -04008317 /*
Liu Bo54160342016-12-13 12:15:19 -08008318 * page_mkwrite gets called when the page is firstly dirtied after it's
8319 * faulted in, but write(2) could also dirty a page and set delalloc
8320 * bits, thus in this case for space account reason, we still need to
8321 * clear any delalloc bits within this page range since we have to
8322 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008323 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308324 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008325 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8326 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008327
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008328 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008329 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308330 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008331 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008332 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008333 ret = VM_FAULT_SIGBUS;
8334 goto out_unlock;
8335 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008336
8337 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008338 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008339 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008340 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008341 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008342
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008343 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008344 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008345 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008346 flush_dcache_page(page);
8347 kunmap(page);
8348 }
Chris Mason247e7432008-07-17 12:53:51 -04008349 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008350 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008351 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008352
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008353 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008354 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008355 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008356
David Sterbae43bbe52017-12-12 21:43:52 +01008357 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008358
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008359 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8360 sb_end_pagefault(inode->i_sb);
8361 extent_changeset_free(data_reserved);
8362 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008363
8364out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008365 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008366out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008367 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008368 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008369 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008370out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008371 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008372 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008373 return ret;
8374}
8375
Filipe Manana213e8c52018-02-06 20:40:31 +00008376static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008377{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008378 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008379 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008380 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008381 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008382 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008383 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008384 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008385
Filipe Manana213e8c52018-02-06 20:40:31 +00008386 if (!skip_writeback) {
8387 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8388 (u64)-1);
8389 if (ret)
8390 return ret;
8391 }
Chris Mason39279cc2007-06-12 06:35:45 -04008392
Josef Bacikfcb80c22011-05-03 10:40:22 -04008393 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008394 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8395 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008396 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008397 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008398 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008399 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008400 * be free'd up by the truncate operation, but also have some slack
8401 * space reserved in case it uses space during the truncate (thank you
8402 * very much snapshotting).
8403 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008404 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008405 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008406 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008407 * doesn't end up using space reserved for updating the inode. We also
8408 * need to be able to stop the transaction and start a new one, which
8409 * means we need to be able to update the inode several times, and we
8410 * have no idea of knowing how many times that will be, so we can't just
8411 * reserve 1 item for the entirety of the operation, so that has to be
8412 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008413 *
8414 * So that leaves us with
8415 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008416 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008417 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008418 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008419 * updating the inode.
8420 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008421 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008422 if (!rsv)
8423 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008424 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008425 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008426
Josef Bacik907cbce2011-08-08 13:46:15 -04008427 /*
Josef Bacik07127182011-08-19 10:29:59 -04008428 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008429 * 1 for updating the inode.
8430 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008431 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008432 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008433 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008434 goto out;
8435 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008436
Josef Bacik907cbce2011-08-08 13:46:15 -04008437 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008438 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008439 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008440 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008441
Chris Mason5a3f23d2009-03-31 13:27:11 -04008442 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008443 * So if we truncate and then write and fsync we normally would just
8444 * write the extents that changed, which is a problem if we need to
8445 * first truncate that entire inode. So set this flag so we write out
8446 * all of the extents in the inode to the sync log so we're completely
8447 * safe.
8448 */
8449 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008450 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008451
Yan, Zheng80825102009-11-12 09:35:36 +00008452 while (1) {
8453 ret = btrfs_truncate_inode_items(trans, root, inode,
8454 inode->i_size,
8455 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008456 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008457 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008458 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008459
Yan, Zheng80825102009-11-12 09:35:36 +00008460 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008461 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008462 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008463
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008464 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008465 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008466
8467 trans = btrfs_start_transaction(root, 2);
8468 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008469 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008470 trans = NULL;
8471 break;
8472 }
8473
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008474 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008475 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008476 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008477 BUG_ON(ret); /* shouldn't happen */
8478 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008479 }
8480
Josef Bacikddfae632017-10-19 14:16:02 -04008481 /*
8482 * We can't call btrfs_truncate_block inside a trans handle as we could
8483 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8484 * we've truncated everything except the last little bit, and can do
8485 * btrfs_truncate_block and then update the disk_i_size.
8486 */
8487 if (ret == NEED_TRUNCATE_BLOCK) {
8488 btrfs_end_transaction(trans);
8489 btrfs_btree_balance_dirty(fs_info);
8490
8491 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8492 if (ret)
8493 goto out;
8494 trans = btrfs_start_transaction(root, 1);
8495 if (IS_ERR(trans)) {
8496 ret = PTR_ERR(trans);
8497 goto out;
8498 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008499 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008500 }
8501
Chris Mason917c16b2011-11-08 14:49:59 -05008502 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008503 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008504
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008505 trans->block_rsv = &fs_info->trans_block_rsv;
8506 ret2 = btrfs_update_inode(trans, root, inode);
8507 if (ret2 && !ret)
8508 ret = ret2;
8509
8510 ret2 = btrfs_end_transaction(trans);
8511 if (ret2 && !ret)
8512 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008513 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008514 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008515out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008516 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008517
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008518 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008519}
8520
Sven Wegener3b963622008-06-09 21:57:42 -04008521/*
Chris Masond352ac62008-09-29 15:18:18 -04008522 * create a new subvolume directory/inode (helper for the ioctl).
8523 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008524int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008525 struct btrfs_root *new_root,
8526 struct btrfs_root *parent_root,
8527 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008528{
Chris Mason39279cc2007-06-12 06:35:45 -04008529 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008530 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008531 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008532
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008533 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8534 new_dirid, new_dirid,
8535 S_IFDIR | (~current_umask() & S_IRWXUGO),
8536 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008537 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008538 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008539 inode->i_op = &btrfs_dir_inode_operations;
8540 inode->i_fop = &btrfs_dir_file_operations;
8541
Miklos Szeredibfe86842011-10-28 14:13:29 +02008542 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008543 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008544 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008545
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008546 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8547 if (err)
8548 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008549 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008550 new_root->root_key.objectid, err);
8551
Yan, Zheng76dda932009-09-21 16:00:26 -04008552 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008553
Yan, Zheng76dda932009-09-21 16:00:26 -04008554 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008555 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008556}
8557
Chris Mason39279cc2007-06-12 06:35:45 -04008558struct inode *btrfs_alloc_inode(struct super_block *sb)
8559{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008560 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008561 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008562 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008563
David Sterba712e36c2017-10-31 17:08:27 +01008564 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008565 if (!ei)
8566 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008567
8568 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008569 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008570 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008571 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008572 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008573 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008574 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008575 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008576 ei->disk_i_size = 0;
8577 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008578 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008579 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008580 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008581 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008582 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008583 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008584
Josef Bacik9e0baf62011-07-15 15:16:44 +00008585 spin_lock_init(&ei->lock);
8586 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008587 if (sb->s_magic != BTRFS_TEST_MAGIC)
8588 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8589 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008590 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008591 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008592 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008593
Miao Xie16cdcec2011-04-22 18:12:22 +08008594 ei->delayed_node = NULL;
8595
chandan r9cc97d62012-07-04 12:48:07 +05308596 ei->i_otime.tv_sec = 0;
8597 ei->i_otime.tv_nsec = 0;
8598
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008599 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008600 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008601 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8602 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8603 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008604 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8605 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008606 ei->io_tree.track_uptodate = true;
8607 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008608 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008609 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008610 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008611 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008612 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008613 RB_CLEAR_NODE(&ei->rb_node);
8614
8615 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008616}
8617
Josef Bacikaaedb552013-10-11 14:44:09 -04008618#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8619void btrfs_test_destroy_inode(struct inode *inode)
8620{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008621 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008622 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8623}
8624#endif
8625
Al Viro26602ca2019-04-10 15:14:41 -04008626void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008627{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008628 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8629}
8630
Nikolay Borisov633cc812020-09-18 12:15:49 +03008631void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008632{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008633 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03008634 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8635 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008636
Nikolay Borisov633cc812020-09-18 12:15:49 +03008637 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8638 WARN_ON(vfs_inode->i_data.nrpages);
8639 WARN_ON(inode->block_rsv.reserved);
8640 WARN_ON(inode->block_rsv.size);
8641 WARN_ON(inode->outstanding_extents);
8642 WARN_ON(inode->delalloc_bytes);
8643 WARN_ON(inode->new_delalloc_bytes);
8644 WARN_ON(inode->csum_bytes);
8645 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008646
Chris Mason5a3f23d2009-03-31 13:27:11 -04008647 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008648 * This can happen where we create an inode, but somebody else also
8649 * created the same inode and we need to destroy the one we already
8650 * created.
8651 */
8652 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008653 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008654
Chris Masond3977122009-01-05 21:25:51 -05008655 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008656 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008657 if (!ordered)
8658 break;
8659 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008660 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008661 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008662 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03008663 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008664 btrfs_put_ordered_extent(ordered);
8665 btrfs_put_ordered_extent(ordered);
8666 }
8667 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03008668 btrfs_qgroup_check_reserved_leak(inode);
8669 inode_tree_del(inode);
8670 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8671 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8672 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008673}
8674
Al Viro45321ac2010-06-07 13:43:19 -04008675int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008676{
8677 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008678
Naohiro Aota6379ef92013-06-06 09:56:34 +00008679 if (root == NULL)
8680 return 1;
8681
Liu Bofa6ac872013-02-20 14:10:23 +00008682 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008683 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008684 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008685 else
Al Viro45321ac2010-06-07 13:43:19 -04008686 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008687}
8688
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008689static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008690{
8691 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8692
8693 inode_init_once(&ei->vfs_inode);
8694}
8695
David Sterbae67c7182018-02-19 17:24:18 +01008696void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008697{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008698 /*
8699 * Make sure all delayed rcu free inodes are flushed before we
8700 * destroy cache.
8701 */
8702 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008703 kmem_cache_destroy(btrfs_inode_cachep);
8704 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008705 kmem_cache_destroy(btrfs_path_cachep);
8706 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008707 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008708}
8709
Liu Bof5c29bd2017-11-02 17:21:50 -06008710int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008711{
David Sterba837e1972012-09-07 03:00:48 -06008712 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008713 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008714 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8715 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008716 if (!btrfs_inode_cachep)
8717 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008718
David Sterba837e1972012-09-07 03:00:48 -06008719 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008720 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008721 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008722 if (!btrfs_trans_handle_cachep)
8723 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008724
David Sterba837e1972012-09-07 03:00:48 -06008725 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008726 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008727 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008728 if (!btrfs_path_cachep)
8729 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008730
David Sterba837e1972012-09-07 03:00:48 -06008731 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008732 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008733 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008734 if (!btrfs_free_space_cachep)
8735 goto fail;
8736
Christophe Leroy3acd4852019-08-21 15:05:55 +00008737 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8738 PAGE_SIZE, PAGE_SIZE,
8739 SLAB_RED_ZONE, NULL);
8740 if (!btrfs_free_space_bitmap_cachep)
8741 goto fail;
8742
Chris Mason39279cc2007-06-12 06:35:45 -04008743 return 0;
8744fail:
8745 btrfs_destroy_cachep();
8746 return -ENOMEM;
8747}
8748
David Howellsa528d352017-01-31 16:46:22 +00008749static int btrfs_getattr(const struct path *path, struct kstat *stat,
8750 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008751{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008752 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008753 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008754 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008755 u32 bi_flags = BTRFS_I(inode)->flags;
8756
8757 stat->result_mask |= STATX_BTIME;
8758 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8759 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8760 if (bi_flags & BTRFS_INODE_APPEND)
8761 stat->attributes |= STATX_ATTR_APPEND;
8762 if (bi_flags & BTRFS_INODE_COMPRESS)
8763 stat->attributes |= STATX_ATTR_COMPRESSED;
8764 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8765 stat->attributes |= STATX_ATTR_IMMUTABLE;
8766 if (bi_flags & BTRFS_INODE_NODUMP)
8767 stat->attributes |= STATX_ATTR_NODUMP;
8768
8769 stat->attributes_mask |= (STATX_ATTR_APPEND |
8770 STATX_ATTR_COMPRESSED |
8771 STATX_ATTR_IMMUTABLE |
8772 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008773
Chris Mason39279cc2007-06-12 06:35:45 -04008774 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008775 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008776
8777 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008778 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008779 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008780 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008781 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008782 return 0;
8783}
8784
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008785static int btrfs_rename_exchange(struct inode *old_dir,
8786 struct dentry *old_dentry,
8787 struct inode *new_dir,
8788 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008789{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008790 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008791 struct btrfs_trans_handle *trans;
8792 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008793 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008794 struct inode *new_inode = new_dentry->d_inode;
8795 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008796 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008797 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8798 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008799 u64 old_idx = 0;
8800 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008801 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01008802 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008803 bool root_log_pinned = false;
8804 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008805
8806 /* we only allow rename subvolume link between subvolumes */
8807 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8808 return -EXDEV;
8809
8810 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008811 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8812 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008813 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008814
8815 /*
8816 * We want to reserve the absolute worst case amount of items. So if
8817 * both inodes are subvols and we need to unlink them then that would
8818 * require 4 item modifications, but if they are both normal inodes it
8819 * would require 5 item modifications, so we'll assume their normal
8820 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8821 * should cover the worst case number of items we'll modify.
8822 */
8823 trans = btrfs_start_transaction(root, 12);
8824 if (IS_ERR(trans)) {
8825 ret = PTR_ERR(trans);
8826 goto out_notrans;
8827 }
8828
Josef Bacik3e174092019-11-15 15:43:06 -05008829 if (dest != root)
8830 btrfs_record_root_in_trans(trans, dest);
8831
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008832 /*
8833 * We need to find a free sequence number both in the source and
8834 * in the destination directory for the exchange.
8835 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008836 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008837 if (ret)
8838 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008839 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008840 if (ret)
8841 goto out_fail;
8842
8843 BTRFS_I(old_inode)->dir_index = 0ULL;
8844 BTRFS_I(new_inode)->dir_index = 0ULL;
8845
8846 /* Reference for the source. */
8847 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8848 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008849 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008850 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008851 btrfs_pin_log_trans(root);
8852 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008853 ret = btrfs_insert_inode_ref(trans, dest,
8854 new_dentry->d_name.name,
8855 new_dentry->d_name.len,
8856 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008857 btrfs_ino(BTRFS_I(new_dir)),
8858 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008859 if (ret)
8860 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008861 }
8862
8863 /* And now for the dest. */
8864 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8865 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008866 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008867 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008868 btrfs_pin_log_trans(dest);
8869 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008870 ret = btrfs_insert_inode_ref(trans, root,
8871 old_dentry->d_name.name,
8872 old_dentry->d_name.len,
8873 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008874 btrfs_ino(BTRFS_I(old_dir)),
8875 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008876 if (ret)
8877 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008878 }
8879
8880 /* Update inode version and ctime/mtime. */
8881 inode_inc_iversion(old_dir);
8882 inode_inc_iversion(new_dir);
8883 inode_inc_iversion(old_inode);
8884 inode_inc_iversion(new_inode);
8885 old_dir->i_ctime = old_dir->i_mtime = ctime;
8886 new_dir->i_ctime = new_dir->i_mtime = ctime;
8887 old_inode->i_ctime = ctime;
8888 new_inode->i_ctime = ctime;
8889
8890 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008891 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8892 BTRFS_I(old_inode), 1);
8893 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8894 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008895 }
8896
8897 /* src is a subvolume */
8898 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008899 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008900 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008901 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8902 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008903 old_dentry->d_name.name,
8904 old_dentry->d_name.len);
8905 if (!ret)
8906 ret = btrfs_update_inode(trans, root, old_inode);
8907 }
8908 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008909 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008910 goto out_fail;
8911 }
8912
8913 /* dest is a subvolume */
8914 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008915 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008916 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008917 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8918 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008919 new_dentry->d_name.name,
8920 new_dentry->d_name.len);
8921 if (!ret)
8922 ret = btrfs_update_inode(trans, dest, new_inode);
8923 }
8924 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008925 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008926 goto out_fail;
8927 }
8928
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008929 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008930 new_dentry->d_name.name,
8931 new_dentry->d_name.len, 0, old_idx);
8932 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008933 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008934 goto out_fail;
8935 }
8936
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008937 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008938 old_dentry->d_name.name,
8939 old_dentry->d_name.len, 0, new_idx);
8940 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008941 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008942 goto out_fail;
8943 }
8944
8945 if (old_inode->i_nlink == 1)
8946 BTRFS_I(old_inode)->dir_index = old_idx;
8947 if (new_inode->i_nlink == 1)
8948 BTRFS_I(new_inode)->dir_index = new_idx;
8949
Filipe Manana86e8aa02016-05-05 02:02:27 +01008950 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01008951 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
8952 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008953 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008954 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008955 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01008956 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01008957 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
8958 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008959 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008960 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008961 }
8962out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01008963 /*
8964 * If we have pinned a log and an error happened, we unpin tasks
8965 * trying to sync the log and force them to fallback to a transaction
8966 * commit if the log currently contains any of the inodes involved in
8967 * this rename operation (to ensure we do not persist a log with an
8968 * inconsistent state for any of these inodes or leading to any
8969 * inconsistencies when replayed). If the transaction was aborted, the
8970 * abortion reason is propagated to userspace when attempting to commit
8971 * the transaction. If the log does not contain any of these inodes, we
8972 * allow the tasks to sync it.
8973 */
8974 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008975 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
8976 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
8977 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01008978 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008979 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01008980 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008981
8982 if (root_log_pinned) {
8983 btrfs_end_log_trans(root);
8984 root_log_pinned = false;
8985 }
8986 if (dest_log_pinned) {
8987 btrfs_end_log_trans(dest);
8988 dest_log_pinned = false;
8989 }
8990 }
Filipe Manana75b463d2020-08-11 12:43:48 +01008991 ret2 = btrfs_end_transaction(trans);
8992 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008993out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05008994 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
8995 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008996 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008997
8998 return ret;
8999}
9000
9001static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9002 struct btrfs_root *root,
9003 struct inode *dir,
9004 struct dentry *dentry)
9005{
9006 int ret;
9007 struct inode *inode;
9008 u64 objectid;
9009 u64 index;
9010
9011 ret = btrfs_find_free_ino(root, &objectid);
9012 if (ret)
9013 return ret;
9014
9015 inode = btrfs_new_inode(trans, root, dir,
9016 dentry->d_name.name,
9017 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009018 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009019 objectid,
9020 S_IFCHR | WHITEOUT_MODE,
9021 &index);
9022
9023 if (IS_ERR(inode)) {
9024 ret = PTR_ERR(inode);
9025 return ret;
9026 }
9027
9028 inode->i_op = &btrfs_special_inode_operations;
9029 init_special_inode(inode, inode->i_mode,
9030 WHITEOUT_DEV);
9031
9032 ret = btrfs_init_inode_security(trans, inode, dir,
9033 &dentry->d_name);
9034 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009035 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009036
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009037 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9038 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009039 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009040 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009041
9042 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009043out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009044 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009045 if (ret)
9046 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009047 iput(inode);
9048
Filipe Mananac9901612016-05-05 01:41:57 +01009049 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009050}
9051
Chris Mason39279cc2007-06-12 06:35:45 -04009052static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009053 struct inode *new_dir, struct dentry *new_dentry,
9054 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009055{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009056 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009057 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009058 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009059 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9060 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009061 struct inode *new_inode = d_inode(new_dentry);
9062 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009063 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009064 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009065 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009066 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009067 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009068
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009069 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009070 return -EPERM;
9071
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009072 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009073 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009074 return -EXDEV;
9075
Li Zefan33345d012011-04-20 10:31:50 +08009076 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009077 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009078 return -ENOTEMPTY;
9079
Chris Mason39279cc2007-06-12 06:35:45 -04009080 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009081 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009082 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009083
9084
9085 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009086 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009087 new_dentry->d_name.name,
9088 new_dentry->d_name.len);
9089
9090 if (ret) {
9091 if (ret == -EEXIST) {
9092 /* we shouldn't get
9093 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309094 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009095 return ret;
9096 }
9097 } else {
9098 /* maybe -EOVERFLOW */
9099 return ret;
9100 }
9101 }
9102 ret = 0;
9103
Chris Mason5a3f23d2009-03-31 13:27:11 -04009104 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009105 * we're using rename to replace one file with another. Start IO on it
9106 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009107 */
Chris Mason8d875f92014-08-12 10:47:42 -07009108 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009109 filemap_flush(old_inode->i_mapping);
9110
Yan, Zheng76dda932009-09-21 16:00:26 -04009111 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009112 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009113 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009114 /*
9115 * We want to reserve the absolute worst case amount of items. So if
9116 * both inodes are subvols and we need to unlink them then that would
9117 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009118 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009119 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9120 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009121 * If our rename has the whiteout flag, we need more 5 units for the
9122 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9123 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009124 */
Filipe Manana5062af32016-05-05 10:26:26 +01009125 trans_num_items = 11;
9126 if (flags & RENAME_WHITEOUT)
9127 trans_num_items += 5;
9128 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009129 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009130 ret = PTR_ERR(trans);
9131 goto out_notrans;
9132 }
Chris Mason5f39d392007-10-15 16:14:19 -04009133
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009134 if (dest != root)
9135 btrfs_record_root_in_trans(trans, dest);
9136
Nikolay Borisov877574e2017-02-20 13:50:33 +02009137 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009138 if (ret)
9139 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009140
Miao Xie67de1172013-12-26 13:07:06 +08009141 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009142 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009143 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009144 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009145 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009146 btrfs_pin_log_trans(root);
9147 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009148 ret = btrfs_insert_inode_ref(trans, dest,
9149 new_dentry->d_name.name,
9150 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009151 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009152 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009153 if (ret)
9154 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009155 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009156
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009157 inode_inc_iversion(old_dir);
9158 inode_inc_iversion(new_dir);
9159 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009160 old_dir->i_ctime = old_dir->i_mtime =
9161 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009162 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009163
Chris Mason12fcfd22009-03-24 10:24:20 -04009164 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009165 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9166 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009167
Li Zefan33345d012011-04-20 10:31:50 +08009168 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009169 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009170 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009171 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9172 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009173 old_dentry->d_name.name,
9174 old_dentry->d_name.len);
9175 if (!ret)
9176 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009177 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009178 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009179 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009180 goto out_fail;
9181 }
Chris Mason39279cc2007-06-12 06:35:45 -04009182
9183 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009184 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009185 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009186 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009187 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009188 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009189 BUG_ON(new_inode->i_nlink == 0);
9190 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009191 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9192 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009193 new_dentry->d_name.name,
9194 new_dentry->d_name.len);
9195 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009196 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009197 ret = btrfs_orphan_add(trans,
9198 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009199 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009200 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009201 goto out_fail;
9202 }
Chris Mason39279cc2007-06-12 06:35:45 -04009203 }
Josef Bacikaec74772008-07-24 12:12:38 -04009204
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009205 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009206 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009207 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009208 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009209 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009210 goto out_fail;
9211 }
Chris Mason39279cc2007-06-12 06:35:45 -04009212
Miao Xie67de1172013-12-26 13:07:06 +08009213 if (old_inode->i_nlink == 1)
9214 BTRFS_I(old_inode)->dir_index = index;
9215
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009216 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009217 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9218 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009219 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009220 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009221 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009222
9223 if (flags & RENAME_WHITEOUT) {
9224 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9225 old_dentry);
9226
9227 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009228 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009229 goto out_fail;
9230 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009231 }
Chris Mason39279cc2007-06-12 06:35:45 -04009232out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009233 /*
9234 * If we have pinned the log and an error happened, we unpin tasks
9235 * trying to sync the log and force them to fallback to a transaction
9236 * commit if the log currently contains any of the inodes involved in
9237 * this rename operation (to ensure we do not persist a log with an
9238 * inconsistent state for any of these inodes or leading to any
9239 * inconsistencies when replayed). If the transaction was aborted, the
9240 * abortion reason is propagated to userspace when attempting to commit
9241 * the transaction. If the log does not contain any of these inodes, we
9242 * allow the tasks to sync it.
9243 */
9244 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009245 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9246 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9247 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009248 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009249 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009250 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009251
9252 btrfs_end_log_trans(root);
9253 log_pinned = false;
9254 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009255 ret2 = btrfs_end_transaction(trans);
9256 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009257out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009258 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009259 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009260
Chris Mason39279cc2007-06-12 06:35:45 -04009261 return ret;
9262}
9263
Miklos Szeredi80ace852014-07-23 15:15:32 +02009264static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9265 struct inode *new_dir, struct dentry *new_dentry,
9266 unsigned int flags)
9267{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009268 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009269 return -EINVAL;
9270
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009271 if (flags & RENAME_EXCHANGE)
9272 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9273 new_dentry);
9274
9275 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009276}
9277
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009278struct btrfs_delalloc_work {
9279 struct inode *inode;
9280 struct completion completion;
9281 struct list_head list;
9282 struct btrfs_work work;
9283};
9284
Miao Xie8ccf6f192012-10-25 09:28:04 +00009285static void btrfs_run_delalloc_work(struct btrfs_work *work)
9286{
9287 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009288 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009289
9290 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9291 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009292 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009293 filemap_flush(inode->i_mapping);
9294 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9295 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009296 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009297
Nikolay Borisov076da912018-04-23 10:54:16 +03009298 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009299 complete(&delalloc_work->completion);
9300}
9301
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009302static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009303{
9304 struct btrfs_delalloc_work *work;
9305
David Sterba100d5702015-12-08 14:39:32 +01009306 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009307 if (!work)
9308 return NULL;
9309
9310 init_completion(&work->completion);
9311 INIT_LIST_HEAD(&work->list);
9312 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009313 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009314
9315 return work;
9316}
9317
Chris Masond352ac62008-09-29 15:18:18 -04009318/*
9319 * some fairly slow code that needs optimization. This walks the list
9320 * of all the inodes with pending delalloc and forces them to disk.
9321 */
Josef Bacikb4912132020-07-21 10:22:12 -04009322static int start_delalloc_inodes(struct btrfs_root *root, u64 *nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009323{
Chris Masonea8c2812008-08-04 23:17:27 -04009324 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009325 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009326 struct btrfs_delalloc_work *work, *next;
9327 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009328 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009329 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009330
Miao Xie8ccf6f192012-10-25 09:28:04 +00009331 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009332 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009333
Miao Xie573bfb72014-03-06 13:55:03 +08009334 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009335 spin_lock(&root->delalloc_lock);
9336 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009337 while (!list_empty(&splice)) {
9338 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009339 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009340
Miao Xieeb73c1b2013-05-15 07:48:22 +00009341 list_move_tail(&binode->delalloc_inodes,
9342 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009343 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009344 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009345 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009346 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009347 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009348 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009349
Ethan Lien3cd24c62018-11-01 14:49:03 +08009350 if (snapshot)
9351 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9352 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009353 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009354 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009355 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009356 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009357 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009358 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009359 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009360 btrfs_queue_work(root->fs_info->flush_workers,
9361 &work->work);
Josef Bacikb4912132020-07-21 10:22:12 -04009362 if (*nr != U64_MAX) {
9363 (*nr)--;
9364 if (*nr == 0)
9365 goto out;
9366 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009367 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009368 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009369 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009370 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009371
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009372out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009373 list_for_each_entry_safe(work, next, &works, list) {
9374 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009375 wait_for_completion(&work->completion);
9376 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009377 }
9378
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009379 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009380 spin_lock(&root->delalloc_lock);
9381 list_splice_tail(&splice, &root->delalloc_inodes);
9382 spin_unlock(&root->delalloc_lock);
9383 }
Miao Xie573bfb72014-03-06 13:55:03 +08009384 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009385 return ret;
9386}
9387
Ethan Lien3cd24c62018-11-01 14:49:03 +08009388int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009389{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009390 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikb4912132020-07-21 10:22:12 -04009391 u64 nr = U64_MAX;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009392
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009393 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009394 return -EROFS;
9395
Josef Bacikb4912132020-07-21 10:22:12 -04009396 return start_delalloc_inodes(root, &nr, true);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009397}
9398
Josef Bacikb4912132020-07-21 10:22:12 -04009399int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, u64 nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009400{
9401 struct btrfs_root *root;
9402 struct list_head splice;
9403 int ret;
9404
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009405 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009406 return -EROFS;
9407
9408 INIT_LIST_HEAD(&splice);
9409
Miao Xie573bfb72014-03-06 13:55:03 +08009410 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009411 spin_lock(&fs_info->delalloc_root_lock);
9412 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009413 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009414 root = list_first_entry(&splice, struct btrfs_root,
9415 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009416 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009417 BUG_ON(!root);
9418 list_move_tail(&root->delalloc_root,
9419 &fs_info->delalloc_roots);
9420 spin_unlock(&fs_info->delalloc_root_lock);
9421
Josef Bacikb4912132020-07-21 10:22:12 -04009422 ret = start_delalloc_inodes(root, &nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009423 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009424 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009425 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009426 spin_lock(&fs_info->delalloc_root_lock);
9427 }
9428 spin_unlock(&fs_info->delalloc_root_lock);
9429
Miao Xie6c255e62014-03-06 13:55:01 +08009430 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009431out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009432 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009433 spin_lock(&fs_info->delalloc_root_lock);
9434 list_splice_tail(&splice, &fs_info->delalloc_roots);
9435 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009436 }
Miao Xie573bfb72014-03-06 13:55:03 +08009437 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009438 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009439}
9440
Chris Mason39279cc2007-06-12 06:35:45 -04009441static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9442 const char *symname)
9443{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009444 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009445 struct btrfs_trans_handle *trans;
9446 struct btrfs_root *root = BTRFS_I(dir)->root;
9447 struct btrfs_path *path;
9448 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009449 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009450 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009451 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309452 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009453 int name_len;
9454 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009455 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009456 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009457 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009458
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009459 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009460 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009461 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009462
Josef Bacik9ed74f22009-09-11 16:12:44 -04009463 /*
9464 * 2 items for inode item and ref
9465 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009466 * 1 item for updating parent inode item
9467 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009468 * 1 item for xattr if selinux is on
9469 */
Filipe Manana9269d122015-12-31 18:16:29 +00009470 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009471 if (IS_ERR(trans))
9472 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009473
Li Zefan581bb052011-04-20 10:06:11 +08009474 err = btrfs_find_free_ino(root, &objectid);
9475 if (err)
9476 goto out_unlock;
9477
Josef Bacikaec74772008-07-24 12:12:38 -04009478 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009479 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9480 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009481 if (IS_ERR(inode)) {
9482 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009483 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009484 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009485 }
Chris Mason39279cc2007-06-12 06:35:45 -04009486
Casey Schauflerad19db72011-12-15 10:09:07 -05009487 /*
9488 * If the active LSM wants to access the inode during
9489 * d_instantiate it needs these. Smack checks to see
9490 * if the filesystem supports xattrs by looking at the
9491 * ops vector.
9492 */
9493 inode->i_fop = &btrfs_file_operations;
9494 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009495 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009496
9497 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9498 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009499 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009500
Chris Mason39279cc2007-06-12 06:35:45 -04009501 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009502 if (!path) {
9503 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009504 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009505 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009506 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009507 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009508 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009509 datasize = btrfs_file_extent_calc_inline_size(name_len);
9510 err = btrfs_insert_empty_item(trans, root, path, &key,
9511 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009512 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009513 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009514 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009515 }
Chris Mason5f39d392007-10-15 16:14:19 -04009516 leaf = path->nodes[0];
9517 ei = btrfs_item_ptr(leaf, path->slots[0],
9518 struct btrfs_file_extent_item);
9519 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9520 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009521 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009522 btrfs_set_file_extent_encryption(leaf, ei, 0);
9523 btrfs_set_file_extent_compression(leaf, ei, 0);
9524 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9525 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9526
Chris Mason39279cc2007-06-12 06:35:45 -04009527 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009528 write_extent_buffer(leaf, symname, ptr, name_len);
9529 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009530 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009531
Chris Mason39279cc2007-06-12 06:35:45 -04009532 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009533 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009534 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009535 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009536 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009537 /*
9538 * Last step, add directory indexes for our symlink inode. This is the
9539 * last step to avoid extra cleanup of these indexes if an error happens
9540 * elsewhere above.
9541 */
9542 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009543 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9544 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009545 if (err)
9546 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009547
Al Viro1e2e5472018-05-04 08:23:01 -04009548 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009549
9550out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009551 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009552 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009553 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009554 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009555 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009556 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009557 return err;
9558}
Chris Mason16432982008-04-10 10:23:21 -04009559
Filipe Manana8fccebf2020-09-08 11:27:20 +01009560static struct btrfs_trans_handle *insert_prealloc_file_extent(
9561 struct btrfs_trans_handle *trans_in,
Qu Wenruo203f44c52020-06-10 09:04:40 +08009562 struct inode *inode, struct btrfs_key *ins,
9563 u64 file_offset)
9564{
9565 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +01009566 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009567 struct btrfs_trans_handle *trans = trans_in;
9568 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009569 u64 start = ins->objectid;
9570 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009571 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009572
9573 memset(&stack_fi, 0, sizeof(stack_fi));
9574
9575 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9576 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9577 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9578 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9579 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9580 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9581 /* Encryption and other encoding is reserved and all 0 */
9582
Nikolay Borisov72b7d152020-06-03 08:55:18 +03009583 ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len);
Qu Wenruo9729f102020-06-10 09:04:41 +08009584 if (ret < 0)
Filipe Manana8fccebf2020-09-08 11:27:20 +01009585 return ERR_PTR(ret);
9586
9587 if (trans) {
9588 ret = insert_reserved_file_extent(trans, BTRFS_I(inode),
9589 file_offset, &stack_fi, ret);
9590 if (ret)
9591 return ERR_PTR(ret);
9592 return trans;
9593 }
9594
9595 extent_info.disk_offset = start;
9596 extent_info.disk_len = len;
9597 extent_info.data_offset = 0;
9598 extent_info.data_len = len;
9599 extent_info.file_offset = file_offset;
9600 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009601 extent_info.is_new_extent = true;
9602 extent_info.qgroup_reserved = ret;
9603 extent_info.insertions = 0;
9604
9605 path = btrfs_alloc_path();
9606 if (!path)
9607 return ERR_PTR(-ENOMEM);
9608
Filipe Manana306bfec2020-09-08 11:27:23 +01009609 ret = btrfs_replace_file_extents(inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +01009610 file_offset + len - 1, &extent_info,
9611 &trans);
9612 btrfs_free_path(path);
9613 if (ret)
9614 return ERR_PTR(ret);
9615
9616 return trans;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009617}
Filipe Manana8fccebf2020-09-08 11:27:20 +01009618
Josef Bacik0af3d002010-06-21 14:48:16 -04009619static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9620 u64 start, u64 num_bytes, u64 min_size,
9621 loff_t actual_len, u64 *alloc_hint,
9622 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009623{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009624 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009625 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9626 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009627 struct btrfs_root *root = BTRFS_I(inode)->root;
9628 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009629 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009630 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009631 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009632 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009633 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009634 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009635 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009636 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009637
Josef Bacik0af3d002010-06-21 14:48:16 -04009638 if (trans)
9639 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009640 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +09009641 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009642 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009643 /*
9644 * If we are severely fragmented we could end up with really
9645 * small allocations, so if the allocator is returning small
9646 * chunks lets make its job easier by only searching for those
9647 * sized chunks.
9648 */
9649 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009650 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9651 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009652 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -04009653 break;
Josef Bacikb778cf92020-02-13 10:47:31 -05009654
9655 /*
9656 * We've reserved this space, and thus converted it from
9657 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9658 * from here on out we will only need to clear our reservation
9659 * for the remaining unreserved area, so advance our
9660 * clear_offset by our extent size.
9661 */
9662 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009663
Josef Bacik0b670dc2015-09-23 17:11:16 -04009664 last_alloc = ins.offset;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009665 trans = insert_prealloc_file_extent(trans, inode, &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +01009666 /*
9667 * Now that we inserted the prealloc extent we can finally
9668 * decrement the number of reservations in the block group.
9669 * If we did it before, we could race with relocation and have
9670 * relocation miss the reserved extent, making it fail later.
9671 */
9672 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009673 if (IS_ERR(trans)) {
9674 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009675 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009676 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009677 break;
9678 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009679
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009680 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009681 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009682
Josef Bacik5dc562c2012-08-17 13:14:17 -04009683 em = alloc_extent_map();
9684 if (!em) {
9685 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9686 &BTRFS_I(inode)->runtime_flags);
9687 goto next;
9688 }
9689
9690 em->start = cur_offset;
9691 em->orig_start = cur_offset;
9692 em->len = ins.offset;
9693 em->block_start = ins.objectid;
9694 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009695 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009696 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009697 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9698 em->generation = trans->transid;
9699
9700 while (1) {
9701 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009702 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009703 write_unlock(&em_tree->lock);
9704 if (ret != -EEXIST)
9705 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009706 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009707 cur_offset + ins.offset - 1,
9708 0);
9709 }
9710 free_extent_map(em);
9711next:
Yan Zhengd899e052008-10-30 14:25:28 -04009712 num_bytes -= ins.offset;
9713 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009714 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009715
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009716 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009717 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009718 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009719 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009720 (actual_len > inode->i_size) &&
9721 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009722 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009723 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009724 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009725 i_size = cur_offset;
9726 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009727 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009728 }
9729
Yan Zhengd899e052008-10-30 14:25:28 -04009730 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009731
9732 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009733 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009734 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009735 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009736 break;
9737 }
Yan Zhengd899e052008-10-30 14:25:28 -04009738
Filipe Manana8fccebf2020-09-08 11:27:20 +01009739 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009740 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009741 trans = NULL;
9742 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009743 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009744 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03009745 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -05009746 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009747 return ret;
9748}
9749
Josef Bacik0af3d002010-06-21 14:48:16 -04009750int btrfs_prealloc_file_range(struct inode *inode, int mode,
9751 u64 start, u64 num_bytes, u64 min_size,
9752 loff_t actual_len, u64 *alloc_hint)
9753{
9754 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9755 min_size, actual_len, alloc_hint,
9756 NULL);
9757}
9758
9759int btrfs_prealloc_file_range_trans(struct inode *inode,
9760 struct btrfs_trans_handle *trans, int mode,
9761 u64 start, u64 num_bytes, u64 min_size,
9762 loff_t actual_len, u64 *alloc_hint)
9763{
9764 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9765 min_size, actual_len, alloc_hint, trans);
9766}
9767
Chris Masone6dcd2d2008-07-17 12:53:50 -04009768static int btrfs_set_page_dirty(struct page *page)
9769{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009770 return __set_page_dirty_nobuffers(page);
9771}
9772
Al Viro10556cb22011-06-20 19:28:19 -04009773static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009774{
Li Zefanb83cc962010-12-20 16:04:08 +08009775 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009776 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009777
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009778 if (mask & MAY_WRITE &&
9779 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9780 if (btrfs_root_readonly(root))
9781 return -EROFS;
9782 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9783 return -EACCES;
9784 }
Al Viro2830ba72011-06-20 19:16:29 -04009785 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009786}
Chris Mason39279cc2007-06-12 06:35:45 -04009787
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009788static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9789{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009790 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009791 struct btrfs_trans_handle *trans;
9792 struct btrfs_root *root = BTRFS_I(dir)->root;
9793 struct inode *inode = NULL;
9794 u64 objectid;
9795 u64 index;
9796 int ret = 0;
9797
9798 /*
9799 * 5 units required for adding orphan entry
9800 */
9801 trans = btrfs_start_transaction(root, 5);
9802 if (IS_ERR(trans))
9803 return PTR_ERR(trans);
9804
9805 ret = btrfs_find_free_ino(root, &objectid);
9806 if (ret)
9807 goto out;
9808
9809 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009810 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009811 if (IS_ERR(inode)) {
9812 ret = PTR_ERR(inode);
9813 inode = NULL;
9814 goto out;
9815 }
9816
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009817 inode->i_fop = &btrfs_file_operations;
9818 inode->i_op = &btrfs_file_inode_operations;
9819
9820 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009821
Chris Masonb0d5d102014-09-08 13:08:51 -07009822 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9823 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009824 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009825
9826 ret = btrfs_update_inode(trans, root, inode);
9827 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009828 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009829 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009830 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009831 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009832
Filipe Manana5762b5c2014-08-01 00:10:32 +01009833 /*
9834 * We set number of links to 0 in btrfs_new_inode(), and here we set
9835 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9836 * through:
9837 *
9838 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9839 */
9840 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009841 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009842 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009843 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009844out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009845 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009846 if (ret && inode)
9847 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009848 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009849 return ret;
9850}
9851
David Sterba5cdc84b2018-07-18 20:32:52 +02009852void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009853{
David Sterba5cdc84b2018-07-18 20:32:52 +02009854 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009855 unsigned long index = start >> PAGE_SHIFT;
9856 unsigned long end_index = end >> PAGE_SHIFT;
9857 struct page *page;
9858
9859 while (index <= end_index) {
9860 page = find_get_page(inode->i_mapping, index);
9861 ASSERT(page); /* Pages should be in the extent_io_tree */
9862 set_page_writeback(page);
9863 put_page(page);
9864 index++;
9865 }
9866}
9867
Omar Sandovaled46ff32016-11-03 10:28:14 -07009868#ifdef CONFIG_SWAP
9869/*
9870 * Add an entry indicating a block group or device which is pinned by a
9871 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9872 * negative errno on failure.
9873 */
9874static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9875 bool is_block_group)
9876{
9877 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9878 struct btrfs_swapfile_pin *sp, *entry;
9879 struct rb_node **p;
9880 struct rb_node *parent = NULL;
9881
9882 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9883 if (!sp)
9884 return -ENOMEM;
9885 sp->ptr = ptr;
9886 sp->inode = inode;
9887 sp->is_block_group = is_block_group;
9888
9889 spin_lock(&fs_info->swapfile_pins_lock);
9890 p = &fs_info->swapfile_pins.rb_node;
9891 while (*p) {
9892 parent = *p;
9893 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9894 if (sp->ptr < entry->ptr ||
9895 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9896 p = &(*p)->rb_left;
9897 } else if (sp->ptr > entry->ptr ||
9898 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9899 p = &(*p)->rb_right;
9900 } else {
9901 spin_unlock(&fs_info->swapfile_pins_lock);
9902 kfree(sp);
9903 return 1;
9904 }
9905 }
9906 rb_link_node(&sp->node, parent, p);
9907 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9908 spin_unlock(&fs_info->swapfile_pins_lock);
9909 return 0;
9910}
9911
9912/* Free all of the entries pinned by this swapfile. */
9913static void btrfs_free_swapfile_pins(struct inode *inode)
9914{
9915 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9916 struct btrfs_swapfile_pin *sp;
9917 struct rb_node *node, *next;
9918
9919 spin_lock(&fs_info->swapfile_pins_lock);
9920 node = rb_first(&fs_info->swapfile_pins);
9921 while (node) {
9922 next = rb_next(node);
9923 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9924 if (sp->inode == inode) {
9925 rb_erase(&sp->node, &fs_info->swapfile_pins);
9926 if (sp->is_block_group)
9927 btrfs_put_block_group(sp->ptr);
9928 kfree(sp);
9929 }
9930 node = next;
9931 }
9932 spin_unlock(&fs_info->swapfile_pins_lock);
9933}
9934
9935struct btrfs_swap_info {
9936 u64 start;
9937 u64 block_start;
9938 u64 block_len;
9939 u64 lowest_ppage;
9940 u64 highest_ppage;
9941 unsigned long nr_pages;
9942 int nr_extents;
9943};
9944
9945static int btrfs_add_swap_extent(struct swap_info_struct *sis,
9946 struct btrfs_swap_info *bsi)
9947{
9948 unsigned long nr_pages;
9949 u64 first_ppage, first_ppage_reported, next_ppage;
9950 int ret;
9951
9952 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
9953 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
9954 PAGE_SIZE) >> PAGE_SHIFT;
9955
9956 if (first_ppage >= next_ppage)
9957 return 0;
9958 nr_pages = next_ppage - first_ppage;
9959
9960 first_ppage_reported = first_ppage;
9961 if (bsi->start == 0)
9962 first_ppage_reported++;
9963 if (bsi->lowest_ppage > first_ppage_reported)
9964 bsi->lowest_ppage = first_ppage_reported;
9965 if (bsi->highest_ppage < (next_ppage - 1))
9966 bsi->highest_ppage = next_ppage - 1;
9967
9968 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
9969 if (ret < 0)
9970 return ret;
9971 bsi->nr_extents += ret;
9972 bsi->nr_pages += nr_pages;
9973 return 0;
9974}
9975
9976static void btrfs_swap_deactivate(struct file *file)
9977{
9978 struct inode *inode = file_inode(file);
9979
9980 btrfs_free_swapfile_pins(inode);
9981 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
9982}
9983
9984static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
9985 sector_t *span)
9986{
9987 struct inode *inode = file_inode(file);
9988 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9989 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9990 struct extent_state *cached_state = NULL;
9991 struct extent_map *em = NULL;
9992 struct btrfs_device *device = NULL;
9993 struct btrfs_swap_info bsi = {
9994 .lowest_ppage = (sector_t)-1ULL,
9995 };
9996 int ret = 0;
9997 u64 isize;
9998 u64 start;
9999
10000 /*
10001 * If the swap file was just created, make sure delalloc is done. If the
10002 * file changes again after this, the user is doing something stupid and
10003 * we don't really care.
10004 */
10005 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10006 if (ret)
10007 return ret;
10008
10009 /*
10010 * The inode is locked, so these flags won't change after we check them.
10011 */
10012 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10013 btrfs_warn(fs_info, "swapfile must not be compressed");
10014 return -EINVAL;
10015 }
10016 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10017 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10018 return -EINVAL;
10019 }
10020 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10021 btrfs_warn(fs_info, "swapfile must not be checksummed");
10022 return -EINVAL;
10023 }
10024
10025 /*
10026 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010027 * under us. The exclop protection makes sure they aren't running/won't
10028 * run concurrently while we are mapping the swap extents, and
10029 * fs_info->swapfile_pins prevents them from running while the swap
10030 * file is active and moving the extents. Note that this also prevents
10031 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010032 * really worth the trouble to allow it.
10033 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010034 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010035 btrfs_warn(fs_info,
10036 "cannot activate swapfile while exclusive operation is running");
10037 return -EBUSY;
10038 }
10039 /*
10040 * Snapshots can create extents which require COW even if NODATACOW is
10041 * set. We use this counter to prevent snapshots. We must increment it
10042 * before walking the extents because we don't want a concurrent
10043 * snapshot to run after we've already checked the extents.
10044 */
10045 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10046
10047 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10048
10049 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10050 start = 0;
10051 while (start < isize) {
10052 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010053 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010054 u64 len = isize - start;
10055
Omar Sandoval39b07b52019-12-02 17:34:23 -080010056 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010057 if (IS_ERR(em)) {
10058 ret = PTR_ERR(em);
10059 goto out;
10060 }
10061
10062 if (em->block_start == EXTENT_MAP_HOLE) {
10063 btrfs_warn(fs_info, "swapfile must not have holes");
10064 ret = -EINVAL;
10065 goto out;
10066 }
10067 if (em->block_start == EXTENT_MAP_INLINE) {
10068 /*
10069 * It's unlikely we'll ever actually find ourselves
10070 * here, as a file small enough to fit inline won't be
10071 * big enough to store more than the swap header, but in
10072 * case something changes in the future, let's catch it
10073 * here rather than later.
10074 */
10075 btrfs_warn(fs_info, "swapfile must not be inline");
10076 ret = -EINVAL;
10077 goto out;
10078 }
10079 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10080 btrfs_warn(fs_info, "swapfile must not be compressed");
10081 ret = -EINVAL;
10082 goto out;
10083 }
10084
10085 logical_block_start = em->block_start + (start - em->start);
10086 len = min(len, em->len - (start - em->start));
10087 free_extent_map(em);
10088 em = NULL;
10089
Boris Burkova84d5d42020-08-18 11:00:05 -070010090 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010091 if (ret < 0) {
10092 goto out;
10093 } else if (ret) {
10094 ret = 0;
10095 } else {
10096 btrfs_warn(fs_info,
10097 "swapfile must not be copy-on-write");
10098 ret = -EINVAL;
10099 goto out;
10100 }
10101
10102 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10103 if (IS_ERR(em)) {
10104 ret = PTR_ERR(em);
10105 goto out;
10106 }
10107
10108 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10109 btrfs_warn(fs_info,
10110 "swapfile must have single data profile");
10111 ret = -EINVAL;
10112 goto out;
10113 }
10114
10115 if (device == NULL) {
10116 device = em->map_lookup->stripes[0].dev;
10117 ret = btrfs_add_swapfile_pin(inode, device, false);
10118 if (ret == 1)
10119 ret = 0;
10120 else if (ret)
10121 goto out;
10122 } else if (device != em->map_lookup->stripes[0].dev) {
10123 btrfs_warn(fs_info, "swapfile must be on one device");
10124 ret = -EINVAL;
10125 goto out;
10126 }
10127
10128 physical_block_start = (em->map_lookup->stripes[0].physical +
10129 (logical_block_start - em->start));
10130 len = min(len, em->len - (logical_block_start - em->start));
10131 free_extent_map(em);
10132 em = NULL;
10133
10134 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10135 if (!bg) {
10136 btrfs_warn(fs_info,
10137 "could not find block group containing swapfile");
10138 ret = -EINVAL;
10139 goto out;
10140 }
10141
10142 ret = btrfs_add_swapfile_pin(inode, bg, true);
10143 if (ret) {
10144 btrfs_put_block_group(bg);
10145 if (ret == 1)
10146 ret = 0;
10147 else
10148 goto out;
10149 }
10150
10151 if (bsi.block_len &&
10152 bsi.block_start + bsi.block_len == physical_block_start) {
10153 bsi.block_len += len;
10154 } else {
10155 if (bsi.block_len) {
10156 ret = btrfs_add_swap_extent(sis, &bsi);
10157 if (ret)
10158 goto out;
10159 }
10160 bsi.start = start;
10161 bsi.block_start = physical_block_start;
10162 bsi.block_len = len;
10163 }
10164
10165 start += len;
10166 }
10167
10168 if (bsi.block_len)
10169 ret = btrfs_add_swap_extent(sis, &bsi);
10170
10171out:
10172 if (!IS_ERR_OR_NULL(em))
10173 free_extent_map(em);
10174
10175 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10176
10177 if (ret)
10178 btrfs_swap_deactivate(file);
10179
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010180 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010181
10182 if (ret)
10183 return ret;
10184
10185 if (device)
10186 sis->bdev = device->bdev;
10187 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10188 sis->max = bsi.nr_pages;
10189 sis->pages = bsi.nr_pages - 1;
10190 sis->highest_bit = bsi.nr_pages - 1;
10191 return bsi.nr_extents;
10192}
10193#else
10194static void btrfs_swap_deactivate(struct file *file)
10195{
10196}
10197
10198static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10199 sector_t *span)
10200{
10201 return -EOPNOTSUPP;
10202}
10203#endif
10204
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010205static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010206 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010207 .lookup = btrfs_lookup,
10208 .create = btrfs_create,
10209 .unlink = btrfs_unlink,
10210 .link = btrfs_link,
10211 .mkdir = btrfs_mkdir,
10212 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010213 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010214 .symlink = btrfs_symlink,
10215 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010216 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010217 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010218 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010219 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010220 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010221 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010222 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010223};
Yan, Zheng76dda932009-09-21 16:00:26 -040010224
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010225static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010226 .llseek = generic_file_llseek,
10227 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010228 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010229 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010230 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010231#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010232 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010233#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010234 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010235 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010236};
10237
Chris Mason35054392009-01-21 13:11:13 -050010238/*
10239 * btrfs doesn't support the bmap operation because swapfiles
10240 * use bmap to make a mapping of extents in the file. They assume
10241 * these extents won't change over the life of the file and they
10242 * use the bmap result to do IO directly to the drive.
10243 *
10244 * the btrfs bmap call would return logical addresses that aren't
10245 * suitable for IO and they also will change frequently as COW
10246 * operations happen. So, swapfile + btrfs == corruption.
10247 *
10248 * For now we're avoiding this by dropping bmap.
10249 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010250static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010251 .readpage = btrfs_readpage,
10252 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010253 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010254 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010255 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010256 .invalidatepage = btrfs_invalidatepage,
10257 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010258#ifdef CONFIG_MIGRATION
10259 .migratepage = btrfs_migratepage,
10260#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010261 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010262 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010263 .swap_activate = btrfs_swap_activate,
10264 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010265};
10266
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010267static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010268 .getattr = btrfs_getattr,
10269 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010270 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010271 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010272 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010273 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010274 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010275 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010276};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010277static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010278 .getattr = btrfs_getattr,
10279 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010280 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010281 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010282 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010283 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010284 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010285};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010286static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010287 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010288 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010289 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010290 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010291 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010292 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010293};
Yan, Zheng76dda932009-09-21 16:00:26 -040010294
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010295const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010296 .d_delete = btrfs_dentry_delete,
10297};