blob: 6138eea1c5332f854a51c7925fbdee8525fb1dfc [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040044#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080045#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070069static const struct inode_operations btrfs_dir_inode_operations;
70static const struct inode_operations btrfs_symlink_inode_operations;
71static const struct inode_operations btrfs_dir_ro_inode_operations;
72static const struct inode_operations btrfs_special_inode_operations;
73static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070074static const struct address_space_operations btrfs_aops;
75static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070076static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050077static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040078
79static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000080static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081struct kmem_cache *btrfs_trans_handle_cachep;
82struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040083struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050084struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040085
86#define S_SHIFT 12
87static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
88 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
89 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
90 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
91 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
92 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
93 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
94 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
95};
96
Eric Sandeen3972f262013-01-12 02:57:22 +000097static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050098static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040099static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500100static noinline int cow_file_range(struct inode *inode,
101 struct page *locked_page,
102 u64 start, u64 end, int *page_started,
103 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400104static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
105 u64 len, u64 orig_start,
106 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400107 u64 orig_block_len, u64 ram_bytes,
108 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400109
Eric Sandeen48a3b632013-04-25 20:41:01 +0000110static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400111
Josef Bacik6a3891c2015-03-16 17:38:52 -0400112#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
113void btrfs_test_inode_set_ops(struct inode *inode)
114{
115 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
116}
117#endif
118
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000119static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500120 struct inode *inode, struct inode *dir,
121 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500122{
123 int err;
124
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000125 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500126 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500127 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500128 return err;
129}
130
Chris Masond352ac62008-09-29 15:18:18 -0400131/*
Chris Masonc8b97812008-10-29 14:49:59 -0400132 * this does all the hard work for inserting an inline extent into
133 * the btree. The caller should have done a btrfs_drop_extents so that
134 * no overlapping inline items exist in the btree
135 */
Chris Mason40f76582014-05-21 13:35:51 -0700136static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000137 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400138 struct btrfs_root *root, struct inode *inode,
139 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000140 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400141 struct page **compressed_pages)
142{
Chris Masonc8b97812008-10-29 14:49:59 -0400143 struct extent_buffer *leaf;
144 struct page *page = NULL;
145 char *kaddr;
146 unsigned long ptr;
147 struct btrfs_file_extent_item *ei;
148 int err = 0;
149 int ret;
150 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400151 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400152
Li Zefanfe3f5662011-03-28 08:30:38 +0000153 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400154 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400155
Chris Masonc8b97812008-10-29 14:49:59 -0400156 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000157
158 if (!extent_inserted) {
159 struct btrfs_key key;
160 size_t datasize;
161
162 key.objectid = btrfs_ino(inode);
163 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200164 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165
166 datasize = btrfs_file_extent_calc_inline_size(cur_size);
167 path->leave_spinning = 1;
168 ret = btrfs_insert_empty_item(trans, root, path, &key,
169 datasize);
170 if (ret) {
171 err = ret;
172 goto fail;
173 }
Chris Masonc8b97812008-10-29 14:49:59 -0400174 }
175 leaf = path->nodes[0];
176 ei = btrfs_item_ptr(leaf, path->slots[0],
177 struct btrfs_file_extent_item);
178 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
179 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
180 btrfs_set_file_extent_encryption(leaf, ei, 0);
181 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
182 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
183 ptr = btrfs_file_extent_inline_start(ei);
184
Li Zefan261507a02010-12-17 14:21:50 +0800185 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400186 struct page *cpage;
187 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500188 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400189 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500190 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400191 PAGE_CACHE_SIZE);
192
Cong Wang7ac687d2011-11-25 23:14:28 +0800193 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400194 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800195 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400196
197 i++;
198 ptr += cur_size;
199 compressed_size -= cur_size;
200 }
201 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800202 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400203 } else {
204 page = find_get_page(inode->i_mapping,
205 start >> PAGE_CACHE_SHIFT);
206 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800207 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400208 offset = start & (PAGE_CACHE_SIZE - 1);
209 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800210 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400211 page_cache_release(page);
212 }
213 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000214 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400215
Yan, Zhengc2167752009-11-12 09:34:21 +0000216 /*
217 * we're an inline extent, so nobody can
218 * extend the file past i_size without locking
219 * a page we already have locked.
220 *
221 * We must do any isize and inode updates
222 * before we unlock the pages. Otherwise we
223 * could end up racing with unlink.
224 */
Chris Masonc8b97812008-10-29 14:49:59 -0400225 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100226 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000227
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100228 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400229fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400230 return err;
231}
232
233
234/*
235 * conditionally insert an inline extent into the file. This
236 * does the checks required to make sure the data is small enough
237 * to fit as an inline extent.
238 */
Josef Bacik00361582013-08-14 14:02:47 -0400239static noinline int cow_file_range_inline(struct btrfs_root *root,
240 struct inode *inode, u64 start,
241 u64 end, size_t compressed_size,
242 int compress_type,
243 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400244{
Josef Bacik00361582013-08-14 14:02:47 -0400245 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400246 u64 isize = i_size_read(inode);
247 u64 actual_end = min(end + 1, isize);
248 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000249 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400250 u64 data_len = inline_len;
251 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000252 struct btrfs_path *path;
253 int extent_inserted = 0;
254 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400255
256 if (compressed_size)
257 data_len = compressed_size;
258
259 if (start > 0 ||
Wang Shilong354877b2014-07-17 11:44:11 +0800260 actual_end > PAGE_CACHE_SIZE ||
261 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400262 (!compressed_size &&
263 (actual_end & (root->sectorsize - 1)) == 0) ||
264 end + 1 < isize ||
265 data_len > root->fs_info->max_inline) {
266 return 1;
267 }
268
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000269 path = btrfs_alloc_path();
270 if (!path)
271 return -ENOMEM;
272
Josef Bacik00361582013-08-14 14:02:47 -0400273 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000274 if (IS_ERR(trans)) {
275 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400276 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000277 }
Josef Bacik00361582013-08-14 14:02:47 -0400278 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
279
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000280 if (compressed_size && compressed_pages)
281 extent_item_size = btrfs_file_extent_calc_inline_size(
282 compressed_size);
283 else
284 extent_item_size = btrfs_file_extent_calc_inline_size(
285 inline_len);
286
287 ret = __btrfs_drop_extents(trans, root, inode, path,
288 start, aligned_end, NULL,
289 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400290 if (ret) {
291 btrfs_abort_transaction(trans, root, ret);
292 goto out;
293 }
Chris Masonc8b97812008-10-29 14:49:59 -0400294
295 if (isize > actual_end)
296 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000297 ret = insert_inline_extent(trans, path, extent_inserted,
298 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400299 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000300 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400301 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100302 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400303 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400304 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400305 ret = 1;
306 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100307 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400308
Josef Bacikbdc20e62013-02-28 13:23:38 -0500309 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400310 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400311 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400312out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800313 /*
314 * Don't forget to free the reserved space, as for inlined extent
315 * it won't count as data extent, free them directly here.
316 * And at reserve time, it's always aligned to page size, so
317 * just free one page here.
318 */
319 btrfs_qgroup_free_data(inode, 0, PAGE_CACHE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000320 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400321 btrfs_end_transaction(trans, root);
322 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400323}
324
Chris Mason771ed682008-11-06 22:02:51 -0500325struct async_extent {
326 u64 start;
327 u64 ram_size;
328 u64 compressed_size;
329 struct page **pages;
330 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800331 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500332 struct list_head list;
333};
334
335struct async_cow {
336 struct inode *inode;
337 struct btrfs_root *root;
338 struct page *locked_page;
339 u64 start;
340 u64 end;
341 struct list_head extents;
342 struct btrfs_work work;
343};
344
345static noinline int add_async_extent(struct async_cow *cow,
346 u64 start, u64 ram_size,
347 u64 compressed_size,
348 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800349 unsigned long nr_pages,
350 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500351{
352 struct async_extent *async_extent;
353
354 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100355 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500356 async_extent->start = start;
357 async_extent->ram_size = ram_size;
358 async_extent->compressed_size = compressed_size;
359 async_extent->pages = pages;
360 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800361 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500362 list_add_tail(&async_extent->list, &cow->extents);
363 return 0;
364}
365
Wang Shilongf79707b2014-07-17 11:44:09 +0800366static inline int inode_need_compress(struct inode *inode)
367{
368 struct btrfs_root *root = BTRFS_I(inode)->root;
369
370 /* force compress */
371 if (btrfs_test_opt(root, FORCE_COMPRESS))
372 return 1;
373 /* bad compression ratios */
374 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
375 return 0;
376 if (btrfs_test_opt(root, COMPRESS) ||
377 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
378 BTRFS_I(inode)->force_compress)
379 return 1;
380 return 0;
381}
382
Chris Masonc8b97812008-10-29 14:49:59 -0400383/*
Chris Mason771ed682008-11-06 22:02:51 -0500384 * we create compressed extents in two phases. The first
385 * phase compresses a range of pages that have already been
386 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400387 *
Chris Mason771ed682008-11-06 22:02:51 -0500388 * This is done inside an ordered work queue, and the compression
389 * is spread across many cpus. The actual IO submission is step
390 * two, and the ordered work queue takes care of making sure that
391 * happens in the same order things were put onto the queue by
392 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400393 *
Chris Mason771ed682008-11-06 22:02:51 -0500394 * If this code finds it can't get good compression, it puts an
395 * entry onto the work queue to write the uncompressed bytes. This
396 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300397 * are written in the same order that the flusher thread sent them
398 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400399 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100400static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500401 struct page *locked_page,
402 u64 start, u64 end,
403 struct async_cow *async_cow,
404 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400405{
406 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400407 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400408 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400409 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500410 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400411 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400412 struct page **pages = NULL;
413 unsigned long nr_pages;
414 unsigned long nr_pages_ret = 0;
415 unsigned long total_compressed = 0;
416 unsigned long total_in = 0;
417 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500418 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400419 int i;
420 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800421 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400422 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400423
Liu Bo4cb13e52012-03-29 09:57:45 -0400424 /* if this is a small write inside eof, kick off a defrag */
425 if ((end - start + 1) < 16 * 1024 &&
426 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400427 btrfs_add_inode_defrag(NULL, inode);
428
Chris Mason42dc7ba2008-12-15 11:44:56 -0500429 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400430again:
431 will_compress = 0;
432 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
433 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
434
Chris Masonf03d9301f2009-02-04 09:31:06 -0500435 /*
436 * we don't want to send crud past the end of i_size through
437 * compression, that's just a waste of CPU time. So, if the
438 * end of the file is before the start of our current
439 * requested range of bytes, we bail out to the uncompressed
440 * cleanup code that can deal with all of this.
441 *
442 * It isn't really the fastest way to fix things, but this is a
443 * very uncommon corner.
444 */
445 if (actual_end <= start)
446 goto cleanup_and_bail_uncompressed;
447
Chris Masonc8b97812008-10-29 14:49:59 -0400448 total_compressed = actual_end - start;
449
Shilong Wang4bcbb332014-10-07 18:44:35 -0400450 /*
451 * skip compression for a small file range(<=blocksize) that
452 * isn't an inline extent, since it dosen't save disk space at all.
453 */
454 if (total_compressed <= blocksize &&
455 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
456 goto cleanup_and_bail_uncompressed;
457
Chris Masonc8b97812008-10-29 14:49:59 -0400458 /* we want to make sure that amount of ram required to uncompress
459 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500460 * of a compressed extent to 128k. This is a crucial number
461 * because it also controls how easily we can spread reads across
462 * cpus for decompression.
463 *
464 * We also want to make sure the amount of IO required to do
465 * a random read is reasonably small, so we limit the size of
466 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400467 */
468 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000469 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500470 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400471 total_in = 0;
472 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400473
Chris Mason771ed682008-11-06 22:02:51 -0500474 /*
475 * we do compression for mount -o compress and when the
476 * inode has not been flagged as nocompress. This flag can
477 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400478 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800479 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400480 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100481 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800482 if (!pages) {
483 /* just bail out to the uncompressed code */
484 goto cont;
485 }
Chris Mason179e29e2007-11-01 11:28:41 -0400486
Li Zefan261507a02010-12-17 14:21:50 +0800487 if (BTRFS_I(inode)->force_compress)
488 compress_type = BTRFS_I(inode)->force_compress;
489
Chris Mason4adaa612013-03-26 13:07:00 -0400490 /*
491 * we need to call clear_page_dirty_for_io on each
492 * page in the range. Otherwise applications with the file
493 * mmap'd can wander in and change the page contents while
494 * we are compressing them.
495 *
496 * If the compression fails for any reason, we set the pages
497 * dirty again later on.
498 */
499 extent_range_clear_dirty_for_io(inode, start, end);
500 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800501 ret = btrfs_compress_pages(compress_type,
502 inode->i_mapping, start,
503 total_compressed, pages,
504 nr_pages, &nr_pages_ret,
505 &total_in,
506 &total_compressed,
507 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400508
509 if (!ret) {
510 unsigned long offset = total_compressed &
511 (PAGE_CACHE_SIZE - 1);
512 struct page *page = pages[nr_pages_ret - 1];
513 char *kaddr;
514
515 /* zero the tail end of the last page, we might be
516 * sending it down to disk
517 */
518 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800519 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400520 memset(kaddr + offset, 0,
521 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800522 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400523 }
524 will_compress = 1;
525 }
526 }
Li Zefan560f7d72011-09-08 10:22:01 +0800527cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400528 if (start == 0) {
529 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500530 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400531 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500532 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400533 */
Josef Bacik00361582013-08-14 14:02:47 -0400534 ret = cow_file_range_inline(root, inode, start, end,
535 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400536 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500537 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400538 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000539 total_compressed,
540 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400541 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100542 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400543 unsigned long clear_flags = EXTENT_DELALLOC |
544 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100545 unsigned long page_error_op;
546
Josef Bacik151a41b2013-07-29 13:22:24 -0400547 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100548 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400549
Chris Mason771ed682008-11-06 22:02:51 -0500550 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100551 * inline extent creation worked or returned error,
552 * we don't need to create any more async work items.
553 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500554 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400555 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400556 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400557 PAGE_CLEAR_DIRTY |
558 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100559 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400560 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400561 goto free_pages_out;
562 }
563 }
564
565 if (will_compress) {
566 /*
567 * we aren't doing an inline extent round the compressed size
568 * up to a block size boundary so the allocator does sane
569 * things
570 */
Qu Wenruofda28322013-02-26 08:10:22 +0000571 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400572
573 /*
574 * one last check to make sure the compression is really a
575 * win, compare the page count read with the blocks on disk
576 */
Qu Wenruofda28322013-02-26 08:10:22 +0000577 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400578 if (total_compressed >= total_in) {
579 will_compress = 0;
580 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400581 num_bytes = total_in;
582 }
583 }
584 if (!will_compress && pages) {
585 /*
586 * the compression code ran but failed to make things smaller,
587 * free any pages it allocated and our page pointer array
588 */
589 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400590 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400591 page_cache_release(pages[i]);
592 }
593 kfree(pages);
594 pages = NULL;
595 total_compressed = 0;
596 nr_pages_ret = 0;
597
598 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500599 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
600 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500601 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500602 }
Chris Masonc8b97812008-10-29 14:49:59 -0400603 }
Chris Mason771ed682008-11-06 22:02:51 -0500604 if (will_compress) {
605 *num_added += 1;
606
607 /* the async work queues will take care of doing actual
608 * allocation on disk for these compressed pages,
609 * and will submit them to the elevator.
610 */
611 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800612 total_compressed, pages, nr_pages_ret,
613 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500614
Yan, Zheng24ae6362010-12-06 07:02:36 +0000615 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500616 start += num_bytes;
617 pages = NULL;
618 cond_resched();
619 goto again;
620 }
621 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500622cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500623 /*
624 * No compression, but we still need to write the pages in
625 * the file we've been given so far. redirty the locked
626 * page if it corresponds to our extent and set things up
627 * for the async work queue to run cow_file_range to do
628 * the normal delalloc dance
629 */
630 if (page_offset(locked_page) >= start &&
631 page_offset(locked_page) <= end) {
632 __set_page_dirty_nobuffers(locked_page);
633 /* unlocked later on in the async handlers */
634 }
Chris Mason4adaa612013-03-26 13:07:00 -0400635 if (redirty)
636 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800637 add_async_extent(async_cow, start, end - start + 1,
638 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500639 *num_added += 1;
640 }
641
Filipe Mananac44f6492014-10-09 21:15:44 +0100642 return;
Chris Mason771ed682008-11-06 22:02:51 -0500643
644free_pages_out:
645 for (i = 0; i < nr_pages_ret; i++) {
646 WARN_ON(pages[i]->mapping);
647 page_cache_release(pages[i]);
648 }
Chris Masond3977122009-01-05 21:25:51 -0500649 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500650}
Chris Mason771ed682008-11-06 22:02:51 -0500651
Filipe Manana40ae8372014-10-06 22:14:24 +0100652static void free_async_extent_pages(struct async_extent *async_extent)
653{
654 int i;
655
656 if (!async_extent->pages)
657 return;
658
659 for (i = 0; i < async_extent->nr_pages; i++) {
660 WARN_ON(async_extent->pages[i]->mapping);
661 page_cache_release(async_extent->pages[i]);
662 }
663 kfree(async_extent->pages);
664 async_extent->nr_pages = 0;
665 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500666}
667
668/*
669 * phase two of compressed writeback. This is the ordered portion
670 * of the code, which only gets called in the order the work was
671 * queued. We walk all the async extents created by compress_file_range
672 * and send them down to the disk.
673 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100674static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500675 struct async_cow *async_cow)
676{
677 struct async_extent *async_extent;
678 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500679 struct btrfs_key ins;
680 struct extent_map *em;
681 struct btrfs_root *root = BTRFS_I(inode)->root;
682 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
683 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500684 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500685
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500686again:
Chris Masond3977122009-01-05 21:25:51 -0500687 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500688 async_extent = list_entry(async_cow->extents.next,
689 struct async_extent, list);
690 list_del(&async_extent->list);
691
692 io_tree = &BTRFS_I(inode)->io_tree;
693
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500694retry:
Chris Mason771ed682008-11-06 22:02:51 -0500695 /* did the compression code fall back to uncompressed IO? */
696 if (!async_extent->pages) {
697 int page_started = 0;
698 unsigned long nr_written = 0;
699
700 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000701 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100702 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500703
704 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500705 ret = cow_file_range(inode, async_cow->locked_page,
706 async_extent->start,
707 async_extent->start +
708 async_extent->ram_size - 1,
709 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500710
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100711 /* JDM XXX */
712
Chris Mason771ed682008-11-06 22:02:51 -0500713 /*
714 * if page_started, cow_file_range inserted an
715 * inline extent and took care of all the unlocking
716 * and IO for us. Otherwise, we need to submit
717 * all those pages down to the drive.
718 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500719 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500720 extent_write_locked_range(io_tree,
721 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500722 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500723 async_extent->ram_size - 1,
724 btrfs_get_extent,
725 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500726 else if (ret)
727 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500728 kfree(async_extent);
729 cond_resched();
730 continue;
731 }
732
733 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100734 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500735
Josef Bacik00361582013-08-14 14:02:47 -0400736 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500737 async_extent->compressed_size,
738 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800739 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500740 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100741 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500742
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400743 if (ret == -ENOSPC) {
744 unlock_extent(io_tree, async_extent->start,
745 async_extent->start +
746 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800747
748 /*
749 * we need to redirty the pages if we decide to
750 * fallback to uncompressed IO, otherwise we
751 * will not submit these pages down to lower
752 * layers.
753 */
754 extent_range_redirty_for_io(inode,
755 async_extent->start,
756 async_extent->start +
757 async_extent->ram_size - 1);
758
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100759 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400760 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500761 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500762 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000763 /*
764 * here we're doing allocation and writeback of the
765 * compressed pages
766 */
767 btrfs_drop_extent_cache(inode, async_extent->start,
768 async_extent->start +
769 async_extent->ram_size - 1, 0);
770
David Sterba172ddd62011-04-21 00:48:27 +0200771 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000772 if (!em) {
773 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500774 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000775 }
Chris Mason771ed682008-11-06 22:02:51 -0500776 em->start = async_extent->start;
777 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500778 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400779 em->mod_start = em->start;
780 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500781
782 em->block_start = ins.objectid;
783 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500784 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400785 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500786 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800787 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500788 set_bit(EXTENT_FLAG_PINNED, &em->flags);
789 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400790 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500791
Chris Masond3977122009-01-05 21:25:51 -0500792 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400793 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400794 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400795 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500796 if (ret != -EEXIST) {
797 free_extent_map(em);
798 break;
799 }
800 btrfs_drop_extent_cache(inode, async_extent->start,
801 async_extent->start +
802 async_extent->ram_size - 1, 0);
803 }
804
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500805 if (ret)
806 goto out_free_reserve;
807
Li Zefan261507a02010-12-17 14:21:50 +0800808 ret = btrfs_add_ordered_extent_compress(inode,
809 async_extent->start,
810 ins.objectid,
811 async_extent->ram_size,
812 ins.offset,
813 BTRFS_ORDERED_COMPRESSED,
814 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100815 if (ret) {
816 btrfs_drop_extent_cache(inode, async_extent->start,
817 async_extent->start +
818 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500819 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100820 }
Chris Mason771ed682008-11-06 22:02:51 -0500821
Chris Mason771ed682008-11-06 22:02:51 -0500822 /*
823 * clear dirty, set writeback and unlock the pages.
824 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400825 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400826 async_extent->start +
827 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400828 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
829 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400830 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500831 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500832 async_extent->start,
833 async_extent->ram_size,
834 ins.objectid,
835 ins.offset, async_extent->pages,
836 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100837 if (ret) {
838 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
839 struct page *p = async_extent->pages[0];
840 const u64 start = async_extent->start;
841 const u64 end = start + async_extent->ram_size - 1;
842
843 p->mapping = inode->i_mapping;
844 tree->ops->writepage_end_io_hook(p, start, end,
845 NULL, 0);
846 p->mapping = NULL;
847 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
848 PAGE_END_WRITEBACK |
849 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100850 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100851 }
Chris Mason771ed682008-11-06 22:02:51 -0500852 alloc_hint = ins.objectid + ins.offset;
853 kfree(async_extent);
854 cond_resched();
855 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100856 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500857out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800858 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100859out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400860 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500861 async_extent->start +
862 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400863 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400864 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
865 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100866 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
867 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100868 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100869 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500870 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500871}
872
Josef Bacik4b46fce2010-05-23 11:00:55 -0400873static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
874 u64 num_bytes)
875{
876 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
877 struct extent_map *em;
878 u64 alloc_hint = 0;
879
880 read_lock(&em_tree->lock);
881 em = search_extent_mapping(em_tree, start, num_bytes);
882 if (em) {
883 /*
884 * if block start isn't an actual block number then find the
885 * first block in this inode and use that as a hint. If that
886 * block is also bogus then just don't worry about it.
887 */
888 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
889 free_extent_map(em);
890 em = search_extent_mapping(em_tree, 0, 0);
891 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
892 alloc_hint = em->block_start;
893 if (em)
894 free_extent_map(em);
895 } else {
896 alloc_hint = em->block_start;
897 free_extent_map(em);
898 }
899 }
900 read_unlock(&em_tree->lock);
901
902 return alloc_hint;
903}
904
Chris Mason771ed682008-11-06 22:02:51 -0500905/*
906 * when extent_io.c finds a delayed allocation range in the file,
907 * the call backs end up in this code. The basic idea is to
908 * allocate extents on disk for the range, and create ordered data structs
909 * in ram to track those extents.
910 *
911 * locked_page is the page that writepage had locked already. We use
912 * it to make sure we don't do extra locks or unlocks.
913 *
914 * *page_started is set to one if we unlock locked_page and do everything
915 * required to start IO on it. It may be clean and already done with
916 * IO when we return.
917 */
Josef Bacik00361582013-08-14 14:02:47 -0400918static noinline int cow_file_range(struct inode *inode,
919 struct page *locked_page,
920 u64 start, u64 end, int *page_started,
921 unsigned long *nr_written,
922 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500923{
Josef Bacik00361582013-08-14 14:02:47 -0400924 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500925 u64 alloc_hint = 0;
926 u64 num_bytes;
927 unsigned long ram_size;
928 u64 disk_num_bytes;
929 u64 cur_alloc_size;
930 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500931 struct btrfs_key ins;
932 struct extent_map *em;
933 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
934 int ret = 0;
935
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400936 if (btrfs_is_free_space_inode(inode)) {
937 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500938 ret = -EINVAL;
939 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400940 }
Chris Mason771ed682008-11-06 22:02:51 -0500941
Qu Wenruofda28322013-02-26 08:10:22 +0000942 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500943 num_bytes = max(blocksize, num_bytes);
944 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500945
Chris Mason4cb53002011-05-24 15:35:30 -0400946 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400947 if (num_bytes < 64 * 1024 &&
948 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400949 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400950
Chris Mason771ed682008-11-06 22:02:51 -0500951 if (start == 0) {
952 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400953 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
954 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500955 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400956 extent_clear_unlock_delalloc(inode, start, end, NULL,
957 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400958 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400959 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
960 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000961
Chris Mason771ed682008-11-06 22:02:51 -0500962 *nr_written = *nr_written +
963 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
964 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500965 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100967 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500968 }
969 }
Chris Masonc8b97812008-10-29 14:49:59 -0400970
971 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200972 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400973
Josef Bacik4b46fce2010-05-23 11:00:55 -0400974 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400975 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400976
Chris Masond3977122009-01-05 21:25:51 -0500977 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400978 unsigned long op;
979
Josef Bacik287a0ab2010-03-19 18:07:23 +0000980 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400981 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500982 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800983 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400984 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100985 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500986
David Sterba172ddd62011-04-21 00:48:27 +0200987 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000988 if (!em) {
989 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000990 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000991 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400992 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500993 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500994 ram_size = ins.offset;
995 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400996 em->mod_start = em->start;
997 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400998
Chris Masone6dcd2d2008-07-17 12:53:50 -0400999 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001000 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001001 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001002 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001003 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001004 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001005 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001006
Chris Masond3977122009-01-05 21:25:51 -05001007 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001008 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001009 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001010 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001011 if (ret != -EEXIST) {
1012 free_extent_map(em);
1013 break;
1014 }
1015 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001016 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001017 }
Liu Boace68ba2013-04-22 10:53:47 +00001018 if (ret)
1019 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001020
Chris Mason98d20f62008-04-14 09:46:10 -04001021 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001022 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001023 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001024 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001025 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001026
Yan Zheng17d217f2008-12-12 10:03:38 -05001027 if (root->root_key.objectid ==
1028 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1029 ret = btrfs_reloc_clone_csums(inode, start,
1030 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001031 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001032 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001033 }
1034
Chris Masond3977122009-01-05 21:25:51 -05001035 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001036 break;
Chris Masond3977122009-01-05 21:25:51 -05001037
Chris Masonc8b97812008-10-29 14:49:59 -04001038 /* we're not doing compressed IO, don't unlock the first
1039 * page (which the caller expects to stay locked), don't
1040 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001041 *
1042 * Do set the Private2 bit so we know this page was properly
1043 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001044 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001045 op = unlock ? PAGE_UNLOCK : 0;
1046 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001047
Josef Bacikc2790a22013-07-29 11:20:47 -04001048 extent_clear_unlock_delalloc(inode, start,
1049 start + ram_size - 1, locked_page,
1050 EXTENT_LOCKED | EXTENT_DELALLOC,
1051 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001052 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001053 num_bytes -= cur_alloc_size;
1054 alloc_hint = ins.objectid + ins.offset;
1055 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001056 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001057out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001058 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001059
Filipe Mananad9f85962014-08-25 10:43:00 +01001060out_drop_extent_cache:
1061 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001062out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001063 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001064out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001065 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001066 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1067 EXTENT_DELALLOC | EXTENT_DEFRAG,
1068 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1069 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001070 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001071}
Chris Masonc8b97812008-10-29 14:49:59 -04001072
Chris Mason771ed682008-11-06 22:02:51 -05001073/*
1074 * work queue call back to started compression on a file and pages
1075 */
1076static noinline void async_cow_start(struct btrfs_work *work)
1077{
1078 struct async_cow *async_cow;
1079 int num_added = 0;
1080 async_cow = container_of(work, struct async_cow, work);
1081
1082 compress_file_range(async_cow->inode, async_cow->locked_page,
1083 async_cow->start, async_cow->end, async_cow,
1084 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001085 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001086 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001087 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001088 }
Chris Mason771ed682008-11-06 22:02:51 -05001089}
1090
1091/*
1092 * work queue call back to submit previously compressed pages
1093 */
1094static noinline void async_cow_submit(struct btrfs_work *work)
1095{
1096 struct async_cow *async_cow;
1097 struct btrfs_root *root;
1098 unsigned long nr_pages;
1099
1100 async_cow = container_of(work, struct async_cow, work);
1101
1102 root = async_cow->root;
1103 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1104 PAGE_CACHE_SHIFT;
1105
David Sterbaee863952015-02-16 19:41:40 +01001106 /*
1107 * atomic_sub_return implies a barrier for waitqueue_active
1108 */
Josef Bacik66657b32012-08-01 15:36:24 -04001109 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001110 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001111 waitqueue_active(&root->fs_info->async_submit_wait))
1112 wake_up(&root->fs_info->async_submit_wait);
1113
Chris Masond3977122009-01-05 21:25:51 -05001114 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001115 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001116}
Chris Masonc8b97812008-10-29 14:49:59 -04001117
Chris Mason771ed682008-11-06 22:02:51 -05001118static noinline void async_cow_free(struct btrfs_work *work)
1119{
1120 struct async_cow *async_cow;
1121 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001122 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001123 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001124 kfree(async_cow);
1125}
1126
1127static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1128 u64 start, u64 end, int *page_started,
1129 unsigned long *nr_written)
1130{
1131 struct async_cow *async_cow;
1132 struct btrfs_root *root = BTRFS_I(inode)->root;
1133 unsigned long nr_pages;
1134 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001135 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001136
Chris Masona3429ab2009-10-08 12:30:20 -04001137 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1138 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001139 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001140 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001141 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001142 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001143 async_cow->root = root;
1144 async_cow->locked_page = locked_page;
1145 async_cow->start = start;
1146
Wang Shilongf79707b2014-07-17 11:44:09 +08001147 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1148 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001149 cur_end = end;
1150 else
1151 cur_end = min(end, start + 512 * 1024 - 1);
1152
1153 async_cow->end = cur_end;
1154 INIT_LIST_HEAD(&async_cow->extents);
1155
Liu Bo9e0af232014-08-15 23:36:53 +08001156 btrfs_init_work(&async_cow->work,
1157 btrfs_delalloc_helper,
1158 async_cow_start, async_cow_submit,
1159 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001160
Chris Mason771ed682008-11-06 22:02:51 -05001161 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1162 PAGE_CACHE_SHIFT;
1163 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1164
Qu Wenruoafe3d242014-02-28 10:46:07 +08001165 btrfs_queue_work(root->fs_info->delalloc_workers,
1166 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001167
1168 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1169 wait_event(root->fs_info->async_submit_wait,
1170 (atomic_read(&root->fs_info->async_delalloc_pages) <
1171 limit));
1172 }
1173
Chris Masond3977122009-01-05 21:25:51 -05001174 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001175 atomic_read(&root->fs_info->async_delalloc_pages)) {
1176 wait_event(root->fs_info->async_submit_wait,
1177 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1178 0));
1179 }
1180
1181 *nr_written += nr_pages;
1182 start = cur_end + 1;
1183 }
1184 *page_started = 1;
1185 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001186}
1187
Chris Masond3977122009-01-05 21:25:51 -05001188static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001189 u64 bytenr, u64 num_bytes)
1190{
1191 int ret;
1192 struct btrfs_ordered_sum *sums;
1193 LIST_HEAD(list);
1194
Yan Zheng07d400a2009-01-06 11:42:00 -05001195 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001196 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001197 if (ret == 0 && list_empty(&list))
1198 return 0;
1199
1200 while (!list_empty(&list)) {
1201 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1202 list_del(&sums->list);
1203 kfree(sums);
1204 }
1205 return 1;
1206}
1207
Chris Masond352ac62008-09-29 15:18:18 -04001208/*
1209 * when nowcow writeback call back. This checks for snapshots or COW copies
1210 * of the extents that exist in the file, and COWs the file as required.
1211 *
1212 * If no cow copies or snapshots exist, we write directly to the existing
1213 * blocks on disk
1214 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001215static noinline int run_delalloc_nocow(struct inode *inode,
1216 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001217 u64 start, u64 end, int *page_started, int force,
1218 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001219{
Chris Masonbe20aa92007-12-17 20:14:01 -05001220 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001221 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001222 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001223 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001224 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001225 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001226 u64 cow_start;
1227 u64 cur_offset;
1228 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001229 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001230 u64 disk_bytenr;
1231 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001232 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001233 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001234 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001235 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001236 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001237 int nocow;
1238 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001239 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001240 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001241
1242 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001243 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001244 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1245 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001246 EXTENT_DO_ACCOUNTING |
1247 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001248 PAGE_CLEAR_DIRTY |
1249 PAGE_SET_WRITEBACK |
1250 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001251 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001252 }
Li Zefan82d59022011-04-20 10:33:24 +08001253
Liu Bo83eea1f2012-07-10 05:28:39 -06001254 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001255
1256 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001257 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001258 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001259 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001260
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001261 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001262 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1263 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001264 EXTENT_DO_ACCOUNTING |
1265 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001266 PAGE_CLEAR_DIRTY |
1267 PAGE_SET_WRITEBACK |
1268 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001269 btrfs_free_path(path);
1270 return PTR_ERR(trans);
1271 }
1272
Josef Bacik74b21072011-04-13 12:02:53 -04001273 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001274
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 cow_start = (u64)-1;
1276 cur_offset = start;
1277 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001278 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001280 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001281 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1283 leaf = path->nodes[0];
1284 btrfs_item_key_to_cpu(leaf, &found_key,
1285 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001286 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 found_key.type == BTRFS_EXTENT_DATA_KEY)
1288 path->slots[0]--;
1289 }
1290 check_prev = 0;
1291next_slot:
1292 leaf = path->nodes[0];
1293 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1294 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001295 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001296 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 if (ret > 0)
1298 break;
1299 leaf = path->nodes[0];
1300 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001301
Yan Zheng80ff3852008-10-30 14:20:02 -04001302 nocow = 0;
1303 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001304 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001306
Li Zefan33345d012011-04-20 10:31:50 +08001307 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1309 found_key.offset > end)
1310 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001311
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 if (found_key.offset > cur_offset) {
1313 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001314 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 goto out_check;
1316 }
Chris Masonc31f8832008-01-08 15:46:31 -05001317
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 fi = btrfs_item_ptr(leaf, path->slots[0],
1319 struct btrfs_file_extent_item);
1320 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001321
Josef Bacikcc95bef2013-04-04 14:31:27 -04001322 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001323 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1324 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001326 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001327 extent_end = found_key.offset +
1328 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001329 disk_num_bytes =
1330 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 if (extent_end <= start) {
1332 path->slots[0]++;
1333 goto next_slot;
1334 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001335 if (disk_bytenr == 0)
1336 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001337 if (btrfs_file_extent_compression(leaf, fi) ||
1338 btrfs_file_extent_encryption(leaf, fi) ||
1339 btrfs_file_extent_other_encoding(leaf, fi))
1340 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001341 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1342 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001343 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001345 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001346 found_key.offset -
1347 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001348 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001349 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001350 disk_bytenr += cur_offset - found_key.offset;
1351 num_bytes = min(end + 1, extent_end) - cur_offset;
1352 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001353 * if there are pending snapshots for this root,
1354 * we fall into common COW way.
1355 */
1356 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001357 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001358 if (!err)
1359 goto out_check;
1360 }
1361 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001362 * force cow if csum exists in the range.
1363 * this ensure that csum for a given extent are
1364 * either valid or do not exist.
1365 */
1366 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1367 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001368 nocow = 1;
1369 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1370 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001371 btrfs_file_extent_inline_len(leaf,
1372 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 extent_end = ALIGN(extent_end, root->sectorsize);
1374 } else {
1375 BUG_ON(1);
1376 }
1377out_check:
1378 if (extent_end <= start) {
1379 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001380 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001381 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001382 goto next_slot;
1383 }
1384 if (!nocow) {
1385 if (cow_start == (u64)-1)
1386 cow_start = cur_offset;
1387 cur_offset = extent_end;
1388 if (cur_offset > end)
1389 break;
1390 path->slots[0]++;
1391 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001392 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001393
David Sterbab3b4aa72011-04-21 01:20:15 +02001394 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001395 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001396 ret = cow_file_range(inode, locked_page,
1397 cow_start, found_key.offset - 1,
1398 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001399 if (ret) {
1400 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001401 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001402 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001403 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001404 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001405 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001406
Yan Zhengd899e052008-10-30 14:25:28 -04001407 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1408 struct extent_map *em;
1409 struct extent_map_tree *em_tree;
1410 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001411 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001412 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001413 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001414 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001415 em->len = num_bytes;
1416 em->block_len = num_bytes;
1417 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001418 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001419 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001420 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001421 em->mod_start = em->start;
1422 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001423 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001424 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001425 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001426 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001427 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001428 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001429 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001430 if (ret != -EEXIST) {
1431 free_extent_map(em);
1432 break;
1433 }
1434 btrfs_drop_extent_cache(inode, em->start,
1435 em->start + em->len - 1, 0);
1436 }
1437 type = BTRFS_ORDERED_PREALLOC;
1438 } else {
1439 type = BTRFS_ORDERED_NOCOW;
1440 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001441
1442 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001443 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001444 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001445
Yan, Zhengefa56462010-05-16 10:49:59 -04001446 if (root->root_key.objectid ==
1447 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1448 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1449 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001450 if (ret) {
1451 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001452 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001453 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001454 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001455 }
1456
Josef Bacikc2790a22013-07-29 11:20:47 -04001457 extent_clear_unlock_delalloc(inode, cur_offset,
1458 cur_offset + num_bytes - 1,
1459 locked_page, EXTENT_LOCKED |
1460 EXTENT_DELALLOC, PAGE_UNLOCK |
1461 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001462 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001463 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001464 cur_offset = extent_end;
1465 if (cur_offset > end)
1466 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001467 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001468 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001469
Josef Bacik17ca04a2012-05-31 15:58:55 -04001470 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001471 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001472 cur_offset = end;
1473 }
1474
Yan Zheng80ff3852008-10-30 14:20:02 -04001475 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001476 ret = cow_file_range(inode, locked_page, cow_start, end,
1477 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001478 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001479 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001480 }
1481
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001482error:
Miao Xiea698d0752012-09-20 01:51:59 -06001483 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001484 if (!ret)
1485 ret = err;
1486
Josef Bacik17ca04a2012-05-31 15:58:55 -04001487 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001488 extent_clear_unlock_delalloc(inode, cur_offset, end,
1489 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001490 EXTENT_DELALLOC | EXTENT_DEFRAG |
1491 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1492 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001493 PAGE_SET_WRITEBACK |
1494 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001495 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001496 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001497}
1498
Wang Shilong47059d92014-07-03 18:22:07 +08001499static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1500{
1501
1502 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1503 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1504 return 0;
1505
1506 /*
1507 * @defrag_bytes is a hint value, no spinlock held here,
1508 * if is not zero, it means the file is defragging.
1509 * Force cow if given extent needs to be defragged.
1510 */
1511 if (BTRFS_I(inode)->defrag_bytes &&
1512 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1513 EXTENT_DEFRAG, 0, NULL))
1514 return 1;
1515
1516 return 0;
1517}
1518
Chris Masond352ac62008-09-29 15:18:18 -04001519/*
1520 * extent_io.c call back to do delayed allocation processing
1521 */
Chris Masonc8b97812008-10-29 14:49:59 -04001522static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001523 u64 start, u64 end, int *page_started,
1524 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001525{
Chris Masonbe20aa92007-12-17 20:14:01 -05001526 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001527 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001528
Wang Shilong47059d92014-07-03 18:22:07 +08001529 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001530 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001531 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001532 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001533 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001534 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001535 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001536 ret = cow_file_range(inode, locked_page, start, end,
1537 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001538 } else {
1539 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1540 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001541 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001542 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001543 }
Chris Masonb888db22007-08-27 16:49:44 -04001544 return ret;
1545}
1546
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001547static void btrfs_split_extent_hook(struct inode *inode,
1548 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001549{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001550 u64 size;
1551
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001552 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001553 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001554 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001555
Josef Bacikdcab6a32015-02-11 15:08:59 -05001556 size = orig->end - orig->start + 1;
1557 if (size > BTRFS_MAX_EXTENT_SIZE) {
1558 u64 num_extents;
1559 u64 new_size;
1560
1561 /*
Josef Bacikba117212015-03-13 15:01:24 -04001562 * See the explanation in btrfs_merge_extent_hook, the same
1563 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001564 */
1565 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001566 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001567 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001568 new_size = split - orig->start;
1569 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1570 BTRFS_MAX_EXTENT_SIZE);
1571 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1572 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001573 return;
1574 }
1575
Josef Bacik9e0baf62011-07-15 15:16:44 +00001576 spin_lock(&BTRFS_I(inode)->lock);
1577 BTRFS_I(inode)->outstanding_extents++;
1578 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001579}
1580
1581/*
1582 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1583 * extents so we can keep track of new extents that are just merged onto old
1584 * extents, such as when we are doing sequential writes, so we can properly
1585 * account for the metadata space we'll need.
1586 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001587static void btrfs_merge_extent_hook(struct inode *inode,
1588 struct extent_state *new,
1589 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001590{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001591 u64 new_size, old_size;
1592 u64 num_extents;
1593
Josef Bacik9ed74f22009-09-11 16:12:44 -04001594 /* not delalloc, ignore it */
1595 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001596 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001597
Josef Bacik8461a3d2015-03-13 15:12:08 -04001598 if (new->start > other->start)
1599 new_size = new->end - other->start + 1;
1600 else
1601 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001602
1603 /* we're not bigger than the max, unreserve the space and go */
1604 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1605 spin_lock(&BTRFS_I(inode)->lock);
1606 BTRFS_I(inode)->outstanding_extents--;
1607 spin_unlock(&BTRFS_I(inode)->lock);
1608 return;
1609 }
1610
1611 /*
Josef Bacikba117212015-03-13 15:01:24 -04001612 * We have to add up either side to figure out how many extents were
1613 * accounted for before we merged into one big extent. If the number of
1614 * extents we accounted for is <= the amount we need for the new range
1615 * then we can return, otherwise drop. Think of it like this
1616 *
1617 * [ 4k][MAX_SIZE]
1618 *
1619 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1620 * need 2 outstanding extents, on one side we have 1 and the other side
1621 * we have 1 so they are == and we can return. But in this case
1622 *
1623 * [MAX_SIZE+4k][MAX_SIZE+4k]
1624 *
1625 * Each range on their own accounts for 2 extents, but merged together
1626 * they are only 3 extents worth of accounting, so we need to drop in
1627 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001628 */
Josef Bacikba117212015-03-13 15:01:24 -04001629 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001630 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1631 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001632 old_size = new->end - new->start + 1;
1633 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1634 BTRFS_MAX_EXTENT_SIZE);
1635
Josef Bacikdcab6a32015-02-11 15:08:59 -05001636 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001637 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001638 return;
1639
Josef Bacik9e0baf62011-07-15 15:16:44 +00001640 spin_lock(&BTRFS_I(inode)->lock);
1641 BTRFS_I(inode)->outstanding_extents--;
1642 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001643}
1644
Miao Xieeb73c1b2013-05-15 07:48:22 +00001645static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1646 struct inode *inode)
1647{
1648 spin_lock(&root->delalloc_lock);
1649 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1650 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1651 &root->delalloc_inodes);
1652 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1653 &BTRFS_I(inode)->runtime_flags);
1654 root->nr_delalloc_inodes++;
1655 if (root->nr_delalloc_inodes == 1) {
1656 spin_lock(&root->fs_info->delalloc_root_lock);
1657 BUG_ON(!list_empty(&root->delalloc_root));
1658 list_add_tail(&root->delalloc_root,
1659 &root->fs_info->delalloc_roots);
1660 spin_unlock(&root->fs_info->delalloc_root_lock);
1661 }
1662 }
1663 spin_unlock(&root->delalloc_lock);
1664}
1665
1666static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1667 struct inode *inode)
1668{
1669 spin_lock(&root->delalloc_lock);
1670 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1671 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1672 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1673 &BTRFS_I(inode)->runtime_flags);
1674 root->nr_delalloc_inodes--;
1675 if (!root->nr_delalloc_inodes) {
1676 spin_lock(&root->fs_info->delalloc_root_lock);
1677 BUG_ON(list_empty(&root->delalloc_root));
1678 list_del_init(&root->delalloc_root);
1679 spin_unlock(&root->fs_info->delalloc_root_lock);
1680 }
1681 }
1682 spin_unlock(&root->delalloc_lock);
1683}
1684
Chris Masond352ac62008-09-29 15:18:18 -04001685/*
1686 * extent_io.c set_bit_hook, used to track delayed allocation
1687 * bytes in this file, and to maintain the list of inodes that
1688 * have pending delalloc work to be done.
1689 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001690static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001691 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001692{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001693
Wang Shilong47059d92014-07-03 18:22:07 +08001694 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1695 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001696 /*
1697 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001698 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001699 * bit, which is only set or cleared with irqs on
1700 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001701 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001702 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001703 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001704 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001705
Josef Bacik9e0baf62011-07-15 15:16:44 +00001706 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001707 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001708 } else {
1709 spin_lock(&BTRFS_I(inode)->lock);
1710 BTRFS_I(inode)->outstanding_extents++;
1711 spin_unlock(&BTRFS_I(inode)->lock);
1712 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001713
Josef Bacik6a3891c2015-03-16 17:38:52 -04001714 /* For sanity tests */
1715 if (btrfs_test_is_dummy_root(root))
1716 return;
1717
Miao Xie963d6782013-01-29 10:10:51 +00001718 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1719 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001720 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001721 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001722 if (*bits & EXTENT_DEFRAG)
1723 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001724 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001725 &BTRFS_I(inode)->runtime_flags))
1726 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001727 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001728 }
Chris Mason291d6732008-01-29 15:55:23 -05001729}
1730
Chris Masond352ac62008-09-29 15:18:18 -04001731/*
1732 * extent_io.c clear_bit_hook, see set_bit_hook for why
1733 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001734static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001735 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001736 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001737{
Wang Shilong47059d92014-07-03 18:22:07 +08001738 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001739 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1740 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001741
1742 spin_lock(&BTRFS_I(inode)->lock);
1743 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1744 BTRFS_I(inode)->defrag_bytes -= len;
1745 spin_unlock(&BTRFS_I(inode)->lock);
1746
Chris Mason75eff682008-12-15 15:54:40 -05001747 /*
1748 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001749 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001750 * bit, which is only set or cleared with irqs on
1751 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001752 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001753 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001754 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001755
Josef Bacik9e0baf62011-07-15 15:16:44 +00001756 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001757 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001758 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1759 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001760 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001761 spin_unlock(&BTRFS_I(inode)->lock);
1762 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001763
Josef Bacikb6d08f02013-09-27 14:57:43 -04001764 /*
1765 * We don't reserve metadata space for space cache inodes so we
1766 * don't need to call dellalloc_release_metadata if there is an
1767 * error.
1768 */
1769 if (*bits & EXTENT_DO_ACCOUNTING &&
1770 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001771 btrfs_delalloc_release_metadata(inode, len);
1772
Josef Bacik6a3891c2015-03-16 17:38:52 -04001773 /* For sanity tests. */
1774 if (btrfs_test_is_dummy_root(root))
1775 return;
1776
Josef Bacik0cb59c92010-07-02 12:14:14 -04001777 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001778 && do_list && !(state->state & EXTENT_NORESERVE))
Qu Wenruo51773be2015-10-08 18:19:37 +08001779 btrfs_free_reserved_data_space_noquota(inode,
1780 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001781
Miao Xie963d6782013-01-29 10:10:51 +00001782 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1783 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001784 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001785 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001786 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001787 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001788 &BTRFS_I(inode)->runtime_flags))
1789 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001790 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001791 }
Chris Mason291d6732008-01-29 15:55:23 -05001792}
1793
Chris Masond352ac62008-09-29 15:18:18 -04001794/*
1795 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1796 * we don't create bios that span stripes or chunks
1797 */
David Woodhouse64a16702009-07-15 23:29:37 +01001798int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001799 size_t size, struct bio *bio,
1800 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001801{
1802 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001803 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001804 u64 length = 0;
1805 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001806 int ret;
1807
Chris Mason771ed682008-11-06 22:02:51 -05001808 if (bio_flags & EXTENT_BIO_COMPRESSED)
1809 return 0;
1810
Kent Overstreet4f024f32013-10-11 15:44:27 -07001811 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001812 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001813 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001814 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001815 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001816 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001817 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001818 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001819 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001820}
1821
Chris Masond352ac62008-09-29 15:18:18 -04001822/*
1823 * in order to insert checksums into the metadata in large chunks,
1824 * we wait until bio submission time. All the pages in the bio are
1825 * checksummed and sums are attached onto the ordered extent record.
1826 *
1827 * At IO completion time the cums attached on the ordered extent record
1828 * are inserted into the btree
1829 */
Chris Masond3977122009-01-05 21:25:51 -05001830static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1831 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001832 unsigned long bio_flags,
1833 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001834{
Chris Mason065631f2008-02-20 12:07:25 -05001835 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001836 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001837
Chris Masond20f7042008-12-08 16:58:54 -05001838 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001839 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001840 return 0;
1841}
Chris Masone0156402008-04-16 11:15:20 -04001842
Chris Mason4a69a412008-11-06 22:03:00 -05001843/*
1844 * in order to insert checksums into the metadata in large chunks,
1845 * we wait until bio submission time. All the pages in the bio are
1846 * checksummed and sums are attached onto the ordered extent record.
1847 *
1848 * At IO completion time the cums attached on the ordered extent record
1849 * are inserted into the btree
1850 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001851static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001852 int mirror_num, unsigned long bio_flags,
1853 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001854{
1855 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001856 int ret;
1857
1858 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001859 if (ret) {
1860 bio->bi_error = ret;
1861 bio_endio(bio);
1862 }
Stefan Behrens61891922012-11-05 18:51:52 +01001863 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001864}
1865
Chris Masond352ac62008-09-29 15:18:18 -04001866/*
Chris Masoncad321a2008-12-17 14:51:42 -05001867 * extent_io.c submission hook. This does the right thing for csum calculation
1868 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001869 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001870static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001871 int mirror_num, unsigned long bio_flags,
1872 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001873{
1874 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301875 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001876 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001877 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001878 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001879
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001880 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001881
Liu Bo83eea1f2012-07-10 05:28:39 -06001882 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301883 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001884
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001885 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001886 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1887 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001888 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001889
Chris Masond20f7042008-12-08 16:58:54 -05001890 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001891 ret = btrfs_submit_compressed_read(inode, bio,
1892 mirror_num,
1893 bio_flags);
1894 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001895 } else if (!skip_sum) {
1896 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1897 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001898 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001899 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001900 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001901 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001902 /* csum items have already been cloned */
1903 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1904 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001905 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001906 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001907 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001908 bio_flags, bio_offset,
1909 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001910 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001911 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001912 } else if (!skip_sum) {
1913 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1914 if (ret)
1915 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001916 }
1917
Chris Mason0b86a832008-03-24 15:01:56 -04001918mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001919 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1920
1921out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001922 if (ret < 0) {
1923 bio->bi_error = ret;
1924 bio_endio(bio);
1925 }
Stefan Behrens61891922012-11-05 18:51:52 +01001926 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001927}
Chris Mason6885f302008-02-20 16:11:05 -05001928
Chris Masond352ac62008-09-29 15:18:18 -04001929/*
1930 * given a list of ordered sums record them in the inode. This happens
1931 * at IO completion time based on sums calculated at bio submission time.
1932 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001933static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001934 struct inode *inode, u64 file_offset,
1935 struct list_head *list)
1936{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001937 struct btrfs_ordered_sum *sum;
1938
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001939 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001940 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001941 btrfs_csum_file_blocks(trans,
1942 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001943 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001944 }
1945 return 0;
1946}
1947
Josef Bacik2ac55d42010-02-03 19:33:23 +00001948int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1949 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001950{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001951 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001952 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001953 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001954}
1955
Chris Masond352ac62008-09-29 15:18:18 -04001956/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001957struct btrfs_writepage_fixup {
1958 struct page *page;
1959 struct btrfs_work work;
1960};
1961
Christoph Hellwigb2950862008-12-02 09:54:17 -05001962static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001963{
1964 struct btrfs_writepage_fixup *fixup;
1965 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001966 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001967 struct page *page;
1968 struct inode *inode;
1969 u64 page_start;
1970 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001971 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001972
1973 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1974 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001975again:
Chris Mason247e7432008-07-17 12:53:51 -04001976 lock_page(page);
1977 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1978 ClearPageChecked(page);
1979 goto out_page;
1980 }
1981
1982 inode = page->mapping->host;
1983 page_start = page_offset(page);
1984 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1985
Josef Bacik2ac55d42010-02-03 19:33:23 +00001986 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001987 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001988
1989 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001990 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001991 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001992
1993 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1994 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001995 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1996 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001997 unlock_page(page);
1998 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001999 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002000 goto again;
2001 }
Chris Mason247e7432008-07-17 12:53:51 -04002002
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002003 ret = btrfs_delalloc_reserve_space(inode, page_start,
2004 PAGE_CACHE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002005 if (ret) {
2006 mapping_set_error(page->mapping, ret);
2007 end_extent_writepage(page, ret, page_start, page_end);
2008 ClearPageChecked(page);
2009 goto out;
2010 }
2011
Josef Bacik2ac55d42010-02-03 19:33:23 +00002012 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002013 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002014 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002015out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002016 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2017 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002018out_page:
2019 unlock_page(page);
2020 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002021 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002022}
2023
2024/*
2025 * There are a few paths in the higher layers of the kernel that directly
2026 * set the page dirty bit without asking the filesystem if it is a
2027 * good idea. This causes problems because we want to make sure COW
2028 * properly happens and the data=ordered rules are followed.
2029 *
Chris Masonc8b97812008-10-29 14:49:59 -04002030 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002031 * hasn't been properly setup for IO. We kick off an async process
2032 * to fix it up. The async helper will wait for ordered extents, set
2033 * the delalloc bit and make it safe to write the page.
2034 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002035static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002036{
2037 struct inode *inode = page->mapping->host;
2038 struct btrfs_writepage_fixup *fixup;
2039 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002040
Chris Mason8b62b722009-09-02 16:53:46 -04002041 /* this page is properly in the ordered list */
2042 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002043 return 0;
2044
2045 if (PageChecked(page))
2046 return -EAGAIN;
2047
2048 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2049 if (!fixup)
2050 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002051
Chris Mason247e7432008-07-17 12:53:51 -04002052 SetPageChecked(page);
2053 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002054 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2055 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002056 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002057 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002058 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002059}
2060
Yan Zhengd899e052008-10-30 14:25:28 -04002061static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2062 struct inode *inode, u64 file_pos,
2063 u64 disk_bytenr, u64 disk_num_bytes,
2064 u64 num_bytes, u64 ram_bytes,
2065 u8 compression, u8 encryption,
2066 u16 other_encoding, int extent_type)
2067{
2068 struct btrfs_root *root = BTRFS_I(inode)->root;
2069 struct btrfs_file_extent_item *fi;
2070 struct btrfs_path *path;
2071 struct extent_buffer *leaf;
2072 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002073 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002074 int ret;
2075
2076 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002077 if (!path)
2078 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002079
Chris Masona1ed8352009-09-11 12:27:37 -04002080 /*
2081 * we may be replacing one extent in the tree with another.
2082 * The new extent is pinned in the extent map, and we don't want
2083 * to drop it from the cache until it is completely in the btree.
2084 *
2085 * So, tell btrfs_drop_extents to leave this extent in the cache.
2086 * the caller is expected to unpin it and allow it to be merged
2087 * with the others.
2088 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002089 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2090 file_pos + num_bytes, NULL, 0,
2091 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002092 if (ret)
2093 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002094
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002095 if (!extent_inserted) {
2096 ins.objectid = btrfs_ino(inode);
2097 ins.offset = file_pos;
2098 ins.type = BTRFS_EXTENT_DATA_KEY;
2099
2100 path->leave_spinning = 1;
2101 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2102 sizeof(*fi));
2103 if (ret)
2104 goto out;
2105 }
Yan Zhengd899e052008-10-30 14:25:28 -04002106 leaf = path->nodes[0];
2107 fi = btrfs_item_ptr(leaf, path->slots[0],
2108 struct btrfs_file_extent_item);
2109 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2110 btrfs_set_file_extent_type(leaf, fi, extent_type);
2111 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2112 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2113 btrfs_set_file_extent_offset(leaf, fi, 0);
2114 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2115 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2116 btrfs_set_file_extent_compression(leaf, fi, compression);
2117 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2118 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002119
Yan Zhengd899e052008-10-30 14:25:28 -04002120 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002121 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002122
2123 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002124
2125 ins.objectid = disk_bytenr;
2126 ins.offset = disk_num_bytes;
2127 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002128 ret = btrfs_alloc_reserved_file_extent(trans, root,
2129 root->root_key.objectid,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002130 btrfs_ino(inode), file_pos,
2131 ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002132 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002133 * Release the reserved range from inode dirty range map, as it is
2134 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002135 */
2136 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002137out:
Yan Zhengd899e052008-10-30 14:25:28 -04002138 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002139
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002140 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002141}
2142
Liu Bo38c227d2013-01-29 03:18:40 +00002143/* snapshot-aware defrag */
2144struct sa_defrag_extent_backref {
2145 struct rb_node node;
2146 struct old_sa_defrag_extent *old;
2147 u64 root_id;
2148 u64 inum;
2149 u64 file_pos;
2150 u64 extent_offset;
2151 u64 num_bytes;
2152 u64 generation;
2153};
2154
2155struct old_sa_defrag_extent {
2156 struct list_head list;
2157 struct new_sa_defrag_extent *new;
2158
2159 u64 extent_offset;
2160 u64 bytenr;
2161 u64 offset;
2162 u64 len;
2163 int count;
2164};
2165
2166struct new_sa_defrag_extent {
2167 struct rb_root root;
2168 struct list_head head;
2169 struct btrfs_path *path;
2170 struct inode *inode;
2171 u64 file_pos;
2172 u64 len;
2173 u64 bytenr;
2174 u64 disk_len;
2175 u8 compress_type;
2176};
2177
2178static int backref_comp(struct sa_defrag_extent_backref *b1,
2179 struct sa_defrag_extent_backref *b2)
2180{
2181 if (b1->root_id < b2->root_id)
2182 return -1;
2183 else if (b1->root_id > b2->root_id)
2184 return 1;
2185
2186 if (b1->inum < b2->inum)
2187 return -1;
2188 else if (b1->inum > b2->inum)
2189 return 1;
2190
2191 if (b1->file_pos < b2->file_pos)
2192 return -1;
2193 else if (b1->file_pos > b2->file_pos)
2194 return 1;
2195
2196 /*
2197 * [------------------------------] ===> (a range of space)
2198 * |<--->| |<---->| =============> (fs/file tree A)
2199 * |<---------------------------->| ===> (fs/file tree B)
2200 *
2201 * A range of space can refer to two file extents in one tree while
2202 * refer to only one file extent in another tree.
2203 *
2204 * So we may process a disk offset more than one time(two extents in A)
2205 * and locate at the same extent(one extent in B), then insert two same
2206 * backrefs(both refer to the extent in B).
2207 */
2208 return 0;
2209}
2210
2211static void backref_insert(struct rb_root *root,
2212 struct sa_defrag_extent_backref *backref)
2213{
2214 struct rb_node **p = &root->rb_node;
2215 struct rb_node *parent = NULL;
2216 struct sa_defrag_extent_backref *entry;
2217 int ret;
2218
2219 while (*p) {
2220 parent = *p;
2221 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2222
2223 ret = backref_comp(backref, entry);
2224 if (ret < 0)
2225 p = &(*p)->rb_left;
2226 else
2227 p = &(*p)->rb_right;
2228 }
2229
2230 rb_link_node(&backref->node, parent, p);
2231 rb_insert_color(&backref->node, root);
2232}
2233
2234/*
2235 * Note the backref might has changed, and in this case we just return 0.
2236 */
2237static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2238 void *ctx)
2239{
2240 struct btrfs_file_extent_item *extent;
2241 struct btrfs_fs_info *fs_info;
2242 struct old_sa_defrag_extent *old = ctx;
2243 struct new_sa_defrag_extent *new = old->new;
2244 struct btrfs_path *path = new->path;
2245 struct btrfs_key key;
2246 struct btrfs_root *root;
2247 struct sa_defrag_extent_backref *backref;
2248 struct extent_buffer *leaf;
2249 struct inode *inode = new->inode;
2250 int slot;
2251 int ret;
2252 u64 extent_offset;
2253 u64 num_bytes;
2254
2255 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2256 inum == btrfs_ino(inode))
2257 return 0;
2258
2259 key.objectid = root_id;
2260 key.type = BTRFS_ROOT_ITEM_KEY;
2261 key.offset = (u64)-1;
2262
2263 fs_info = BTRFS_I(inode)->root->fs_info;
2264 root = btrfs_read_fs_root_no_name(fs_info, &key);
2265 if (IS_ERR(root)) {
2266 if (PTR_ERR(root) == -ENOENT)
2267 return 0;
2268 WARN_ON(1);
2269 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2270 inum, offset, root_id);
2271 return PTR_ERR(root);
2272 }
2273
2274 key.objectid = inum;
2275 key.type = BTRFS_EXTENT_DATA_KEY;
2276 if (offset > (u64)-1 << 32)
2277 key.offset = 0;
2278 else
2279 key.offset = offset;
2280
2281 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302282 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002283 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002284 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002285
2286 while (1) {
2287 cond_resched();
2288
2289 leaf = path->nodes[0];
2290 slot = path->slots[0];
2291
2292 if (slot >= btrfs_header_nritems(leaf)) {
2293 ret = btrfs_next_leaf(root, path);
2294 if (ret < 0) {
2295 goto out;
2296 } else if (ret > 0) {
2297 ret = 0;
2298 goto out;
2299 }
2300 continue;
2301 }
2302
2303 path->slots[0]++;
2304
2305 btrfs_item_key_to_cpu(leaf, &key, slot);
2306
2307 if (key.objectid > inum)
2308 goto out;
2309
2310 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2311 continue;
2312
2313 extent = btrfs_item_ptr(leaf, slot,
2314 struct btrfs_file_extent_item);
2315
2316 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2317 continue;
2318
Liu Boe68afa42013-07-01 22:13:26 +08002319 /*
2320 * 'offset' refers to the exact key.offset,
2321 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2322 * (key.offset - extent_offset).
2323 */
2324 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002325 continue;
2326
Liu Boe68afa42013-07-01 22:13:26 +08002327 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002328 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002329
Liu Bo38c227d2013-01-29 03:18:40 +00002330 if (extent_offset >= old->extent_offset + old->offset +
2331 old->len || extent_offset + num_bytes <=
2332 old->extent_offset + old->offset)
2333 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002334 break;
2335 }
2336
2337 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2338 if (!backref) {
2339 ret = -ENOENT;
2340 goto out;
2341 }
2342
2343 backref->root_id = root_id;
2344 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002345 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002346 backref->num_bytes = num_bytes;
2347 backref->extent_offset = extent_offset;
2348 backref->generation = btrfs_file_extent_generation(leaf, extent);
2349 backref->old = old;
2350 backref_insert(&new->root, backref);
2351 old->count++;
2352out:
2353 btrfs_release_path(path);
2354 WARN_ON(ret);
2355 return ret;
2356}
2357
2358static noinline bool record_extent_backrefs(struct btrfs_path *path,
2359 struct new_sa_defrag_extent *new)
2360{
2361 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2362 struct old_sa_defrag_extent *old, *tmp;
2363 int ret;
2364
2365 new->path = path;
2366
2367 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002368 ret = iterate_inodes_from_logical(old->bytenr +
2369 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002370 path, record_one_backref,
2371 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002372 if (ret < 0 && ret != -ENOENT)
2373 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002374
2375 /* no backref to be processed for this extent */
2376 if (!old->count) {
2377 list_del(&old->list);
2378 kfree(old);
2379 }
2380 }
2381
2382 if (list_empty(&new->head))
2383 return false;
2384
2385 return true;
2386}
2387
2388static int relink_is_mergable(struct extent_buffer *leaf,
2389 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002390 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002391{
Liu Bo116e0022013-08-02 16:30:40 +08002392 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002393 return 0;
2394
2395 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2396 return 0;
2397
Liu Bo116e0022013-08-02 16:30:40 +08002398 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2399 return 0;
2400
2401 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002402 btrfs_file_extent_other_encoding(leaf, fi))
2403 return 0;
2404
2405 return 1;
2406}
2407
2408/*
2409 * Note the backref might has changed, and in this case we just return 0.
2410 */
2411static noinline int relink_extent_backref(struct btrfs_path *path,
2412 struct sa_defrag_extent_backref *prev,
2413 struct sa_defrag_extent_backref *backref)
2414{
2415 struct btrfs_file_extent_item *extent;
2416 struct btrfs_file_extent_item *item;
2417 struct btrfs_ordered_extent *ordered;
2418 struct btrfs_trans_handle *trans;
2419 struct btrfs_fs_info *fs_info;
2420 struct btrfs_root *root;
2421 struct btrfs_key key;
2422 struct extent_buffer *leaf;
2423 struct old_sa_defrag_extent *old = backref->old;
2424 struct new_sa_defrag_extent *new = old->new;
2425 struct inode *src_inode = new->inode;
2426 struct inode *inode;
2427 struct extent_state *cached = NULL;
2428 int ret = 0;
2429 u64 start;
2430 u64 len;
2431 u64 lock_start;
2432 u64 lock_end;
2433 bool merge = false;
2434 int index;
2435
2436 if (prev && prev->root_id == backref->root_id &&
2437 prev->inum == backref->inum &&
2438 prev->file_pos + prev->num_bytes == backref->file_pos)
2439 merge = true;
2440
2441 /* step 1: get root */
2442 key.objectid = backref->root_id;
2443 key.type = BTRFS_ROOT_ITEM_KEY;
2444 key.offset = (u64)-1;
2445
2446 fs_info = BTRFS_I(src_inode)->root->fs_info;
2447 index = srcu_read_lock(&fs_info->subvol_srcu);
2448
2449 root = btrfs_read_fs_root_no_name(fs_info, &key);
2450 if (IS_ERR(root)) {
2451 srcu_read_unlock(&fs_info->subvol_srcu, index);
2452 if (PTR_ERR(root) == -ENOENT)
2453 return 0;
2454 return PTR_ERR(root);
2455 }
Liu Bo38c227d2013-01-29 03:18:40 +00002456
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002457 if (btrfs_root_readonly(root)) {
2458 srcu_read_unlock(&fs_info->subvol_srcu, index);
2459 return 0;
2460 }
2461
Liu Bo38c227d2013-01-29 03:18:40 +00002462 /* step 2: get inode */
2463 key.objectid = backref->inum;
2464 key.type = BTRFS_INODE_ITEM_KEY;
2465 key.offset = 0;
2466
2467 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2468 if (IS_ERR(inode)) {
2469 srcu_read_unlock(&fs_info->subvol_srcu, index);
2470 return 0;
2471 }
2472
2473 srcu_read_unlock(&fs_info->subvol_srcu, index);
2474
2475 /* step 3: relink backref */
2476 lock_start = backref->file_pos;
2477 lock_end = backref->file_pos + backref->num_bytes - 1;
2478 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2479 0, &cached);
2480
2481 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2482 if (ordered) {
2483 btrfs_put_ordered_extent(ordered);
2484 goto out_unlock;
2485 }
2486
2487 trans = btrfs_join_transaction(root);
2488 if (IS_ERR(trans)) {
2489 ret = PTR_ERR(trans);
2490 goto out_unlock;
2491 }
2492
2493 key.objectid = backref->inum;
2494 key.type = BTRFS_EXTENT_DATA_KEY;
2495 key.offset = backref->file_pos;
2496
2497 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2498 if (ret < 0) {
2499 goto out_free_path;
2500 } else if (ret > 0) {
2501 ret = 0;
2502 goto out_free_path;
2503 }
2504
2505 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2506 struct btrfs_file_extent_item);
2507
2508 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2509 backref->generation)
2510 goto out_free_path;
2511
2512 btrfs_release_path(path);
2513
2514 start = backref->file_pos;
2515 if (backref->extent_offset < old->extent_offset + old->offset)
2516 start += old->extent_offset + old->offset -
2517 backref->extent_offset;
2518
2519 len = min(backref->extent_offset + backref->num_bytes,
2520 old->extent_offset + old->offset + old->len);
2521 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2522
2523 ret = btrfs_drop_extents(trans, root, inode, start,
2524 start + len, 1);
2525 if (ret)
2526 goto out_free_path;
2527again:
2528 key.objectid = btrfs_ino(inode);
2529 key.type = BTRFS_EXTENT_DATA_KEY;
2530 key.offset = start;
2531
Liu Boa09a0a72013-03-11 09:20:58 +00002532 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002533 if (merge) {
2534 struct btrfs_file_extent_item *fi;
2535 u64 extent_len;
2536 struct btrfs_key found_key;
2537
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002538 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002539 if (ret < 0)
2540 goto out_free_path;
2541
2542 path->slots[0]--;
2543 leaf = path->nodes[0];
2544 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2545
2546 fi = btrfs_item_ptr(leaf, path->slots[0],
2547 struct btrfs_file_extent_item);
2548 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2549
Liu Bo116e0022013-08-02 16:30:40 +08002550 if (extent_len + found_key.offset == start &&
2551 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002552 btrfs_set_file_extent_num_bytes(leaf, fi,
2553 extent_len + len);
2554 btrfs_mark_buffer_dirty(leaf);
2555 inode_add_bytes(inode, len);
2556
2557 ret = 1;
2558 goto out_free_path;
2559 } else {
2560 merge = false;
2561 btrfs_release_path(path);
2562 goto again;
2563 }
2564 }
2565
2566 ret = btrfs_insert_empty_item(trans, root, path, &key,
2567 sizeof(*extent));
2568 if (ret) {
2569 btrfs_abort_transaction(trans, root, ret);
2570 goto out_free_path;
2571 }
2572
2573 leaf = path->nodes[0];
2574 item = btrfs_item_ptr(leaf, path->slots[0],
2575 struct btrfs_file_extent_item);
2576 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2577 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2578 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2579 btrfs_set_file_extent_num_bytes(leaf, item, len);
2580 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2581 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2582 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2583 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2584 btrfs_set_file_extent_encryption(leaf, item, 0);
2585 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2586
2587 btrfs_mark_buffer_dirty(leaf);
2588 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002589 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002590
2591 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2592 new->disk_len, 0,
2593 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002594 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002595 if (ret) {
2596 btrfs_abort_transaction(trans, root, ret);
2597 goto out_free_path;
2598 }
2599
2600 ret = 1;
2601out_free_path:
2602 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002603 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002604 btrfs_end_transaction(trans, root);
2605out_unlock:
2606 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2607 &cached, GFP_NOFS);
2608 iput(inode);
2609 return ret;
2610}
2611
Liu Bo6f519562013-10-29 10:45:05 +08002612static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2613{
2614 struct old_sa_defrag_extent *old, *tmp;
2615
2616 if (!new)
2617 return;
2618
2619 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002620 kfree(old);
2621 }
2622 kfree(new);
2623}
2624
Liu Bo38c227d2013-01-29 03:18:40 +00002625static void relink_file_extents(struct new_sa_defrag_extent *new)
2626{
2627 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002628 struct sa_defrag_extent_backref *backref;
2629 struct sa_defrag_extent_backref *prev = NULL;
2630 struct inode *inode;
2631 struct btrfs_root *root;
2632 struct rb_node *node;
2633 int ret;
2634
2635 inode = new->inode;
2636 root = BTRFS_I(inode)->root;
2637
2638 path = btrfs_alloc_path();
2639 if (!path)
2640 return;
2641
2642 if (!record_extent_backrefs(path, new)) {
2643 btrfs_free_path(path);
2644 goto out;
2645 }
2646 btrfs_release_path(path);
2647
2648 while (1) {
2649 node = rb_first(&new->root);
2650 if (!node)
2651 break;
2652 rb_erase(node, &new->root);
2653
2654 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2655
2656 ret = relink_extent_backref(path, prev, backref);
2657 WARN_ON(ret < 0);
2658
2659 kfree(prev);
2660
2661 if (ret == 1)
2662 prev = backref;
2663 else
2664 prev = NULL;
2665 cond_resched();
2666 }
2667 kfree(prev);
2668
2669 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002670out:
Liu Bo6f519562013-10-29 10:45:05 +08002671 free_sa_defrag_extent(new);
2672
Liu Bo38c227d2013-01-29 03:18:40 +00002673 atomic_dec(&root->fs_info->defrag_running);
2674 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002675}
2676
2677static struct new_sa_defrag_extent *
2678record_old_file_extents(struct inode *inode,
2679 struct btrfs_ordered_extent *ordered)
2680{
2681 struct btrfs_root *root = BTRFS_I(inode)->root;
2682 struct btrfs_path *path;
2683 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002684 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002685 struct new_sa_defrag_extent *new;
2686 int ret;
2687
2688 new = kmalloc(sizeof(*new), GFP_NOFS);
2689 if (!new)
2690 return NULL;
2691
2692 new->inode = inode;
2693 new->file_pos = ordered->file_offset;
2694 new->len = ordered->len;
2695 new->bytenr = ordered->start;
2696 new->disk_len = ordered->disk_len;
2697 new->compress_type = ordered->compress_type;
2698 new->root = RB_ROOT;
2699 INIT_LIST_HEAD(&new->head);
2700
2701 path = btrfs_alloc_path();
2702 if (!path)
2703 goto out_kfree;
2704
2705 key.objectid = btrfs_ino(inode);
2706 key.type = BTRFS_EXTENT_DATA_KEY;
2707 key.offset = new->file_pos;
2708
2709 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2710 if (ret < 0)
2711 goto out_free_path;
2712 if (ret > 0 && path->slots[0] > 0)
2713 path->slots[0]--;
2714
2715 /* find out all the old extents for the file range */
2716 while (1) {
2717 struct btrfs_file_extent_item *extent;
2718 struct extent_buffer *l;
2719 int slot;
2720 u64 num_bytes;
2721 u64 offset;
2722 u64 end;
2723 u64 disk_bytenr;
2724 u64 extent_offset;
2725
2726 l = path->nodes[0];
2727 slot = path->slots[0];
2728
2729 if (slot >= btrfs_header_nritems(l)) {
2730 ret = btrfs_next_leaf(root, path);
2731 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002732 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002733 else if (ret > 0)
2734 break;
2735 continue;
2736 }
2737
2738 btrfs_item_key_to_cpu(l, &key, slot);
2739
2740 if (key.objectid != btrfs_ino(inode))
2741 break;
2742 if (key.type != BTRFS_EXTENT_DATA_KEY)
2743 break;
2744 if (key.offset >= new->file_pos + new->len)
2745 break;
2746
2747 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2748
2749 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2750 if (key.offset + num_bytes < new->file_pos)
2751 goto next;
2752
2753 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2754 if (!disk_bytenr)
2755 goto next;
2756
2757 extent_offset = btrfs_file_extent_offset(l, extent);
2758
2759 old = kmalloc(sizeof(*old), GFP_NOFS);
2760 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002761 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002762
2763 offset = max(new->file_pos, key.offset);
2764 end = min(new->file_pos + new->len, key.offset + num_bytes);
2765
2766 old->bytenr = disk_bytenr;
2767 old->extent_offset = extent_offset;
2768 old->offset = offset - key.offset;
2769 old->len = end - offset;
2770 old->new = new;
2771 old->count = 0;
2772 list_add_tail(&old->list, &new->head);
2773next:
2774 path->slots[0]++;
2775 cond_resched();
2776 }
2777
2778 btrfs_free_path(path);
2779 atomic_inc(&root->fs_info->defrag_running);
2780
2781 return new;
2782
Liu Bo38c227d2013-01-29 03:18:40 +00002783out_free_path:
2784 btrfs_free_path(path);
2785out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002786 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002787 return NULL;
2788}
2789
Miao Xiee570fd22014-06-19 10:42:50 +08002790static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2791 u64 start, u64 len)
2792{
2793 struct btrfs_block_group_cache *cache;
2794
2795 cache = btrfs_lookup_block_group(root->fs_info, start);
2796 ASSERT(cache);
2797
2798 spin_lock(&cache->lock);
2799 cache->delalloc_bytes -= len;
2800 spin_unlock(&cache->lock);
2801
2802 btrfs_put_block_group(cache);
2803}
2804
Chris Masond352ac62008-09-29 15:18:18 -04002805/* as ordered data IO finishes, this gets called so we can finish
2806 * an ordered extent if the range of bytes in the file it covers are
2807 * fully written.
2808 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002809static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002810{
Josef Bacik5fd02042012-05-02 14:00:54 -04002811 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002812 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002813 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002814 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002815 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002816 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002817 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002818 int ret = 0;
2819 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002820 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002821 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002822
Liu Bo83eea1f2012-07-10 05:28:39 -06002823 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002824
Josef Bacik5fd02042012-05-02 14:00:54 -04002825 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2826 ret = -EIO;
2827 goto out;
2828 }
2829
Miao Xief6124962014-09-12 18:44:04 +08002830 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2831 ordered_extent->file_offset +
2832 ordered_extent->len - 1);
2833
Josef Bacik77cef2e2013-08-29 13:57:21 -04002834 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2835 truncated = true;
2836 logical_len = ordered_extent->truncated_len;
2837 /* Truncated the entire extent, don't bother adding */
2838 if (!logical_len)
2839 goto out;
2840 }
2841
Yan, Zhengc2167752009-11-12 09:34:21 +00002842 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002843 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002844
2845 /*
2846 * For mwrite(mmap + memset to write) case, we still reserve
2847 * space for NOCOW range.
2848 * As NOCOW won't cause a new delayed ref, just free the space
2849 */
2850 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2851 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002852 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2853 if (nolock)
2854 trans = btrfs_join_transaction_nolock(root);
2855 else
2856 trans = btrfs_join_transaction(root);
2857 if (IS_ERR(trans)) {
2858 ret = PTR_ERR(trans);
2859 trans = NULL;
2860 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002861 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002862 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2863 ret = btrfs_update_inode_fallback(trans, root, inode);
2864 if (ret) /* -ENOMEM or corruption */
2865 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002866 goto out;
2867 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002868
Josef Bacik2ac55d42010-02-03 19:33:23 +00002869 lock_extent_bits(io_tree, ordered_extent->file_offset,
2870 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002871 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002872
Liu Bo38c227d2013-01-29 03:18:40 +00002873 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2874 ordered_extent->file_offset + ordered_extent->len - 1,
2875 EXTENT_DEFRAG, 1, cached_state);
2876 if (ret) {
2877 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002878 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002879 /* the inode is shared */
2880 new = record_old_file_extents(inode, ordered_extent);
2881
2882 clear_extent_bit(io_tree, ordered_extent->file_offset,
2883 ordered_extent->file_offset + ordered_extent->len - 1,
2884 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2885 }
2886
Josef Bacik0cb59c92010-07-02 12:14:14 -04002887 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002888 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002889 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002890 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002891 if (IS_ERR(trans)) {
2892 ret = PTR_ERR(trans);
2893 trans = NULL;
2894 goto out_unlock;
2895 }
Chris Masona79b7d42014-05-22 16:18:52 -07002896
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002897 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002898
Chris Masonc8b97812008-10-29 14:49:59 -04002899 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002900 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002901 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002902 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002903 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002904 ordered_extent->file_offset,
2905 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002906 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002907 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002908 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002909 ret = insert_reserved_file_extent(trans, inode,
2910 ordered_extent->file_offset,
2911 ordered_extent->start,
2912 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002913 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002914 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002915 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002916 if (!ret)
2917 btrfs_release_delalloc_bytes(root,
2918 ordered_extent->start,
2919 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002920 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002921 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2922 ordered_extent->file_offset, ordered_extent->len,
2923 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002924 if (ret < 0) {
2925 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002926 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002927 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002928
Chris Masone6dcd2d2008-07-17 12:53:50 -04002929 add_pending_csums(trans, inode, ordered_extent->file_offset,
2930 &ordered_extent->list);
2931
Josef Bacik6c760c02012-11-09 10:53:21 -05002932 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2933 ret = btrfs_update_inode_fallback(trans, root, inode);
2934 if (ret) { /* -ENOMEM or corruption */
2935 btrfs_abort_transaction(trans, root, ret);
2936 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002937 }
2938 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002939out_unlock:
2940 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2941 ordered_extent->file_offset +
2942 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002943out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002944 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002945 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002946 if (trans)
2947 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002948
Josef Bacik77cef2e2013-08-29 13:57:21 -04002949 if (ret || truncated) {
2950 u64 start, end;
2951
2952 if (truncated)
2953 start = ordered_extent->file_offset + logical_len;
2954 else
2955 start = ordered_extent->file_offset;
2956 end = ordered_extent->file_offset + ordered_extent->len - 1;
2957 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2958
2959 /* Drop the cache for the part of the extent we didn't write. */
2960 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002961
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002962 /*
2963 * If the ordered extent had an IOERR or something else went
2964 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002965 * back to the allocator. We only free the extent in the
2966 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002967 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002968 if ((ret || !logical_len) &&
2969 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002970 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2971 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002972 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002973 }
2974
2975
Josef Bacik5fd02042012-05-02 14:00:54 -04002976 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002977 * This needs to be done to make sure anybody waiting knows we are done
2978 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002979 */
2980 btrfs_remove_ordered_extent(inode, ordered_extent);
2981
Liu Bo38c227d2013-01-29 03:18:40 +00002982 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002983 if (new) {
2984 if (ret) {
2985 free_sa_defrag_extent(new);
2986 atomic_dec(&root->fs_info->defrag_running);
2987 } else {
2988 relink_file_extents(new);
2989 }
2990 }
Liu Bo38c227d2013-01-29 03:18:40 +00002991
Chris Masone6dcd2d2008-07-17 12:53:50 -04002992 /* once for us */
2993 btrfs_put_ordered_extent(ordered_extent);
2994 /* once for the tree */
2995 btrfs_put_ordered_extent(ordered_extent);
2996
Josef Bacik5fd02042012-05-02 14:00:54 -04002997 return ret;
2998}
2999
3000static void finish_ordered_fn(struct btrfs_work *work)
3001{
3002 struct btrfs_ordered_extent *ordered_extent;
3003 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3004 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003005}
3006
Christoph Hellwigb2950862008-12-02 09:54:17 -05003007static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003008 struct extent_state *state, int uptodate)
3009{
Josef Bacik5fd02042012-05-02 14:00:54 -04003010 struct inode *inode = page->mapping->host;
3011 struct btrfs_root *root = BTRFS_I(inode)->root;
3012 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003013 struct btrfs_workqueue *wq;
3014 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003015
liubo1abe9b82011-03-24 11:18:59 +00003016 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3017
Chris Mason8b62b722009-09-02 16:53:46 -04003018 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003019 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3020 end - start + 1, uptodate))
3021 return 0;
3022
Liu Bo9e0af232014-08-15 23:36:53 +08003023 if (btrfs_is_free_space_inode(inode)) {
3024 wq = root->fs_info->endio_freespace_worker;
3025 func = btrfs_freespace_write_helper;
3026 } else {
3027 wq = root->fs_info->endio_write_workers;
3028 func = btrfs_endio_write_helper;
3029 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003030
Liu Bo9e0af232014-08-15 23:36:53 +08003031 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3032 NULL);
3033 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003034
3035 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003036}
3037
Miao Xiedc380ae2014-09-12 18:43:55 +08003038static int __readpage_endio_check(struct inode *inode,
3039 struct btrfs_io_bio *io_bio,
3040 int icsum, struct page *page,
3041 int pgoff, u64 start, size_t len)
3042{
3043 char *kaddr;
3044 u32 csum_expected;
3045 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003046
3047 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3048
3049 kaddr = kmap_atomic(page);
3050 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3051 btrfs_csum_final(csum, (char *)&csum);
3052 if (csum != csum_expected)
3053 goto zeroit;
3054
3055 kunmap_atomic(kaddr);
3056 return 0;
3057zeroit:
David Sterba94647322015-10-08 11:01:36 +02003058 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3059 "csum failed ino %llu off %llu csum %u expected csum %u",
Miao Xiedc380ae2014-09-12 18:43:55 +08003060 btrfs_ino(inode), start, csum, csum_expected);
3061 memset(kaddr + pgoff, 1, len);
3062 flush_dcache_page(page);
3063 kunmap_atomic(kaddr);
3064 if (csum_expected == 0)
3065 return 0;
3066 return -EIO;
3067}
3068
Chris Masond352ac62008-09-29 15:18:18 -04003069/*
Chris Masond352ac62008-09-29 15:18:18 -04003070 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003071 * if there's a match, we allow the bio to finish. If not, the code in
3072 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003073 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003074static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3075 u64 phy_offset, struct page *page,
3076 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003077{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003078 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003079 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003080 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003081 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003082
Chris Masond20f7042008-12-08 16:58:54 -05003083 if (PageChecked(page)) {
3084 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003085 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003086 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003087
3088 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003089 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003090
Yan Zheng17d217f2008-12-12 10:03:38 -05003091 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003092 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05003093 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
3094 GFP_NOFS);
3095 return 0;
3096 }
3097
Miao Xiefacc8a222013-07-25 19:22:34 +08003098 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003099 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3100 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003101}
Chris Masonb888db22007-08-27 16:49:44 -04003102
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003103struct delayed_iput {
3104 struct list_head list;
3105 struct inode *inode;
3106};
3107
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003108/* JDM: If this is fs-wide, why can't we add a pointer to
3109 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003110void btrfs_add_delayed_iput(struct inode *inode)
3111{
3112 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3113 struct delayed_iput *delayed;
3114
3115 if (atomic_add_unless(&inode->i_count, -1, 1))
3116 return;
3117
3118 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3119 delayed->inode = inode;
3120
3121 spin_lock(&fs_info->delayed_iput_lock);
3122 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3123 spin_unlock(&fs_info->delayed_iput_lock);
3124}
3125
3126void btrfs_run_delayed_iputs(struct btrfs_root *root)
3127{
3128 LIST_HEAD(list);
3129 struct btrfs_fs_info *fs_info = root->fs_info;
3130 struct delayed_iput *delayed;
3131 int empty;
3132
3133 spin_lock(&fs_info->delayed_iput_lock);
3134 empty = list_empty(&fs_info->delayed_iputs);
3135 spin_unlock(&fs_info->delayed_iput_lock);
3136 if (empty)
3137 return;
3138
Zhao Leid7c15172015-02-26 10:49:20 +08003139 down_read(&fs_info->delayed_iput_sem);
3140
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003141 spin_lock(&fs_info->delayed_iput_lock);
3142 list_splice_init(&fs_info->delayed_iputs, &list);
3143 spin_unlock(&fs_info->delayed_iput_lock);
3144
3145 while (!list_empty(&list)) {
3146 delayed = list_entry(list.next, struct delayed_iput, list);
3147 list_del(&delayed->list);
3148 iput(delayed->inode);
3149 kfree(delayed);
3150 }
Zhao Leid7c15172015-02-26 10:49:20 +08003151
3152 up_read(&root->fs_info->delayed_iput_sem);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003153}
3154
Yan, Zhengd68fc572010-05-16 10:49:58 -04003155/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003156 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003157 * files in the subvolume, it removes orphan item and frees block_rsv
3158 * structure.
3159 */
3160void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3161 struct btrfs_root *root)
3162{
Josef Bacik90290e12011-12-02 15:44:12 -05003163 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003164 int ret;
3165
Josef Bacik8a35d952012-05-23 14:26:42 -04003166 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003167 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3168 return;
3169
Josef Bacik90290e12011-12-02 15:44:12 -05003170 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003171 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003172 spin_unlock(&root->orphan_lock);
3173 return;
3174 }
3175
3176 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3177 spin_unlock(&root->orphan_lock);
3178 return;
3179 }
3180
3181 block_rsv = root->orphan_block_rsv;
3182 root->orphan_block_rsv = NULL;
3183 spin_unlock(&root->orphan_lock);
3184
Miao Xie27cdeb72014-04-02 19:51:05 +08003185 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003186 btrfs_root_refs(&root->root_item) > 0) {
3187 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3188 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003189 if (ret)
3190 btrfs_abort_transaction(trans, root, ret);
3191 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003192 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3193 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003194 }
3195
Josef Bacik90290e12011-12-02 15:44:12 -05003196 if (block_rsv) {
3197 WARN_ON(block_rsv->size > 0);
3198 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003199 }
3200}
3201
3202/*
Josef Bacik7b128762008-07-24 12:17:14 -04003203 * This creates an orphan entry for the given inode in case something goes
3204 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003205 *
3206 * NOTE: caller of this function should reserve 5 units of metadata for
3207 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003208 */
3209int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3210{
3211 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003212 struct btrfs_block_rsv *block_rsv = NULL;
3213 int reserve = 0;
3214 int insert = 0;
3215 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003216
Yan, Zhengd68fc572010-05-16 10:49:58 -04003217 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003218 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003219 if (!block_rsv)
3220 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003221 }
3222
Yan, Zhengd68fc572010-05-16 10:49:58 -04003223 spin_lock(&root->orphan_lock);
3224 if (!root->orphan_block_rsv) {
3225 root->orphan_block_rsv = block_rsv;
3226 } else if (block_rsv) {
3227 btrfs_free_block_rsv(root, block_rsv);
3228 block_rsv = NULL;
3229 }
Josef Bacik7b128762008-07-24 12:17:14 -04003230
Josef Bacik8a35d952012-05-23 14:26:42 -04003231 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3232 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003233#if 0
3234 /*
3235 * For proper ENOSPC handling, we should do orphan
3236 * cleanup when mounting. But this introduces backward
3237 * compatibility issue.
3238 */
3239 if (!xchg(&root->orphan_item_inserted, 1))
3240 insert = 2;
3241 else
3242 insert = 1;
3243#endif
3244 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003245 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003246 }
Josef Bacik7b128762008-07-24 12:17:14 -04003247
Josef Bacik72ac3c02012-05-23 14:13:11 -04003248 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3249 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003250 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003251 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003252
Yan, Zhengd68fc572010-05-16 10:49:58 -04003253 /* grab metadata reservation from transaction handle */
3254 if (reserve) {
3255 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003256 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003257 }
3258
3259 /* insert an orphan item to track this unlinked/truncated file */
3260 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003261 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003262 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003263 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003264 if (reserve) {
3265 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3266 &BTRFS_I(inode)->runtime_flags);
3267 btrfs_orphan_release_metadata(inode);
3268 }
3269 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003270 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3271 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003272 btrfs_abort_transaction(trans, root, ret);
3273 return ret;
3274 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003275 }
3276 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 }
3278
3279 /* insert an orphan item to track subvolume contains orphan files */
3280 if (insert >= 2) {
3281 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3282 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003283 if (ret && ret != -EEXIST) {
3284 btrfs_abort_transaction(trans, root, ret);
3285 return ret;
3286 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003287 }
3288 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003289}
3290
3291/*
3292 * We have done the truncate/delete so we can go ahead and remove the orphan
3293 * item for this particular inode.
3294 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003295static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3296 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003297{
3298 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003299 int delete_item = 0;
3300 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003301 int ret = 0;
3302
Yan, Zhengd68fc572010-05-16 10:49:58 -04003303 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003304 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3305 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003306 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003307
Josef Bacik72ac3c02012-05-23 14:13:11 -04003308 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3309 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003310 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003311 spin_unlock(&root->orphan_lock);
3312
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003313 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003314 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003315 if (trans)
3316 ret = btrfs_del_orphan_item(trans, root,
3317 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003318 }
Josef Bacik7b128762008-07-24 12:17:14 -04003319
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003320 if (release_rsv)
3321 btrfs_orphan_release_metadata(inode);
3322
Josef Bacik4ef31a42013-08-13 14:10:08 -04003323 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003324}
3325
3326/*
3327 * this cleans up any orphans that may be left on the list from the last use
3328 * of this root.
3329 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003330int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003331{
3332 struct btrfs_path *path;
3333 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003334 struct btrfs_key key, found_key;
3335 struct btrfs_trans_handle *trans;
3336 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003337 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003338 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3339
Yan, Zhengd68fc572010-05-16 10:49:58 -04003340 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003341 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003342
3343 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003344 if (!path) {
3345 ret = -ENOMEM;
3346 goto out;
3347 }
Josef Bacik7b128762008-07-24 12:17:14 -04003348 path->reada = -1;
3349
3350 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003351 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003352 key.offset = (u64)-1;
3353
Josef Bacik7b128762008-07-24 12:17:14 -04003354 while (1) {
3355 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003356 if (ret < 0)
3357 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003358
3359 /*
3360 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003361 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003362 * find the key and see if we have stuff that matches
3363 */
3364 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003365 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003366 if (path->slots[0] == 0)
3367 break;
3368 path->slots[0]--;
3369 }
3370
3371 /* pull out the item */
3372 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003373 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3374
3375 /* make sure the item matches what we want */
3376 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3377 break;
David Sterba962a2982014-06-04 18:41:45 +02003378 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003379 break;
3380
3381 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003382 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003383
3384 /*
3385 * this is where we are basically btrfs_lookup, without the
3386 * crossing root thing. we store the inode number in the
3387 * offset of the orphan item.
3388 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003389
3390 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003391 btrfs_err(root->fs_info,
3392 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003393 ret = -EINVAL;
3394 goto out;
3395 }
3396
3397 last_objectid = found_key.offset;
3398
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003399 found_key.objectid = found_key.offset;
3400 found_key.type = BTRFS_INODE_ITEM_KEY;
3401 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003402 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303403 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003404 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003405 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003406
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003407 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3408 struct btrfs_root *dead_root;
3409 struct btrfs_fs_info *fs_info = root->fs_info;
3410 int is_dead_root = 0;
3411
3412 /*
3413 * this is an orphan in the tree root. Currently these
3414 * could come from 2 sources:
3415 * a) a snapshot deletion in progress
3416 * b) a free space cache inode
3417 * We need to distinguish those two, as the snapshot
3418 * orphan must not get deleted.
3419 * find_dead_roots already ran before us, so if this
3420 * is a snapshot deletion, we should find the root
3421 * in the dead_roots list
3422 */
3423 spin_lock(&fs_info->trans_lock);
3424 list_for_each_entry(dead_root, &fs_info->dead_roots,
3425 root_list) {
3426 if (dead_root->root_key.objectid ==
3427 found_key.objectid) {
3428 is_dead_root = 1;
3429 break;
3430 }
3431 }
3432 spin_unlock(&fs_info->trans_lock);
3433 if (is_dead_root) {
3434 /* prevent this orphan from being found again */
3435 key.offset = found_key.objectid - 1;
3436 continue;
3437 }
3438 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003439 /*
3440 * Inode is already gone but the orphan item is still there,
3441 * kill the orphan item.
3442 */
3443 if (ret == -ESTALE) {
3444 trans = btrfs_start_transaction(root, 1);
3445 if (IS_ERR(trans)) {
3446 ret = PTR_ERR(trans);
3447 goto out;
3448 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003449 btrfs_debug(root->fs_info, "auto deleting %Lu",
3450 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003451 ret = btrfs_del_orphan_item(trans, root,
3452 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003453 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003454 if (ret)
3455 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003456 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003457 }
Josef Bacik7b128762008-07-24 12:17:14 -04003458
Josef Bacik7b128762008-07-24 12:17:14 -04003459 /*
3460 * add this inode to the orphan list so btrfs_orphan_del does
3461 * the proper thing when we hit it
3462 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003463 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3464 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003465 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003466
Josef Bacik7b128762008-07-24 12:17:14 -04003467 /* if we have links, this was a truncate, lets do that */
3468 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303469 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003470 iput(inode);
3471 continue;
3472 }
Josef Bacik7b128762008-07-24 12:17:14 -04003473 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003474
3475 /* 1 for the orphan item deletion. */
3476 trans = btrfs_start_transaction(root, 1);
3477 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003478 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003479 ret = PTR_ERR(trans);
3480 goto out;
3481 }
3482 ret = btrfs_orphan_add(trans, inode);
3483 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003484 if (ret) {
3485 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003486 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003487 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003488
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003489 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003490 if (ret)
3491 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003492 } else {
3493 nr_unlink++;
3494 }
3495
3496 /* this will do delete_inode and everything for us */
3497 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003498 if (ret)
3499 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003500 }
Miao Xie3254c872011-11-10 20:45:05 -05003501 /* release the path since we're done with it */
3502 btrfs_release_path(path);
3503
Yan, Zhengd68fc572010-05-16 10:49:58 -04003504 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3505
3506 if (root->orphan_block_rsv)
3507 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3508 (u64)-1);
3509
Miao Xie27cdeb72014-04-02 19:51:05 +08003510 if (root->orphan_block_rsv ||
3511 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003512 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003513 if (!IS_ERR(trans))
3514 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003515 }
Josef Bacik7b128762008-07-24 12:17:14 -04003516
3517 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003518 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003519 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003520 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003521
3522out:
3523 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003524 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003525 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003526 btrfs_free_path(path);
3527 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003528}
3529
Chris Masond352ac62008-09-29 15:18:18 -04003530/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003531 * very simple check to peek ahead in the leaf looking for xattrs. If we
3532 * don't find any xattrs, we know there can't be any acls.
3533 *
3534 * slot is the slot the inode is in, objectid is the objectid of the inode
3535 */
3536static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003537 int slot, u64 objectid,
3538 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003539{
3540 u32 nritems = btrfs_header_nritems(leaf);
3541 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003542 static u64 xattr_access = 0;
3543 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003544 int scanned = 0;
3545
Josef Bacikf23b5a52013-06-19 10:16:26 -04003546 if (!xattr_access) {
3547 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3548 strlen(POSIX_ACL_XATTR_ACCESS));
3549 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3550 strlen(POSIX_ACL_XATTR_DEFAULT));
3551 }
3552
Chris Mason46a53cc2009-04-27 11:47:50 -04003553 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003554 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003555 while (slot < nritems) {
3556 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3557
3558 /* we found a different objectid, there must not be acls */
3559 if (found_key.objectid != objectid)
3560 return 0;
3561
3562 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003563 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003564 if (*first_xattr_slot == -1)
3565 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003566 if (found_key.offset == xattr_access ||
3567 found_key.offset == xattr_default)
3568 return 1;
3569 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003570
3571 /*
3572 * we found a key greater than an xattr key, there can't
3573 * be any acls later on
3574 */
3575 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3576 return 0;
3577
3578 slot++;
3579 scanned++;
3580
3581 /*
3582 * it goes inode, inode backrefs, xattrs, extents,
3583 * so if there are a ton of hard links to an inode there can
3584 * be a lot of backrefs. Don't waste time searching too hard,
3585 * this is just an optimization
3586 */
3587 if (scanned >= 8)
3588 break;
3589 }
3590 /* we hit the end of the leaf before we found an xattr or
3591 * something larger than an xattr. We have to assume the inode
3592 * has acls
3593 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003594 if (*first_xattr_slot == -1)
3595 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003596 return 1;
3597}
3598
3599/*
Chris Masond352ac62008-09-29 15:18:18 -04003600 * read an inode from the btree into the in-memory inode
3601 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003602static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003603{
3604 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003605 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003606 struct btrfs_inode_item *inode_item;
3607 struct btrfs_root *root = BTRFS_I(inode)->root;
3608 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003609 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003610 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003611 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003612 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003613 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003614 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003615
3616 ret = btrfs_fill_inode(inode, &rdev);
3617 if (!ret)
3618 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003619
3620 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003621 if (!path)
3622 goto make_bad;
3623
Chris Mason39279cc2007-06-12 06:35:45 -04003624 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003625
Chris Mason39279cc2007-06-12 06:35:45 -04003626 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003627 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003628 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003629
Chris Mason5f39d392007-10-15 16:14:19 -04003630 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003631
3632 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003633 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003634
Chris Mason5f39d392007-10-15 16:14:19 -04003635 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3636 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003637 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003638 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003639 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3640 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003641 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003642
David Sterbaa937b972014-12-12 17:39:12 +01003643 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3644 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003645
David Sterbaa937b972014-12-12 17:39:12 +01003646 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3647 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003648
David Sterbaa937b972014-12-12 17:39:12 +01003649 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3650 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003651
chandan r9cc97d62012-07-04 12:48:07 +05303652 BTRFS_I(inode)->i_otime.tv_sec =
3653 btrfs_timespec_sec(leaf, &inode_item->otime);
3654 BTRFS_I(inode)->i_otime.tv_nsec =
3655 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003656
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003657 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003658 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003659 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3660
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003661 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003662 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003663 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003664 rdev = btrfs_inode_rdev(leaf, inode_item);
3665
Josef Bacikaec74772008-07-24 12:12:38 -04003666 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003667 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003668
3669cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003670 /*
3671 * If we were modified in the current generation and evicted from memory
3672 * and then re-read we need to do a full sync since we don't have any
3673 * idea about which extents were modified before we were evicted from
3674 * cache.
3675 *
3676 * This is required for both inode re-read from disk and delayed inode
3677 * in delayed_nodes_tree.
3678 */
3679 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3680 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3681 &BTRFS_I(inode)->runtime_flags);
3682
Filipe Mananabde6c242015-07-24 00:00:19 +01003683 /*
3684 * We don't persist the id of the transaction where an unlink operation
3685 * against the inode was last made. So here we assume the inode might
3686 * have been evicted, and therefore the exact value of last_unlink_trans
3687 * lost, and set it to last_trans to avoid metadata inconsistencies
3688 * between the inode and its parent if the inode is fsync'ed and the log
3689 * replayed. For example, in the scenario:
3690 *
3691 * touch mydir/foo
3692 * ln mydir/foo mydir/bar
3693 * sync
3694 * unlink mydir/bar
3695 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3696 * xfs_io -c fsync mydir/foo
3697 * <power failure>
3698 * mount fs, triggers fsync log replay
3699 *
3700 * We must make sure that when we fsync our inode foo we also log its
3701 * parent inode, otherwise after log replay the parent still has the
3702 * dentry with the "bar" name but our inode foo has a link count of 1
3703 * and doesn't have an inode ref with the name "bar" anymore.
3704 *
3705 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3706 * but it guarantees correctness at the expense of ocassional full
3707 * transaction commits on fsync if our inode is a directory, or if our
3708 * inode is not a directory, logging its parent unnecessarily.
3709 */
3710 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3711
Miao Xie67de1172013-12-26 13:07:06 +08003712 path->slots[0]++;
3713 if (inode->i_nlink != 1 ||
3714 path->slots[0] >= btrfs_header_nritems(leaf))
3715 goto cache_acl;
3716
3717 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3718 if (location.objectid != btrfs_ino(inode))
3719 goto cache_acl;
3720
3721 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3722 if (location.type == BTRFS_INODE_REF_KEY) {
3723 struct btrfs_inode_ref *ref;
3724
3725 ref = (struct btrfs_inode_ref *)ptr;
3726 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3727 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3728 struct btrfs_inode_extref *extref;
3729
3730 extref = (struct btrfs_inode_extref *)ptr;
3731 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3732 extref);
3733 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003734cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003735 /*
3736 * try to precache a NULL acl entry for files that don't have
3737 * any xattrs or acls
3738 */
Li Zefan33345d012011-04-20 10:31:50 +08003739 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003740 btrfs_ino(inode), &first_xattr_slot);
3741 if (first_xattr_slot != -1) {
3742 path->slots[0] = first_xattr_slot;
3743 ret = btrfs_load_inode_props(inode, path);
3744 if (ret)
3745 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003746 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003747 btrfs_ino(inode),
3748 root->root_key.objectid, ret);
3749 }
3750 btrfs_free_path(path);
3751
Al Viro72c04902009-06-24 16:58:48 -04003752 if (!maybe_acls)
3753 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003754
Chris Mason39279cc2007-06-12 06:35:45 -04003755 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003756 case S_IFREG:
3757 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003758 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003759 inode->i_fop = &btrfs_file_operations;
3760 inode->i_op = &btrfs_file_inode_operations;
3761 break;
3762 case S_IFDIR:
3763 inode->i_fop = &btrfs_dir_file_operations;
3764 if (root == root->fs_info->tree_root)
3765 inode->i_op = &btrfs_dir_ro_inode_operations;
3766 else
3767 inode->i_op = &btrfs_dir_inode_operations;
3768 break;
3769 case S_IFLNK:
3770 inode->i_op = &btrfs_symlink_inode_operations;
3771 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3772 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003773 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003774 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003775 init_special_inode(inode, inode->i_mode, rdev);
3776 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003777 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003778
3779 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003780 return;
3781
3782make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003783 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003784 make_bad_inode(inode);
3785}
3786
Chris Masond352ac62008-09-29 15:18:18 -04003787/*
3788 * given a leaf and an inode, copy the inode fields into the leaf
3789 */
Chris Masone02119d2008-09-05 16:13:11 -04003790static void fill_inode_item(struct btrfs_trans_handle *trans,
3791 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003792 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003793 struct inode *inode)
3794{
Liu Bo51fab692012-12-27 09:01:21 +00003795 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003796
Liu Bo51fab692012-12-27 09:01:21 +00003797 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003798
Liu Bo51fab692012-12-27 09:01:21 +00003799 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3800 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3801 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3802 &token);
3803 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3804 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003805
David Sterbaa937b972014-12-12 17:39:12 +01003806 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003807 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003808 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003809 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003810
David Sterbaa937b972014-12-12 17:39:12 +01003811 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003812 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003813 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003814 inode->i_mtime.tv_nsec, &token);
3815
David Sterbaa937b972014-12-12 17:39:12 +01003816 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003817 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003818 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003819 inode->i_ctime.tv_nsec, &token);
3820
chandan r9cc97d62012-07-04 12:48:07 +05303821 btrfs_set_token_timespec_sec(leaf, &item->otime,
3822 BTRFS_I(inode)->i_otime.tv_sec, &token);
3823 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3824 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3825
Liu Bo51fab692012-12-27 09:01:21 +00003826 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3827 &token);
3828 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3829 &token);
3830 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3831 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3832 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3833 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3834 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003835}
3836
Chris Masond352ac62008-09-29 15:18:18 -04003837/*
3838 * copy everything in the in-memory inode into the btree.
3839 */
Chris Mason21151332011-11-10 20:39:08 -05003840static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003841 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003842{
3843 struct btrfs_inode_item *inode_item;
3844 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003845 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003846 int ret;
3847
3848 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003849 if (!path)
3850 return -ENOMEM;
3851
Chris Masonb9473432009-03-13 11:00:37 -04003852 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003853 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3854 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003855 if (ret) {
3856 if (ret > 0)
3857 ret = -ENOENT;
3858 goto failed;
3859 }
3860
Chris Mason5f39d392007-10-15 16:14:19 -04003861 leaf = path->nodes[0];
3862 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003863 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003864
Chris Masone02119d2008-09-05 16:13:11 -04003865 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003866 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003867 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003868 ret = 0;
3869failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003870 btrfs_free_path(path);
3871 return ret;
3872}
3873
Chris Masond352ac62008-09-29 15:18:18 -04003874/*
Chris Mason21151332011-11-10 20:39:08 -05003875 * copy everything in the in-memory inode into the btree.
3876 */
3877noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3878 struct btrfs_root *root, struct inode *inode)
3879{
3880 int ret;
3881
3882 /*
3883 * If the inode is a free space inode, we can deadlock during commit
3884 * if we put it into the delayed code.
3885 *
3886 * The data relocation inode should also be directly updated
3887 * without delay
3888 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003889 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003890 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3891 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003892 btrfs_update_root_times(trans, root);
3893
Chris Mason21151332011-11-10 20:39:08 -05003894 ret = btrfs_delayed_update_inode(trans, root, inode);
3895 if (!ret)
3896 btrfs_set_inode_last_trans(trans, inode);
3897 return ret;
3898 }
3899
3900 return btrfs_update_inode_item(trans, root, inode);
3901}
3902
Josef Bacikbe6aef62012-10-22 15:43:12 -04003903noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3904 struct btrfs_root *root,
3905 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003906{
3907 int ret;
3908
3909 ret = btrfs_update_inode(trans, root, inode);
3910 if (ret == -ENOSPC)
3911 return btrfs_update_inode_item(trans, root, inode);
3912 return ret;
3913}
3914
3915/*
Chris Masond352ac62008-09-29 15:18:18 -04003916 * unlink helper that gets used here in inode.c and in the tree logging
3917 * recovery code. It remove a link in a directory with a given name, and
3918 * also drops the back refs in the inode to the directory
3919 */
Al Viro92986792011-03-04 17:14:37 +00003920static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3921 struct btrfs_root *root,
3922 struct inode *dir, struct inode *inode,
3923 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003924{
3925 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003926 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003927 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003928 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003929 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003930 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003931 u64 ino = btrfs_ino(inode);
3932 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003933
3934 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003935 if (!path) {
3936 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003937 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003938 }
3939
Chris Masonb9473432009-03-13 11:00:37 -04003940 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003941 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003942 name, name_len, -1);
3943 if (IS_ERR(di)) {
3944 ret = PTR_ERR(di);
3945 goto err;
3946 }
3947 if (!di) {
3948 ret = -ENOENT;
3949 goto err;
3950 }
Chris Mason5f39d392007-10-15 16:14:19 -04003951 leaf = path->nodes[0];
3952 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003953 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003954 if (ret)
3955 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003956 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003957
Miao Xie67de1172013-12-26 13:07:06 +08003958 /*
3959 * If we don't have dir index, we have to get it by looking up
3960 * the inode ref, since we get the inode ref, remove it directly,
3961 * it is unnecessary to do delayed deletion.
3962 *
3963 * But if we have dir index, needn't search inode ref to get it.
3964 * Since the inode ref is close to the inode item, it is better
3965 * that we delay to delete it, and just do this deletion when
3966 * we update the inode item.
3967 */
3968 if (BTRFS_I(inode)->dir_index) {
3969 ret = btrfs_delayed_delete_inode_ref(inode);
3970 if (!ret) {
3971 index = BTRFS_I(inode)->dir_index;
3972 goto skip_backref;
3973 }
3974 }
3975
Li Zefan33345d012011-04-20 10:31:50 +08003976 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3977 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003978 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003979 btrfs_info(root->fs_info,
3980 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003981 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003982 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003983 goto err;
3984 }
Miao Xie67de1172013-12-26 13:07:06 +08003985skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003986 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003987 if (ret) {
3988 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003989 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003990 }
Chris Mason39279cc2007-06-12 06:35:45 -04003991
Chris Masone02119d2008-09-05 16:13:11 -04003992 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003993 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003994 if (ret != 0 && ret != -ENOENT) {
3995 btrfs_abort_transaction(trans, root, ret);
3996 goto err;
3997 }
Chris Masone02119d2008-09-05 16:13:11 -04003998
3999 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
4000 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04004001 if (ret == -ENOENT)
4002 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004003 else if (ret)
4004 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004005err:
4006 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004007 if (ret)
4008 goto out;
4009
4010 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004011 inode_inc_iversion(inode);
4012 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04004013 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06004014 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004015out:
Chris Mason39279cc2007-06-12 06:35:45 -04004016 return ret;
4017}
4018
Al Viro92986792011-03-04 17:14:37 +00004019int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4020 struct btrfs_root *root,
4021 struct inode *dir, struct inode *inode,
4022 const char *name, int name_len)
4023{
4024 int ret;
4025 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4026 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004027 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004028 ret = btrfs_update_inode(trans, root, inode);
4029 }
4030 return ret;
4031}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004032
4033/*
4034 * helper to start transaction for unlink and rmdir.
4035 *
Josef Bacikd52be812013-05-29 14:54:47 -04004036 * unlink and rmdir are special in btrfs, they do not always free space, so
4037 * if we cannot make our reservations the normal way try and see if there is
4038 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4039 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004040 */
Josef Bacikd52be812013-05-29 14:54:47 -04004041static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004042{
4043 struct btrfs_trans_handle *trans;
4044 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004045 int ret;
4046
Josef Bacike70bea52011-10-11 14:18:24 -04004047 /*
4048 * 1 for the possible orphan item
4049 * 1 for the dir item
4050 * 1 for the dir index
4051 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004052 * 1 for the inode
4053 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04004054 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004055 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
4056 return trans;
4057
Josef Bacikd52be812013-05-29 14:54:47 -04004058 if (PTR_ERR(trans) == -ENOSPC) {
4059 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004060
Josef Bacikd52be812013-05-29 14:54:47 -04004061 trans = btrfs_start_transaction(root, 0);
4062 if (IS_ERR(trans))
4063 return trans;
4064 ret = btrfs_cond_migrate_bytes(root->fs_info,
4065 &root->fs_info->trans_block_rsv,
4066 num_bytes, 5);
4067 if (ret) {
4068 btrfs_end_transaction(trans, root);
4069 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004070 }
Josef Bacik5a77d762011-11-01 14:32:23 -04004071 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04004072 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004073 }
Josef Bacikd52be812013-05-29 14:54:47 -04004074 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004075}
4076
Chris Mason39279cc2007-06-12 06:35:45 -04004077static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4078{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004079 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004080 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004081 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004082 int ret;
4083
Josef Bacikd52be812013-05-29 14:54:47 -04004084 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004085 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004086 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004087
David Howells2b0143b2015-03-17 22:25:59 +00004088 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004089
David Howells2b0143b2015-03-17 22:25:59 +00004090 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004091 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004092 if (ret)
4093 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004094
Yan, Zhenga22285a2010-05-16 10:48:46 -04004095 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004096 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004097 if (ret)
4098 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004099 }
Josef Bacik7b128762008-07-24 12:17:14 -04004100
Tsutomu Itohb5324022011-07-19 07:27:20 +00004101out:
Josef Bacikd52be812013-05-29 14:54:47 -04004102 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004103 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004104 return ret;
4105}
4106
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004107int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4108 struct btrfs_root *root,
4109 struct inode *dir, u64 objectid,
4110 const char *name, int name_len)
4111{
4112 struct btrfs_path *path;
4113 struct extent_buffer *leaf;
4114 struct btrfs_dir_item *di;
4115 struct btrfs_key key;
4116 u64 index;
4117 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004118 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004119
4120 path = btrfs_alloc_path();
4121 if (!path)
4122 return -ENOMEM;
4123
Li Zefan33345d012011-04-20 10:31:50 +08004124 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004125 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004126 if (IS_ERR_OR_NULL(di)) {
4127 if (!di)
4128 ret = -ENOENT;
4129 else
4130 ret = PTR_ERR(di);
4131 goto out;
4132 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004133
4134 leaf = path->nodes[0];
4135 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4136 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4137 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004138 if (ret) {
4139 btrfs_abort_transaction(trans, root, ret);
4140 goto out;
4141 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004142 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004143
4144 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4145 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004146 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004147 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004148 if (ret != -ENOENT) {
4149 btrfs_abort_transaction(trans, root, ret);
4150 goto out;
4151 }
Li Zefan33345d012011-04-20 10:31:50 +08004152 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004153 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004154 if (IS_ERR_OR_NULL(di)) {
4155 if (!di)
4156 ret = -ENOENT;
4157 else
4158 ret = PTR_ERR(di);
4159 btrfs_abort_transaction(trans, root, ret);
4160 goto out;
4161 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004162
4163 leaf = path->nodes[0];
4164 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004165 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004166 index = key.offset;
4167 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004168 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004169
Miao Xie16cdcec2011-04-22 18:12:22 +08004170 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004171 if (ret) {
4172 btrfs_abort_transaction(trans, root, ret);
4173 goto out;
4174 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004175
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004176 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004177 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004178 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004179 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004180 if (ret)
4181 btrfs_abort_transaction(trans, root, ret);
4182out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004183 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004184 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004185}
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;
Chris Mason39279cc2007-06-12 06:35:45 -04004193
David Sterbab3ae2442012-09-13 16:04:34 -06004194 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004195 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004196 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4197 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004198
Josef Bacikd52be812013-05-29 14:54:47 -04004199 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004200 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004201 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004202
Li Zefan33345d012011-04-20 10:31:50 +08004203 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004204 err = btrfs_unlink_subvol(trans, root, dir,
4205 BTRFS_I(inode)->location.objectid,
4206 dentry->d_name.name,
4207 dentry->d_name.len);
4208 goto out;
4209 }
4210
Josef Bacik7b128762008-07-24 12:17:14 -04004211 err = btrfs_orphan_add(trans, inode);
4212 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004213 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004214
Chris Mason39279cc2007-06-12 06:35:45 -04004215 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004216 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004217 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004218 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004219 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004220out:
Josef Bacikd52be812013-05-29 14:54:47 -04004221 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004222 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004223
Chris Mason39279cc2007-06-12 06:35:45 -04004224 return err;
4225}
4226
Chris Mason28f75a02015-02-04 06:59:29 -08004227static int truncate_space_check(struct btrfs_trans_handle *trans,
4228 struct btrfs_root *root,
4229 u64 bytes_deleted)
4230{
4231 int ret;
4232
4233 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
4234 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4235 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4236 if (!ret)
4237 trans->bytes_reserved += bytes_deleted;
4238 return ret;
4239
4240}
4241
Filipe Manana0305cd52015-10-16 12:34:25 +01004242static int truncate_inline_extent(struct inode *inode,
4243 struct btrfs_path *path,
4244 struct btrfs_key *found_key,
4245 const u64 item_end,
4246 const u64 new_size)
4247{
4248 struct extent_buffer *leaf = path->nodes[0];
4249 int slot = path->slots[0];
4250 struct btrfs_file_extent_item *fi;
4251 u32 size = (u32)(new_size - found_key->offset);
4252 struct btrfs_root *root = BTRFS_I(inode)->root;
4253
4254 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4255
4256 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4257 loff_t offset = new_size;
4258 loff_t page_end = ALIGN(offset, PAGE_CACHE_SIZE);
4259
4260 /*
4261 * Zero out the remaining of the last page of our inline extent,
4262 * instead of directly truncating our inline extent here - that
4263 * would be much more complex (decompressing all the data, then
4264 * compressing the truncated data, which might be bigger than
4265 * the size of the inline extent, resize the extent, etc).
4266 * We release the path because to get the page we might need to
4267 * read the extent item from disk (data not in the page cache).
4268 */
4269 btrfs_release_path(path);
4270 return btrfs_truncate_page(inode, offset, page_end - offset, 0);
4271 }
4272
4273 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4274 size = btrfs_file_extent_calc_inline_size(size);
4275 btrfs_truncate_item(root, path, size, 1);
4276
4277 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4278 inode_sub_bytes(inode, item_end + 1 - new_size);
4279
4280 return 0;
4281}
4282
Chris Mason323ac952008-10-01 19:05:46 -04004283/*
Chris Mason39279cc2007-06-12 06:35:45 -04004284 * this can truncate away extent items, csum items and directory items.
4285 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004286 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004287 *
4288 * csum items that cross the new i_size are truncated to the new size
4289 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004290 *
4291 * min_type is the minimum key type to truncate down to. If set to 0, this
4292 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004293 */
Yan, Zheng80825102009-11-12 09:35:36 +00004294int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4295 struct btrfs_root *root,
4296 struct inode *inode,
4297 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004298{
Chris Mason39279cc2007-06-12 06:35:45 -04004299 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004300 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004301 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004302 struct btrfs_key key;
4303 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004304 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004305 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004306 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004307 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004308 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004309 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004310 int found_extent;
4311 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004312 int pending_del_nr = 0;
4313 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004314 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004315 int ret;
4316 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004317 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004318 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004319 bool be_nice = 0;
4320 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004321 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004322
4323 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004324
Chris Mason28ed1342014-12-17 09:41:04 -08004325 /*
4326 * for non-free space inodes and ref cows, we want to back off from
4327 * time to time
4328 */
4329 if (!btrfs_is_free_space_inode(inode) &&
4330 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4331 be_nice = 1;
4332
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004333 path = btrfs_alloc_path();
4334 if (!path)
4335 return -ENOMEM;
4336 path->reada = -1;
4337
Josef Bacik5dc562c2012-08-17 13:14:17 -04004338 /*
4339 * We want to drop from the next block forward in case this new size is
4340 * not block aligned since we will be keeping the last block of the
4341 * extent just the way it is.
4342 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004343 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4344 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004345 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4346 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004347
Miao Xie16cdcec2011-04-22 18:12:22 +08004348 /*
4349 * This function is also used to drop the items in the log tree before
4350 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4351 * it is used to drop the loged items. So we shouldn't kill the delayed
4352 * items.
4353 */
4354 if (min_type == 0 && root == BTRFS_I(inode)->root)
4355 btrfs_kill_delayed_inode_items(inode);
4356
Li Zefan33345d012011-04-20 10:31:50 +08004357 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004358 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004359 key.type = (u8)-1;
4360
Chris Mason85e21ba2008-01-29 15:11:36 -05004361search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004362 /*
4363 * with a 16K leaf size and 128MB extents, you can actually queue
4364 * up a huge file in a single leaf. Most of the time that
4365 * bytes_deleted is > 0, it will be huge by the time we get here
4366 */
4367 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
4368 if (btrfs_should_end_transaction(trans, root)) {
4369 err = -EAGAIN;
4370 goto error;
4371 }
4372 }
4373
4374
Chris Masonb9473432009-03-13 11:00:37 -04004375 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004376 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004377 if (ret < 0) {
4378 err = ret;
4379 goto out;
4380 }
Chris Masond3977122009-01-05 21:25:51 -05004381
Chris Mason85e21ba2008-01-29 15:11:36 -05004382 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004383 /* there are no items in the tree for us to truncate, we're
4384 * done
4385 */
Yan, Zheng80825102009-11-12 09:35:36 +00004386 if (path->slots[0] == 0)
4387 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004388 path->slots[0]--;
4389 }
4390
Chris Masond3977122009-01-05 21:25:51 -05004391 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004392 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004393 leaf = path->nodes[0];
4394 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004395 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004396
Li Zefan33345d012011-04-20 10:31:50 +08004397 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004398 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004399
Chris Mason85e21ba2008-01-29 15:11:36 -05004400 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004401 break;
4402
Chris Mason5f39d392007-10-15 16:14:19 -04004403 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004404 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004405 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004406 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004407 extent_type = btrfs_file_extent_type(leaf, fi);
4408 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004409 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004410 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004411 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004412 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004413 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004414 }
Yan008630c2007-11-07 13:31:09 -05004415 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004416 }
Yan, Zheng80825102009-11-12 09:35:36 +00004417 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004418 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004419 } else {
4420 if (item_end < new_size)
4421 break;
4422 if (found_key.offset >= new_size)
4423 del_item = 1;
4424 else
4425 del_item = 0;
4426 }
Chris Mason39279cc2007-06-12 06:35:45 -04004427 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004428 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004429 if (found_type != BTRFS_EXTENT_DATA_KEY)
4430 goto delete;
4431
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004432 if (del_item)
4433 last_size = found_key.offset;
4434 else
4435 last_size = new_size;
4436
Chris Mason179e29e2007-11-01 11:28:41 -04004437 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004438 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004439 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004440 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004441 u64 orig_num_bytes =
4442 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004443 extent_num_bytes = ALIGN(new_size -
4444 found_key.offset,
4445 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004446 btrfs_set_file_extent_num_bytes(leaf, fi,
4447 extent_num_bytes);
4448 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004449 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004450 if (test_bit(BTRFS_ROOT_REF_COWS,
4451 &root->state) &&
4452 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004453 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004454 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004455 } else {
Chris Masondb945352007-10-15 16:15:53 -04004456 extent_num_bytes =
4457 btrfs_file_extent_disk_num_bytes(leaf,
4458 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004459 extent_offset = found_key.offset -
4460 btrfs_file_extent_offset(leaf, fi);
4461
Chris Mason39279cc2007-06-12 06:35:45 -04004462 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004463 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004464 if (extent_start != 0) {
4465 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004466 if (test_bit(BTRFS_ROOT_REF_COWS,
4467 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004468 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004469 }
4470 }
Chris Mason90692182008-02-08 13:49:28 -05004471 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004472 /*
4473 * we can't truncate inline items that have had
4474 * special encodings
4475 */
4476 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004477 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4478 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004479
4480 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004481 * Need to release path in order to truncate a
4482 * compressed extent. So delete any accumulated
4483 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004484 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004485 if (btrfs_file_extent_compression(leaf, fi) !=
4486 BTRFS_COMPRESS_NONE && pending_del_nr) {
4487 err = btrfs_del_items(trans, root, path,
4488 pending_del_slot,
4489 pending_del_nr);
4490 if (err) {
4491 btrfs_abort_transaction(trans,
4492 root,
4493 err);
4494 goto error;
4495 }
4496 pending_del_nr = 0;
4497 }
4498
4499 err = truncate_inline_extent(inode, path,
4500 &found_key,
4501 item_end,
4502 new_size);
4503 if (err) {
4504 btrfs_abort_transaction(trans,
4505 root, err);
4506 goto error;
4507 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004508 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4509 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004510 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004511 }
Chris Mason39279cc2007-06-12 06:35:45 -04004512 }
Chris Mason179e29e2007-11-01 11:28:41 -04004513delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004514 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004515 if (!pending_del_nr) {
4516 /* no pending yet, add ourselves */
4517 pending_del_slot = path->slots[0];
4518 pending_del_nr = 1;
4519 } else if (pending_del_nr &&
4520 path->slots[0] + 1 == pending_del_slot) {
4521 /* hop on the pending chunk */
4522 pending_del_nr++;
4523 pending_del_slot = path->slots[0];
4524 } else {
Chris Masond3977122009-01-05 21:25:51 -05004525 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004526 }
Chris Mason39279cc2007-06-12 06:35:45 -04004527 } else {
4528 break;
4529 }
Chris Mason28f75a02015-02-04 06:59:29 -08004530 should_throttle = 0;
4531
Miao Xie27cdeb72014-04-02 19:51:05 +08004532 if (found_extent &&
4533 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4534 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004535 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004536 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004537 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004538 extent_num_bytes, 0,
4539 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004540 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004541 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004542 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004543 btrfs_async_run_delayed_refs(root,
4544 trans->delayed_ref_updates * 2, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004545 if (be_nice) {
4546 if (truncate_space_check(trans, root,
4547 extent_num_bytes)) {
4548 should_end = 1;
4549 }
4550 if (btrfs_should_throttle_delayed_refs(trans,
4551 root)) {
4552 should_throttle = 1;
4553 }
4554 }
Chris Mason39279cc2007-06-12 06:35:45 -04004555 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004556
Yan, Zheng80825102009-11-12 09:35:36 +00004557 if (found_type == BTRFS_INODE_ITEM_KEY)
4558 break;
4559
4560 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004561 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004562 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004563 if (pending_del_nr) {
4564 ret = btrfs_del_items(trans, root, path,
4565 pending_del_slot,
4566 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004567 if (ret) {
4568 btrfs_abort_transaction(trans,
4569 root, ret);
4570 goto error;
4571 }
Yan, Zheng80825102009-11-12 09:35:36 +00004572 pending_del_nr = 0;
4573 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004574 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004575 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004576 unsigned long updates = trans->delayed_ref_updates;
4577 if (updates) {
4578 trans->delayed_ref_updates = 0;
4579 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4580 if (ret && !err)
4581 err = ret;
4582 }
4583 }
Chris Mason28f75a02015-02-04 06:59:29 -08004584 /*
4585 * if we failed to refill our space rsv, bail out
4586 * and let the transaction restart
4587 */
4588 if (should_end) {
4589 err = -EAGAIN;
4590 goto error;
4591 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004592 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004593 } else {
4594 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004595 }
Chris Mason39279cc2007-06-12 06:35:45 -04004596 }
Yan, Zheng80825102009-11-12 09:35:36 +00004597out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004598 if (pending_del_nr) {
4599 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4600 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004601 if (ret)
4602 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004603 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004604error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004605 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004606 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004607
Chris Mason39279cc2007-06-12 06:35:45 -04004608 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004609
Chris Mason28f75a02015-02-04 06:59:29 -08004610 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
Chris Mason28ed1342014-12-17 09:41:04 -08004611 unsigned long updates = trans->delayed_ref_updates;
4612 if (updates) {
4613 trans->delayed_ref_updates = 0;
4614 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4615 if (ret && !err)
4616 err = ret;
4617 }
4618 }
Yan, Zheng80825102009-11-12 09:35:36 +00004619 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004620}
4621
Chris Masona52d9a82007-08-27 16:49:44 -04004622/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004623 * btrfs_truncate_page - read, zero a chunk and write a page
4624 * @inode - inode that we're zeroing
4625 * @from - the offset to start zeroing
4626 * @len - the length to zero, 0 to zero the entire range respective to the
4627 * offset
4628 * @front - zero up to the offset instead of from the offset on
4629 *
4630 * This will find the page for the "from" offset and cow the page and zero the
4631 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004632 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004633int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4634 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004635{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004636 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004637 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004638 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4639 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004640 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004641 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004642 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004643 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4644 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4645 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004646 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004647 int ret = 0;
4648 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004649 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004650
Josef Bacik2aaa6652012-08-29 14:27:18 -04004651 if ((offset & (blocksize - 1)) == 0 &&
4652 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004653 goto out;
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004654 ret = btrfs_delalloc_reserve_space(inode,
Qu Wenruodf480632015-09-08 17:25:54 +08004655 round_down(from, PAGE_CACHE_SIZE), PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004656 if (ret)
4657 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004658
Chris Mason211c17f2008-05-15 09:13:45 -04004659again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004660 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004661 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004662 btrfs_delalloc_release_space(inode,
Qu Wenruodf480632015-09-08 17:25:54 +08004663 round_down(from, PAGE_CACHE_SIZE),
4664 PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004665 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004666 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004667 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004668
4669 page_start = page_offset(page);
4670 page_end = page_start + PAGE_CACHE_SIZE - 1;
4671
Chris Masona52d9a82007-08-27 16:49:44 -04004672 if (!PageUptodate(page)) {
4673 ret = btrfs_readpage(NULL, page);
4674 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004675 if (page->mapping != mapping) {
4676 unlock_page(page);
4677 page_cache_release(page);
4678 goto again;
4679 }
Chris Masona52d9a82007-08-27 16:49:44 -04004680 if (!PageUptodate(page)) {
4681 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004682 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004683 }
4684 }
Chris Mason211c17f2008-05-15 09:13:45 -04004685 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004686
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004687 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004688 set_page_extent_mapped(page);
4689
4690 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4691 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004692 unlock_extent_cached(io_tree, page_start, page_end,
4693 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004694 unlock_page(page);
4695 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004696 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004697 btrfs_put_ordered_extent(ordered);
4698 goto again;
4699 }
4700
Josef Bacik2ac55d42010-02-03 19:33:23 +00004701 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004702 EXTENT_DIRTY | EXTENT_DELALLOC |
4703 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004704 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004705
Josef Bacik2ac55d42010-02-03 19:33:23 +00004706 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4707 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004708 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004709 unlock_extent_cached(io_tree, page_start, page_end,
4710 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004711 goto out_unlock;
4712 }
4713
Chris Masone6dcd2d2008-07-17 12:53:50 -04004714 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004715 if (!len)
4716 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004717 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004718 if (front)
4719 memset(kaddr, 0, offset);
4720 else
4721 memset(kaddr + offset, 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);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004727 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4728 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004729
Chris Mason89642222008-07-24 09:41:53 -04004730out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004731 if (ret)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004732 btrfs_delalloc_release_space(inode, page_start,
4733 PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004734 unlock_page(page);
4735 page_cache_release(page);
4736out:
4737 return ret;
4738}
4739
Josef Bacik16e75492013-10-22 12:18:51 -04004740static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4741 u64 offset, u64 len)
4742{
4743 struct btrfs_trans_handle *trans;
4744 int ret;
4745
4746 /*
4747 * Still need to make sure the inode looks like it's been updated so
4748 * that any holes get logged if we fsync.
4749 */
4750 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4751 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4752 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4753 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4754 return 0;
4755 }
4756
4757 /*
4758 * 1 - for the one we're dropping
4759 * 1 - for the one we're adding
4760 * 1 - for updating the inode.
4761 */
4762 trans = btrfs_start_transaction(root, 3);
4763 if (IS_ERR(trans))
4764 return PTR_ERR(trans);
4765
4766 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4767 if (ret) {
4768 btrfs_abort_transaction(trans, root, ret);
4769 btrfs_end_transaction(trans, root);
4770 return ret;
4771 }
4772
4773 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4774 0, 0, len, 0, len, 0, 0, 0);
4775 if (ret)
4776 btrfs_abort_transaction(trans, root, ret);
4777 else
4778 btrfs_update_inode(trans, root, inode);
4779 btrfs_end_transaction(trans, root);
4780 return ret;
4781}
4782
Josef Bacik695a0d02011-03-04 15:46:53 -05004783/*
4784 * This function puts in dummy file extents for the area we're creating a hole
4785 * for. So if we are truncating this file to a larger size we need to insert
4786 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4787 * the range between oldsize and size
4788 */
Josef Bacika41ad392011-01-31 15:30:16 -05004789int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004790{
Yan Zheng9036c102008-10-30 14:19:41 -04004791 struct btrfs_root *root = BTRFS_I(inode)->root;
4792 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004793 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004794 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004795 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004796 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4797 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004798 u64 last_byte;
4799 u64 cur_offset;
4800 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004801 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004802
Josef Bacika71754f2013-06-17 17:14:39 -04004803 /*
4804 * If our size started in the middle of a page we need to zero out the
4805 * rest of the page before we expand the i_size, otherwise we could
4806 * expose stale data.
4807 */
4808 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4809 if (err)
4810 return err;
4811
Yan Zheng9036c102008-10-30 14:19:41 -04004812 if (size <= hole_start)
4813 return 0;
4814
Yan Zheng9036c102008-10-30 14:19:41 -04004815 while (1) {
4816 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004817
Josef Bacik2ac55d42010-02-03 19:33:23 +00004818 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004819 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004820 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4821 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004822 if (!ordered)
4823 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004824 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4825 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004826 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004827 btrfs_put_ordered_extent(ordered);
4828 }
4829
Yan Zheng9036c102008-10-30 14:19:41 -04004830 cur_offset = hole_start;
4831 while (1) {
4832 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4833 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004834 if (IS_ERR(em)) {
4835 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004836 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004837 break;
4838 }
Yan Zheng9036c102008-10-30 14:19:41 -04004839 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004840 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004841 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004842 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004843 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004844
Josef Bacik16e75492013-10-22 12:18:51 -04004845 err = maybe_insert_hole(root, inode, cur_offset,
4846 hole_size);
4847 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004848 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004849 btrfs_drop_extent_cache(inode, cur_offset,
4850 cur_offset + hole_size - 1, 0);
4851 hole_em = alloc_extent_map();
4852 if (!hole_em) {
4853 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4854 &BTRFS_I(inode)->runtime_flags);
4855 goto next;
4856 }
4857 hole_em->start = cur_offset;
4858 hole_em->len = hole_size;
4859 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004860
Josef Bacik5dc562c2012-08-17 13:14:17 -04004861 hole_em->block_start = EXTENT_MAP_HOLE;
4862 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004863 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004864 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004865 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4866 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004867 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004868
4869 while (1) {
4870 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004871 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004872 write_unlock(&em_tree->lock);
4873 if (err != -EEXIST)
4874 break;
4875 btrfs_drop_extent_cache(inode, cur_offset,
4876 cur_offset +
4877 hole_size - 1, 0);
4878 }
4879 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004880 }
Josef Bacik16e75492013-10-22 12:18:51 -04004881next:
Yan Zheng9036c102008-10-30 14:19:41 -04004882 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004883 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004884 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004885 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004886 break;
4887 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004888 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004889 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4890 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004891 return err;
4892}
4893
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004894static int wait_snapshoting_atomic_t(atomic_t *a)
4895{
4896 schedule();
4897 return 0;
4898}
4899
4900static void wait_for_snapshot_creation(struct btrfs_root *root)
4901{
4902 while (true) {
4903 int ret;
4904
4905 ret = btrfs_start_write_no_snapshoting(root);
4906 if (ret)
4907 break;
4908 wait_on_atomic_t(&root->will_be_snapshoted,
4909 wait_snapshoting_atomic_t,
4910 TASK_UNINTERRUPTIBLE);
4911 }
4912}
4913
Eric Sandeen3972f262013-01-12 02:57:22 +00004914static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004915{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004916 struct btrfs_root *root = BTRFS_I(inode)->root;
4917 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004918 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004919 loff_t newsize = attr->ia_size;
4920 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004921 int ret;
4922
Eric Sandeen3972f262013-01-12 02:57:22 +00004923 /*
4924 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4925 * special case where we need to update the times despite not having
4926 * these flags set. For all other operations the VFS set these flags
4927 * explicitly if it wants a timestamp update.
4928 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004929 if (newsize != oldsize) {
4930 inode_inc_iversion(inode);
4931 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4932 inode->i_ctime = inode->i_mtime =
4933 current_fs_time(inode->i_sb);
4934 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004935
Josef Bacika41ad392011-01-31 15:30:16 -05004936 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004937 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004938 /*
4939 * Don't do an expanding truncate while snapshoting is ongoing.
4940 * This is to ensure the snapshot captures a fully consistent
4941 * state of this file - if the snapshot captures this expanding
4942 * truncation, it must capture all writes that happened before
4943 * this truncation.
4944 */
4945 wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004946 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004947 if (ret) {
4948 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004949 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004950 }
Yan, Zheng80825102009-11-12 09:35:36 +00004951
Miao Xief4a2f4c2011-12-14 20:12:01 -05004952 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004953 if (IS_ERR(trans)) {
4954 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004955 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004956 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004957
4958 i_size_write(inode, newsize);
4959 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4960 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004961 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004962 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004963 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004964
Josef Bacika41ad392011-01-31 15:30:16 -05004965 /*
4966 * We're truncating a file that used to have good data down to
4967 * zero. Make sure it gets into the ordered flush list so that
4968 * any new writes get down to disk quickly.
4969 */
4970 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004971 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4972 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004973
Josef Bacikf3fe8202013-01-07 17:03:21 -05004974 /*
4975 * 1 for the orphan item we're going to add
4976 * 1 for the orphan item deletion.
4977 */
4978 trans = btrfs_start_transaction(root, 2);
4979 if (IS_ERR(trans))
4980 return PTR_ERR(trans);
4981
4982 /*
4983 * We need to do this in case we fail at _any_ point during the
4984 * actual truncate. Once we do the truncate_setsize we could
4985 * invalidate pages which forces any outstanding ordered io to
4986 * be instantly completed which will give us extents that need
4987 * to be truncated. If we fail to get an orphan inode down we
4988 * could have left over extents that were never meant to live,
4989 * so we need to garuntee from this point on that everything
4990 * will be consistent.
4991 */
4992 ret = btrfs_orphan_add(trans, inode);
4993 btrfs_end_transaction(trans, root);
4994 if (ret)
4995 return ret;
4996
Josef Bacika41ad392011-01-31 15:30:16 -05004997 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4998 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004999
5000 /* Disable nonlocked read DIO to avoid the end less truncate */
5001 btrfs_inode_block_unlocked_dio(inode);
5002 inode_dio_wait(inode);
5003 btrfs_inode_resume_unlocked_dio(inode);
5004
Josef Bacika41ad392011-01-31 15:30:16 -05005005 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005006 if (ret && inode->i_nlink) {
5007 int err;
5008
5009 /*
5010 * failed to truncate, disk_i_size is only adjusted down
5011 * as we remove extents, so it should represent the true
5012 * size of the inode, so reset the in memory size and
5013 * delete our orphan entry.
5014 */
5015 trans = btrfs_join_transaction(root);
5016 if (IS_ERR(trans)) {
5017 btrfs_orphan_del(NULL, inode);
5018 return ret;
5019 }
5020 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5021 err = btrfs_orphan_del(trans, inode);
5022 if (err)
5023 btrfs_abort_transaction(trans, root, err);
5024 btrfs_end_transaction(trans, root);
5025 }
Yan, Zheng80825102009-11-12 09:35:36 +00005026 }
5027
Josef Bacika41ad392011-01-31 15:30:16 -05005028 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005029}
5030
Chris Mason39279cc2007-06-12 06:35:45 -04005031static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5032{
David Howells2b0143b2015-03-17 22:25:59 +00005033 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005034 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005035 int err;
5036
Li Zefanb83cc962010-12-20 16:04:08 +08005037 if (btrfs_root_readonly(root))
5038 return -EROFS;
5039
Chris Mason39279cc2007-06-12 06:35:45 -04005040 err = inode_change_ok(inode, attr);
5041 if (err)
5042 return err;
5043
Chris Mason5a3f23d2009-03-31 13:27:11 -04005044 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005045 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005046 if (err)
5047 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005048 }
Yan Zheng9036c102008-10-30 14:19:41 -04005049
Christoph Hellwig10257742010-06-04 11:30:02 +02005050 if (attr->ia_valid) {
5051 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005052 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005053 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005054
Josef Bacik22c44fe2011-11-30 10:45:38 -05005055 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005056 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005057 }
5058
Chris Mason39279cc2007-06-12 06:35:45 -04005059 return err;
5060}
Chris Mason61295eb2008-01-14 16:24:38 -05005061
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005062/*
5063 * While truncating the inode pages during eviction, we get the VFS calling
5064 * btrfs_invalidatepage() against each page of the inode. This is slow because
5065 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5066 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5067 * extent_state structures over and over, wasting lots of time.
5068 *
5069 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5070 * those expensive operations on a per page basis and do only the ordered io
5071 * finishing, while we release here the extent_map and extent_state structures,
5072 * without the excessive merging and splitting.
5073 */
5074static void evict_inode_truncate_pages(struct inode *inode)
5075{
5076 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5077 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5078 struct rb_node *node;
5079
5080 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005081 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005082
5083 write_lock(&map_tree->lock);
5084 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5085 struct extent_map *em;
5086
5087 node = rb_first(&map_tree->map);
5088 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005089 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5090 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005091 remove_extent_mapping(map_tree, em);
5092 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005093 if (need_resched()) {
5094 write_unlock(&map_tree->lock);
5095 cond_resched();
5096 write_lock(&map_tree->lock);
5097 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005098 }
5099 write_unlock(&map_tree->lock);
5100
Filipe Manana6ca07092015-05-26 00:55:42 +01005101 /*
5102 * Keep looping until we have no more ranges in the io tree.
5103 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005104 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5105 * still in progress (unlocked the pages in the bio but did not yet
5106 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005107 * ranges can still be locked and eviction started because before
5108 * submitting those bios, which are executed by a separate task (work
5109 * queue kthread), inode references (inode->i_count) were not taken
5110 * (which would be dropped in the end io callback of each bio).
5111 * Therefore here we effectively end up waiting for those bios and
5112 * anyone else holding locked ranges without having bumped the inode's
5113 * reference count - if we don't do it, when they access the inode's
5114 * io_tree to unlock a range it may be too late, leading to an
5115 * use-after-free issue.
5116 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005117 spin_lock(&io_tree->lock);
5118 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5119 struct extent_state *state;
5120 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005121 u64 start;
5122 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005123
5124 node = rb_first(&io_tree->state);
5125 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005126 start = state->start;
5127 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005128 spin_unlock(&io_tree->lock);
5129
Filipe Manana6ca07092015-05-26 00:55:42 +01005130 lock_extent_bits(io_tree, start, end, 0, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005131
5132 /*
5133 * If still has DELALLOC flag, the extent didn't reach disk,
5134 * and its reserved space won't be freed by delayed_ref.
5135 * So we need to free its reserved space here.
5136 * (Refer to comment in btrfs_invalidatepage, case 2)
5137 *
5138 * Note, end is the bytenr of last byte, so we need + 1 here.
5139 */
5140 if (state->state & EXTENT_DELALLOC)
5141 btrfs_qgroup_free_data(inode, start, end - start + 1);
5142
Filipe Manana6ca07092015-05-26 00:55:42 +01005143 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005144 EXTENT_LOCKED | EXTENT_DIRTY |
5145 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5146 EXTENT_DEFRAG, 1, 1,
5147 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005148
Filipe Manana7064dd52014-08-08 02:47:05 +01005149 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005150 spin_lock(&io_tree->lock);
5151 }
5152 spin_unlock(&io_tree->lock);
5153}
5154
Al Virobd555972010-06-07 11:35:40 -04005155void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005156{
5157 struct btrfs_trans_handle *trans;
5158 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005159 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005160 int steal_from_global = 0;
Josef Bacik07127182011-08-19 10:29:59 -04005161 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04005162 int ret;
5163
liubo1abe9b82011-03-24 11:18:59 +00005164 trace_btrfs_inode_evict(inode);
5165
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005166 evict_inode_truncate_pages(inode);
5167
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005168 if (inode->i_nlink &&
5169 ((btrfs_root_refs(&root->root_item) != 0 &&
5170 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5171 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005172 goto no_delete;
5173
Chris Mason39279cc2007-06-12 06:35:45 -04005174 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005175 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005176 goto no_delete;
5177 }
Al Virobd555972010-06-07 11:35:40 -04005178 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005179 if (!special_file(inode->i_mode))
5180 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005181
Miao Xief6124962014-09-12 18:44:04 +08005182 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5183
Yan, Zhengc71bf092009-11-12 09:34:40 +00005184 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06005185 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005186 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005187 goto no_delete;
5188 }
5189
Yan, Zheng76dda932009-09-21 16:00:26 -04005190 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005191 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5192 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005193 goto no_delete;
5194 }
5195
Miao Xie0e8c36a2012-12-19 06:59:51 +00005196 ret = btrfs_commit_inode_delayed_inode(inode);
5197 if (ret) {
5198 btrfs_orphan_del(NULL, inode);
5199 goto no_delete;
5200 }
5201
Miao Xie66d8f3d2012-09-06 04:02:28 -06005202 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005203 if (!rsv) {
5204 btrfs_orphan_del(NULL, inode);
5205 goto no_delete;
5206 }
Josef Bacik4a338542011-08-29 11:01:31 -04005207 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005208 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005209 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005210
Chris Masondbe674a2008-07-17 12:54:05 -04005211 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005212
Josef Bacik4289a662011-08-05 13:22:24 -04005213 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005214 * This is a bit simpler than btrfs_truncate since we've already
5215 * reserved our space for our orphan item in the unlink, so we just
5216 * need to reserve some slack space in case we add bytes and update
5217 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005218 */
Yan, Zheng80825102009-11-12 09:35:36 +00005219 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005220 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5221 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005222
Josef Bacik726c35f2011-09-26 15:46:06 -04005223 /*
5224 * Try and steal from the global reserve since we will
5225 * likely not use this space anyway, we want to try as
5226 * hard as possible to get this to work.
5227 */
5228 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005229 steal_from_global++;
5230 else
5231 steal_from_global = 0;
5232 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005233
Josef Bacik3bce8762015-02-24 12:35:51 -08005234 /*
5235 * steal_from_global == 0: we reserved stuff, hooray!
5236 * steal_from_global == 1: we didn't reserve stuff, boo!
5237 * steal_from_global == 2: we've committed, still not a lot of
5238 * room but maybe we'll have room in the global reserve this
5239 * time.
5240 * steal_from_global == 3: abandon all hope!
5241 */
5242 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005243 btrfs_warn(root->fs_info,
5244 "Could not get space for a delete, will truncate on mount %d",
5245 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005246 btrfs_orphan_del(NULL, inode);
5247 btrfs_free_block_rsv(root, rsv);
5248 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005249 }
5250
Miao Xie0e8c36a2012-12-19 06:59:51 +00005251 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005252 if (IS_ERR(trans)) {
5253 btrfs_orphan_del(NULL, inode);
5254 btrfs_free_block_rsv(root, rsv);
5255 goto no_delete;
5256 }
5257
Josef Bacik3bce8762015-02-24 12:35:51 -08005258 /*
5259 * We can't just steal from the global reserve, we need tomake
5260 * sure there is room to do it, if not we need to commit and try
5261 * again.
5262 */
5263 if (steal_from_global) {
5264 if (!btrfs_check_space_for_delayed_refs(trans, root))
5265 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5266 min_size);
5267 else
5268 ret = -ENOSPC;
5269 }
5270
5271 /*
5272 * Couldn't steal from the global reserve, we have too much
5273 * pending stuff built up, commit the transaction and try it
5274 * again.
5275 */
5276 if (ret) {
5277 ret = btrfs_commit_transaction(trans, root);
5278 if (ret) {
5279 btrfs_orphan_del(NULL, inode);
5280 btrfs_free_block_rsv(root, rsv);
5281 goto no_delete;
5282 }
5283 continue;
5284 } else {
5285 steal_from_global = 0;
5286 }
5287
Josef Bacik4289a662011-08-05 13:22:24 -04005288 trans->block_rsv = rsv;
5289
Yan, Zhengd68fc572010-05-16 10:49:58 -04005290 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005291 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005292 break;
5293
Miao Xie8407aa42012-09-07 01:43:32 -06005294 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005295 btrfs_end_transaction(trans, root);
5296 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005297 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005298 }
5299
Josef Bacik4289a662011-08-05 13:22:24 -04005300 btrfs_free_block_rsv(root, rsv);
5301
Josef Bacik4ef31a42013-08-13 14:10:08 -04005302 /*
5303 * Errors here aren't a big deal, it just means we leave orphan items
5304 * in the tree. They will be cleaned up on the next mount.
5305 */
Yan, Zheng80825102009-11-12 09:35:36 +00005306 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005307 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005308 btrfs_orphan_del(trans, inode);
5309 } else {
5310 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005311 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005312
Josef Bacik4289a662011-08-05 13:22:24 -04005313 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005314 if (!(root == root->fs_info->tree_root ||
5315 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005316 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005317
Chris Mason54aa1f42007-06-22 14:16:25 -04005318 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005319 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005320no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005321 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005322 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005323 return;
Chris Mason39279cc2007-06-12 06:35:45 -04005324}
5325
5326/*
5327 * this returns the key found in the dir entry in the location pointer.
5328 * If no dir entries were found, location->objectid is 0.
5329 */
5330static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5331 struct btrfs_key *location)
5332{
5333 const char *name = dentry->d_name.name;
5334 int namelen = dentry->d_name.len;
5335 struct btrfs_dir_item *di;
5336 struct btrfs_path *path;
5337 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005338 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005339
5340 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005341 if (!path)
5342 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005343
Li Zefan33345d012011-04-20 10:31:50 +08005344 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005345 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005346 if (IS_ERR(di))
5347 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005348
David Sterbac7040052011-04-19 18:00:01 +02005349 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005350 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005351
Chris Mason5f39d392007-10-15 16:14:19 -04005352 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005353out:
Chris Mason39279cc2007-06-12 06:35:45 -04005354 btrfs_free_path(path);
5355 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005356out_err:
5357 location->objectid = 0;
5358 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005359}
5360
5361/*
5362 * when we hit a tree root in a directory, the btrfs part of the inode
5363 * needs to be changed to reflect the root directory of the tree root. This
5364 * is kind of like crossing a mount point.
5365 */
5366static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005367 struct inode *dir,
5368 struct dentry *dentry,
5369 struct btrfs_key *location,
5370 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005371{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005372 struct btrfs_path *path;
5373 struct btrfs_root *new_root;
5374 struct btrfs_root_ref *ref;
5375 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005376 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005377 int ret;
5378 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005379
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005380 path = btrfs_alloc_path();
5381 if (!path) {
5382 err = -ENOMEM;
5383 goto out;
5384 }
Chris Mason39279cc2007-06-12 06:35:45 -04005385
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005386 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005387 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5388 key.type = BTRFS_ROOT_REF_KEY;
5389 key.offset = location->objectid;
5390
5391 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5392 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005393 if (ret) {
5394 if (ret < 0)
5395 err = ret;
5396 goto out;
5397 }
Chris Mason39279cc2007-06-12 06:35:45 -04005398
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005399 leaf = path->nodes[0];
5400 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005401 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005402 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5403 goto out;
5404
5405 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5406 (unsigned long)(ref + 1),
5407 dentry->d_name.len);
5408 if (ret)
5409 goto out;
5410
David Sterbab3b4aa72011-04-21 01:20:15 +02005411 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005412
5413 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5414 if (IS_ERR(new_root)) {
5415 err = PTR_ERR(new_root);
5416 goto out;
5417 }
5418
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005419 *sub_root = new_root;
5420 location->objectid = btrfs_root_dirid(&new_root->root_item);
5421 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005422 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005423 err = 0;
5424out:
5425 btrfs_free_path(path);
5426 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005427}
5428
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005429static void inode_tree_add(struct inode *inode)
5430{
5431 struct btrfs_root *root = BTRFS_I(inode)->root;
5432 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005433 struct rb_node **p;
5434 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005435 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005436 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005437
Al Viro1d3382cb2010-10-23 15:19:20 -04005438 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005439 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005440 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005441 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005442 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005443 while (*p) {
5444 parent = *p;
5445 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5446
Li Zefan33345d012011-04-20 10:31:50 +08005447 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005448 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005449 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005450 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005451 else {
5452 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005453 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005454 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005455 RB_CLEAR_NODE(parent);
5456 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005457 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005458 }
5459 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005460 rb_link_node(new, parent, p);
5461 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005462 spin_unlock(&root->inode_lock);
5463}
5464
5465static void inode_tree_del(struct inode *inode)
5466{
5467 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005468 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005469
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005470 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005471 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005472 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005473 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005474 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005475 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005476 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005477
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005478 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005479 synchronize_srcu(&root->fs_info->subvol_srcu);
5480 spin_lock(&root->inode_lock);
5481 empty = RB_EMPTY_ROOT(&root->inode_tree);
5482 spin_unlock(&root->inode_lock);
5483 if (empty)
5484 btrfs_add_dead_root(root);
5485 }
5486}
5487
Jeff Mahoney143bede2012-03-01 14:56:26 +01005488void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005489{
5490 struct rb_node *node;
5491 struct rb_node *prev;
5492 struct btrfs_inode *entry;
5493 struct inode *inode;
5494 u64 objectid = 0;
5495
Liu Bo7813b3d2014-02-10 17:37:25 +08005496 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5497 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005498
5499 spin_lock(&root->inode_lock);
5500again:
5501 node = root->inode_tree.rb_node;
5502 prev = NULL;
5503 while (node) {
5504 prev = node;
5505 entry = rb_entry(node, struct btrfs_inode, rb_node);
5506
Li Zefan33345d012011-04-20 10:31:50 +08005507 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005508 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005509 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005510 node = node->rb_right;
5511 else
5512 break;
5513 }
5514 if (!node) {
5515 while (prev) {
5516 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005517 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005518 node = prev;
5519 break;
5520 }
5521 prev = rb_next(prev);
5522 }
5523 }
5524 while (node) {
5525 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005526 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005527 inode = igrab(&entry->vfs_inode);
5528 if (inode) {
5529 spin_unlock(&root->inode_lock);
5530 if (atomic_read(&inode->i_count) > 1)
5531 d_prune_aliases(inode);
5532 /*
Al Viro45321ac2010-06-07 13:43:19 -04005533 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005534 * the inode cache when its usage count
5535 * hits zero.
5536 */
5537 iput(inode);
5538 cond_resched();
5539 spin_lock(&root->inode_lock);
5540 goto again;
5541 }
5542
5543 if (cond_resched_lock(&root->inode_lock))
5544 goto again;
5545
5546 node = rb_next(node);
5547 }
5548 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005549}
5550
Chris Masone02119d2008-09-05 16:13:11 -04005551static int btrfs_init_locked_inode(struct inode *inode, void *p)
5552{
5553 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005554 inode->i_ino = args->location->objectid;
5555 memcpy(&BTRFS_I(inode)->location, args->location,
5556 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005557 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005558 return 0;
5559}
5560
5561static int btrfs_find_actor(struct inode *inode, void *opaque)
5562{
5563 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005564 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005565 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005566}
5567
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005568static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005569 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005570 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005571{
5572 struct inode *inode;
5573 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005574 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005575
Chris Mason90d3e592014-01-09 17:28:00 -08005576 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005577 args.root = root;
5578
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005579 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005580 btrfs_init_locked_inode,
5581 (void *)&args);
5582 return inode;
5583}
5584
Balaji Rao1a54ef82008-07-21 02:01:04 +05305585/* Get an inode object given its location and corresponding root.
5586 * Returns in *is_new if the inode was read from disk
5587 */
5588struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005589 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305590{
5591 struct inode *inode;
5592
Chris Mason90d3e592014-01-09 17:28:00 -08005593 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305594 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005595 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305596
5597 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305598 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005599 if (!is_bad_inode(inode)) {
5600 inode_tree_add(inode);
5601 unlock_new_inode(inode);
5602 if (new)
5603 *new = 1;
5604 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005605 unlock_new_inode(inode);
5606 iput(inode);
5607 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005608 }
5609 }
5610
Balaji Rao1a54ef82008-07-21 02:01:04 +05305611 return inode;
5612}
5613
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005614static struct inode *new_simple_dir(struct super_block *s,
5615 struct btrfs_key *key,
5616 struct btrfs_root *root)
5617{
5618 struct inode *inode = new_inode(s);
5619
5620 if (!inode)
5621 return ERR_PTR(-ENOMEM);
5622
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005623 BTRFS_I(inode)->root = root;
5624 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005625 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005626
5627 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005628 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005629 inode->i_fop = &simple_dir_operations;
5630 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305631 inode->i_mtime = CURRENT_TIME;
5632 inode->i_atime = inode->i_mtime;
5633 inode->i_ctime = inode->i_mtime;
5634 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005635
5636 return inode;
5637}
5638
Chris Mason3de45862008-11-17 21:02:50 -05005639struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005640{
Chris Masond3977122009-01-05 21:25:51 -05005641 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005642 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005643 struct btrfs_root *sub_root = root;
5644 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005645 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005646 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005647
5648 if (dentry->d_name.len > BTRFS_NAME_LEN)
5649 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005650
Jeff Layton39e3c952012-11-28 11:30:53 -05005651 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005652 if (ret < 0)
5653 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005654
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005655 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005656 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005657
5658 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005659 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005660 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005661 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005662
5663 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5664
Yan, Zheng76dda932009-09-21 16:00:26 -04005665 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005666 ret = fixup_tree_root_location(root, dir, dentry,
5667 &location, &sub_root);
5668 if (ret < 0) {
5669 if (ret != -ENOENT)
5670 inode = ERR_PTR(ret);
5671 else
5672 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5673 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005674 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005675 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005676 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5677
Julia Lawall34d19ba2011-01-24 19:55:19 +00005678 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005679 down_read(&root->fs_info->cleanup_work_sem);
5680 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005681 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005682 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005683 if (ret) {
5684 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005685 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005686 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005687 }
5688
Chris Mason3de45862008-11-17 21:02:50 -05005689 return inode;
5690}
5691
Nick Pigginfe15ce42011-01-07 17:49:23 +11005692static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005693{
5694 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005695 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005696
Li Zefan848cce02012-02-21 17:04:28 +08005697 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005698 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005699
Li Zefan848cce02012-02-21 17:04:28 +08005700 if (inode) {
5701 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005702 if (btrfs_root_refs(&root->root_item) == 0)
5703 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005704
5705 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5706 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005707 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005708 return 0;
5709}
5710
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005711static void btrfs_dentry_release(struct dentry *dentry)
5712{
Daeseok Youn944a4512014-04-14 15:37:02 +09005713 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005714}
5715
Chris Mason3de45862008-11-17 21:02:50 -05005716static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005717 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005718{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005719 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005720
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005721 inode = btrfs_lookup_dentry(dir, dentry);
5722 if (IS_ERR(inode)) {
5723 if (PTR_ERR(inode) == -ENOENT)
5724 inode = NULL;
5725 else
5726 return ERR_CAST(inode);
5727 }
5728
Al Viro41d28bc2014-10-12 22:24:21 -04005729 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005730}
5731
Miao Xie16cdcec2011-04-22 18:12:22 +08005732unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005733 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5734};
5735
Al Viro9cdda8d2013-05-22 16:48:09 -04005736static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005737{
Al Viro9cdda8d2013-05-22 16:48:09 -04005738 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005739 struct btrfs_root *root = BTRFS_I(inode)->root;
5740 struct btrfs_item *item;
5741 struct btrfs_dir_item *di;
5742 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005743 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005744 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005745 struct list_head ins_list;
5746 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005747 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005748 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005749 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005750 unsigned char d_type;
5751 int over = 0;
5752 u32 di_cur;
5753 u32 di_total;
5754 u32 di_len;
5755 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005756 char tmp_name[32];
5757 char *name_ptr;
5758 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005759 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005760
5761 /* FIXME, use a real flag for deciding about the key type */
5762 if (root->fs_info->tree_root == root)
5763 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005764
Al Viro9cdda8d2013-05-22 16:48:09 -04005765 if (!dir_emit_dots(file, ctx))
5766 return 0;
5767
David Woodhouse49593bf2008-08-17 17:08:36 +01005768 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005769 if (!path)
5770 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005771
Josef Bacik026fd312011-05-13 10:32:11 -04005772 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005773
Miao Xie16cdcec2011-04-22 18:12:22 +08005774 if (key_type == BTRFS_DIR_INDEX_KEY) {
5775 INIT_LIST_HEAD(&ins_list);
5776 INIT_LIST_HEAD(&del_list);
5777 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5778 }
5779
David Sterba962a2982014-06-04 18:41:45 +02005780 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005781 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005782 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005783
Chris Mason39279cc2007-06-12 06:35:45 -04005784 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5785 if (ret < 0)
5786 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005787
5788 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005789 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005790 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005791 if (slot >= btrfs_header_nritems(leaf)) {
5792 ret = btrfs_next_leaf(root, path);
5793 if (ret < 0)
5794 goto err;
5795 else if (ret > 0)
5796 break;
5797 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005798 }
Chris Mason3de45862008-11-17 21:02:50 -05005799
Ross Kirkdd3cc162013-09-16 15:58:09 +01005800 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005801 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5802
5803 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005804 break;
David Sterba962a2982014-06-04 18:41:45 +02005805 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005806 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005807 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005808 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005809 if (key_type == BTRFS_DIR_INDEX_KEY &&
5810 btrfs_should_delete_dir_index(&del_list,
5811 found_key.offset))
5812 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005813
Al Viro9cdda8d2013-05-22 16:48:09 -04005814 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005815 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005816
Chris Mason39279cc2007-06-12 06:35:45 -04005817 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5818 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005819 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005820
5821 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005822 struct btrfs_key location;
5823
Josef Bacik22a94d42011-03-16 16:47:17 -04005824 if (verify_dir_item(root, leaf, di))
5825 break;
5826
Chris Mason5f39d392007-10-15 16:14:19 -04005827 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005828 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005829 name_ptr = tmp_name;
5830 } else {
5831 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005832 if (!name_ptr) {
5833 ret = -ENOMEM;
5834 goto err;
5835 }
Chris Mason5f39d392007-10-15 16:14:19 -04005836 }
5837 read_extent_buffer(leaf, name_ptr,
5838 (unsigned long)(di + 1), name_len);
5839
5840 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5841 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005842
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005843
Chris Mason3de45862008-11-17 21:02:50 -05005844 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005845 * skip it.
5846 *
5847 * In contrast to old kernels, we insert the snapshot's
5848 * dir item and dir index after it has been created, so
5849 * we won't find a reference to our own snapshot. We
5850 * still keep the following code for backward
5851 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005852 */
5853 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5854 location.objectid == root->root_key.objectid) {
5855 over = 0;
5856 goto skip;
5857 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005858 over = !dir_emit(ctx, name_ptr, name_len,
5859 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005860
Chris Mason3de45862008-11-17 21:02:50 -05005861skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005862 if (name_ptr != tmp_name)
5863 kfree(name_ptr);
5864
Chris Mason39279cc2007-06-12 06:35:45 -04005865 if (over)
5866 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005867 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005868 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005869 di_cur += di_len;
5870 di = (struct btrfs_dir_item *)((char *)di + di_len);
5871 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005872next:
5873 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005874 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005875
Miao Xie16cdcec2011-04-22 18:12:22 +08005876 if (key_type == BTRFS_DIR_INDEX_KEY) {
5877 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005878 ctx->pos++;
5879 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005880 if (ret)
5881 goto nopos;
5882 }
5883
David Woodhouse49593bf2008-08-17 17:08:36 +01005884 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005885 ctx->pos++;
5886
5887 /*
5888 * Stop new entries from being returned after we return the last
5889 * entry.
5890 *
5891 * New directory entries are assigned a strictly increasing
5892 * offset. This means that new entries created during readdir
5893 * are *guaranteed* to be seen in the future by that readdir.
5894 * This has broken buggy programs which operate on names as
5895 * they're returned by readdir. Until we re-use freed offsets
5896 * we have this hack to stop new entries from being returned
5897 * under the assumption that they'll never reach this huge
5898 * offset.
5899 *
5900 * This is being careful not to overflow 32bit loff_t unless the
5901 * last entry requires it because doing so has broken 32bit apps
5902 * in the past.
5903 */
5904 if (key_type == BTRFS_DIR_INDEX_KEY) {
5905 if (ctx->pos >= INT_MAX)
5906 ctx->pos = LLONG_MAX;
5907 else
5908 ctx->pos = INT_MAX;
5909 }
Chris Mason39279cc2007-06-12 06:35:45 -04005910nopos:
5911 ret = 0;
5912err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005913 if (key_type == BTRFS_DIR_INDEX_KEY)
5914 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005915 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005916 return ret;
5917}
5918
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005919int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005920{
5921 struct btrfs_root *root = BTRFS_I(inode)->root;
5922 struct btrfs_trans_handle *trans;
5923 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005924 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005925
Josef Bacik72ac3c02012-05-23 14:13:11 -04005926 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005927 return 0;
5928
Liu Bo83eea1f2012-07-10 05:28:39 -06005929 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005930 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005931
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005932 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005933 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005934 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005935 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005936 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005937 if (IS_ERR(trans))
5938 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005939 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005940 }
5941 return ret;
5942}
5943
5944/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005945 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005946 * inode changes. But, it is most likely to find the inode in cache.
5947 * FIXME, needs more benchmarking...there are no reasons other than performance
5948 * to keep or drop this code.
5949 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005950static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005951{
5952 struct btrfs_root *root = BTRFS_I(inode)->root;
5953 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005954 int ret;
5955
Josef Bacik72ac3c02012-05-23 14:13:11 -04005956 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005957 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005958
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005959 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005960 if (IS_ERR(trans))
5961 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005962
5963 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005964 if (ret && ret == -ENOSPC) {
5965 /* whoops, lets try again with the full transaction */
5966 btrfs_end_transaction(trans, root);
5967 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005968 if (IS_ERR(trans))
5969 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005970
Chris Mason94b60442010-05-26 11:02:00 -04005971 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005972 }
Chris Mason39279cc2007-06-12 06:35:45 -04005973 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005974 if (BTRFS_I(inode)->delayed_node)
5975 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005976
5977 return ret;
5978}
5979
5980/*
5981 * This is a copy of file_update_time. We need this so we can return error on
5982 * ENOSPC for updating the inode in the case of file write and mmap writes.
5983 */
Josef Bacike41f9412012-03-26 09:46:47 -04005984static int btrfs_update_time(struct inode *inode, struct timespec *now,
5985 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005986{
Alexander Block2bc5565282012-06-15 09:49:33 +02005987 struct btrfs_root *root = BTRFS_I(inode)->root;
5988
5989 if (btrfs_root_readonly(root))
5990 return -EROFS;
5991
Josef Bacike41f9412012-03-26 09:46:47 -04005992 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005993 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005994 if (flags & S_CTIME)
5995 inode->i_ctime = *now;
5996 if (flags & S_MTIME)
5997 inode->i_mtime = *now;
5998 if (flags & S_ATIME)
5999 inode->i_atime = *now;
6000 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006001}
6002
Chris Masond352ac62008-09-29 15:18:18 -04006003/*
6004 * find the highest existing sequence number in a directory
6005 * and then set the in-memory index_cnt variable to reflect
6006 * free sequence numbers
6007 */
Josef Bacikaec74772008-07-24 12:12:38 -04006008static int btrfs_set_inode_index_count(struct inode *inode)
6009{
6010 struct btrfs_root *root = BTRFS_I(inode)->root;
6011 struct btrfs_key key, found_key;
6012 struct btrfs_path *path;
6013 struct extent_buffer *leaf;
6014 int ret;
6015
Li Zefan33345d012011-04-20 10:31:50 +08006016 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006017 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006018 key.offset = (u64)-1;
6019
6020 path = btrfs_alloc_path();
6021 if (!path)
6022 return -ENOMEM;
6023
6024 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6025 if (ret < 0)
6026 goto out;
6027 /* FIXME: we should be able to handle this */
6028 if (ret == 0)
6029 goto out;
6030 ret = 0;
6031
6032 /*
6033 * MAGIC NUMBER EXPLANATION:
6034 * since we search a directory based on f_pos we have to start at 2
6035 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6036 * else has to start at 2
6037 */
6038 if (path->slots[0] == 0) {
6039 BTRFS_I(inode)->index_cnt = 2;
6040 goto out;
6041 }
6042
6043 path->slots[0]--;
6044
6045 leaf = path->nodes[0];
6046 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6047
Li Zefan33345d012011-04-20 10:31:50 +08006048 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006049 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006050 BTRFS_I(inode)->index_cnt = 2;
6051 goto out;
6052 }
6053
6054 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6055out:
6056 btrfs_free_path(path);
6057 return ret;
6058}
6059
Chris Masond352ac62008-09-29 15:18:18 -04006060/*
6061 * helper to find a free sequence number in a given directory. This current
6062 * code is very simple, later versions will do smarter things in the btree
6063 */
Chris Mason3de45862008-11-17 21:02:50 -05006064int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006065{
6066 int ret = 0;
6067
6068 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006069 ret = btrfs_inode_delayed_dir_index_count(dir);
6070 if (ret) {
6071 ret = btrfs_set_inode_index_count(dir);
6072 if (ret)
6073 return ret;
6074 }
Josef Bacikaec74772008-07-24 12:12:38 -04006075 }
6076
Chris Mason00e4e6b2008-08-05 11:18:09 -04006077 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006078 BTRFS_I(dir)->index_cnt++;
6079
6080 return ret;
6081}
6082
Chris Masonb0d5d102014-09-08 13:08:51 -07006083static int btrfs_insert_inode_locked(struct inode *inode)
6084{
6085 struct btrfs_iget_args args;
6086 args.location = &BTRFS_I(inode)->location;
6087 args.root = BTRFS_I(inode)->root;
6088
6089 return insert_inode_locked4(inode,
6090 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6091 btrfs_find_actor, &args);
6092}
6093
Chris Mason39279cc2007-06-12 06:35:45 -04006094static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6095 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006096 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006097 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006098 u64 ref_objectid, u64 objectid,
6099 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006100{
6101 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006102 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006103 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006104 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006105 struct btrfs_inode_ref *ref;
6106 struct btrfs_key key[2];
6107 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006108 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006109 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006110 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006111
Chris Mason5f39d392007-10-15 16:14:19 -04006112 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006113 if (!path)
6114 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006115
Chris Mason39279cc2007-06-12 06:35:45 -04006116 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006117 if (!inode) {
6118 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006119 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006120 }
Chris Mason39279cc2007-06-12 06:35:45 -04006121
Li Zefan581bb052011-04-20 10:06:11 +08006122 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006123 * O_TMPFILE, set link count to 0, so that after this point,
6124 * we fill in an inode item with the correct link count.
6125 */
6126 if (!name)
6127 set_nlink(inode, 0);
6128
6129 /*
Li Zefan581bb052011-04-20 10:06:11 +08006130 * we have to initialize this early, so we can reclaim the inode
6131 * number if we fail afterwards in this function.
6132 */
6133 inode->i_ino = objectid;
6134
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006135 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006136 trace_btrfs_inode_request(dir);
6137
Chris Mason3de45862008-11-17 21:02:50 -05006138 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006139 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006140 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006141 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006142 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006143 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006144 } else if (dir) {
6145 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006146 }
6147 /*
6148 * index_cnt is ignored for everything but a dir,
6149 * btrfs_get_inode_index_count has an explanation for the magic
6150 * number
6151 */
6152 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006153 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006154 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006155 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006156 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006157
Josef Bacik5dc562c2012-08-17 13:14:17 -04006158 /*
6159 * We could have gotten an inode number from somebody who was fsynced
6160 * and then removed in this same transaction, so let's just set full
6161 * sync since it will be a full sync anyway and this will blow away the
6162 * old info in the log.
6163 */
6164 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6165
Chris Mason9c583092008-01-29 15:15:18 -05006166 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006167 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006168 key[0].offset = 0;
6169
Chris Mason9c583092008-01-29 15:15:18 -05006170 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006171
6172 if (name) {
6173 /*
6174 * Start new inodes with an inode_ref. This is slightly more
6175 * efficient for small numbers of hard links since they will
6176 * be packed into one item. Extended refs will kick in if we
6177 * add more hard links than can fit in the ref item.
6178 */
6179 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006180 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006181 key[1].offset = ref_objectid;
6182
6183 sizes[1] = name_len + sizeof(*ref);
6184 }
Chris Mason9c583092008-01-29 15:15:18 -05006185
Chris Masonb0d5d102014-09-08 13:08:51 -07006186 location = &BTRFS_I(inode)->location;
6187 location->objectid = objectid;
6188 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006189 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006190
6191 ret = btrfs_insert_inode_locked(inode);
6192 if (ret < 0)
6193 goto fail;
6194
Chris Masonb9473432009-03-13 11:00:37 -04006195 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006196 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006197 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006198 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006199
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006200 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006201 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306202
6203 inode->i_mtime = CURRENT_TIME;
6204 inode->i_atime = inode->i_mtime;
6205 inode->i_ctime = inode->i_mtime;
6206 BTRFS_I(inode)->i_otime = inode->i_mtime;
6207
Chris Mason5f39d392007-10-15 16:14:19 -04006208 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6209 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006210 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6211 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006212 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006213
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006214 if (name) {
6215 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6216 struct btrfs_inode_ref);
6217 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6218 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6219 ptr = (unsigned long)(ref + 1);
6220 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6221 }
Chris Mason9c583092008-01-29 15:15:18 -05006222
Chris Mason5f39d392007-10-15 16:14:19 -04006223 btrfs_mark_buffer_dirty(path->nodes[0]);
6224 btrfs_free_path(path);
6225
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006226 btrfs_inherit_iflags(inode, dir);
6227
Al Viro569254b2011-07-24 17:08:40 -04006228 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04006229 if (btrfs_test_opt(root, NODATASUM))
6230 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06006231 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006232 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6233 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006234 }
6235
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006236 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006237
6238 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006239 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006240
Alexander Block8ea05e32012-07-25 17:35:53 +02006241 btrfs_update_root_times(trans, root);
6242
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006243 ret = btrfs_inode_inherit_props(trans, inode, dir);
6244 if (ret)
6245 btrfs_err(root->fs_info,
6246 "error inheriting props for ino %llu (root %llu): %d",
6247 btrfs_ino(inode), root->root_key.objectid, ret);
6248
Chris Mason39279cc2007-06-12 06:35:45 -04006249 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006250
6251fail_unlock:
6252 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006253fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006254 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006255 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006256 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006257 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006258 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006259}
6260
6261static inline u8 btrfs_inode_type(struct inode *inode)
6262{
6263 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6264}
6265
Chris Masond352ac62008-09-29 15:18:18 -04006266/*
6267 * utility function to add 'inode' into 'parent_inode' with
6268 * a give name and a given sequence number.
6269 * if 'add_backref' is true, also insert a backref from the
6270 * inode to the parent directory.
6271 */
Chris Masone02119d2008-09-05 16:13:11 -04006272int btrfs_add_link(struct btrfs_trans_handle *trans,
6273 struct inode *parent_inode, struct inode *inode,
6274 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006275{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006276 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006277 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006278 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006279 u64 ino = btrfs_ino(inode);
6280 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006281
Li Zefan33345d012011-04-20 10:31:50 +08006282 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006283 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6284 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006285 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006286 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006287 key.offset = 0;
6288 }
Chris Mason39279cc2007-06-12 06:35:45 -04006289
Li Zefan33345d012011-04-20 10:31:50 +08006290 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006291 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6292 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006293 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006294 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006295 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6296 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006297 }
6298
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006299 /* Nothing to clean up yet */
6300 if (ret)
6301 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006302
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006303 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6304 parent_inode, &key,
6305 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006306 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006307 goto fail_dir_item;
6308 else if (ret) {
6309 btrfs_abort_transaction(trans, root, ret);
6310 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006311 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006312
6313 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6314 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006315 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006316 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6317 ret = btrfs_update_inode(trans, root, parent_inode);
6318 if (ret)
6319 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006320 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006321
6322fail_dir_item:
6323 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6324 u64 local_index;
6325 int err;
6326 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6327 key.objectid, root->root_key.objectid,
6328 parent_ino, &local_index, name, name_len);
6329
6330 } else if (add_backref) {
6331 u64 local_index;
6332 int err;
6333
6334 err = btrfs_del_inode_ref(trans, root, name, name_len,
6335 ino, parent_ino, &local_index);
6336 }
6337 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006338}
6339
6340static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006341 struct inode *dir, struct dentry *dentry,
6342 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006343{
Josef Bacika1b075d2010-11-19 20:36:11 +00006344 int err = btrfs_add_link(trans, dir, inode,
6345 dentry->d_name.name, dentry->d_name.len,
6346 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006347 if (err > 0)
6348 err = -EEXIST;
6349 return err;
6350}
6351
Josef Bacik618e21d2007-07-11 10:18:17 -04006352static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006353 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006354{
6355 struct btrfs_trans_handle *trans;
6356 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006357 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006358 int err;
6359 int drop_inode = 0;
6360 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006361 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006362
6363 if (!new_valid_dev(rdev))
6364 return -EINVAL;
6365
Josef Bacik9ed74f22009-09-11 16:12:44 -04006366 /*
6367 * 2 for inode item and ref
6368 * 2 for dir items
6369 * 1 for xattr if selinux is on
6370 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006371 trans = btrfs_start_transaction(root, 5);
6372 if (IS_ERR(trans))
6373 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006374
Li Zefan581bb052011-04-20 10:06:11 +08006375 err = btrfs_find_free_ino(root, &objectid);
6376 if (err)
6377 goto out_unlock;
6378
Josef Bacikaec74772008-07-24 12:12:38 -04006379 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006380 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006381 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006382 if (IS_ERR(inode)) {
6383 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006384 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006385 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006386
Casey Schauflerad19db72011-12-15 10:09:07 -05006387 /*
6388 * If the active LSM wants to access the inode during
6389 * d_instantiate it needs these. Smack checks to see
6390 * if the filesystem supports xattrs by looking at the
6391 * ops vector.
6392 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006393 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006394 init_special_inode(inode, inode->i_mode, rdev);
6395
6396 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006397 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006398 goto out_unlock_inode;
6399
6400 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6401 if (err) {
6402 goto out_unlock_inode;
6403 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006404 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006405 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006406 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006407 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006408
Josef Bacik618e21d2007-07-11 10:18:17 -04006409out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006410 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006411 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006412 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006413 if (drop_inode) {
6414 inode_dec_link_count(inode);
6415 iput(inode);
6416 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006417 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006418
6419out_unlock_inode:
6420 drop_inode = 1;
6421 unlock_new_inode(inode);
6422 goto out_unlock;
6423
Josef Bacik618e21d2007-07-11 10:18:17 -04006424}
6425
Chris Mason39279cc2007-06-12 06:35:45 -04006426static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006427 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006428{
6429 struct btrfs_trans_handle *trans;
6430 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006431 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006432 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006433 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006434 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006435 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006436
Josef Bacik9ed74f22009-09-11 16:12:44 -04006437 /*
6438 * 2 for inode item and ref
6439 * 2 for dir items
6440 * 1 for xattr if selinux is on
6441 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006442 trans = btrfs_start_transaction(root, 5);
6443 if (IS_ERR(trans))
6444 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006445
Li Zefan581bb052011-04-20 10:06:11 +08006446 err = btrfs_find_free_ino(root, &objectid);
6447 if (err)
6448 goto out_unlock;
6449
Josef Bacikaec74772008-07-24 12:12:38 -04006450 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006451 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006452 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006453 if (IS_ERR(inode)) {
6454 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006455 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006456 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006457 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006458 /*
6459 * If the active LSM wants to access the inode during
6460 * d_instantiate it needs these. Smack checks to see
6461 * if the filesystem supports xattrs by looking at the
6462 * ops vector.
6463 */
6464 inode->i_fop = &btrfs_file_operations;
6465 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006466 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006467
6468 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6469 if (err)
6470 goto out_unlock_inode;
6471
6472 err = btrfs_update_inode(trans, root, inode);
6473 if (err)
6474 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006475
Josef Bacika1b075d2010-11-19 20:36:11 +00006476 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006477 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006478 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006479
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006480 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006481 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006482 d_instantiate(dentry, inode);
6483
Chris Mason39279cc2007-06-12 06:35:45 -04006484out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006485 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006486 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006487 inode_dec_link_count(inode);
6488 iput(inode);
6489 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006490 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006491 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006492 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006493
6494out_unlock_inode:
6495 unlock_new_inode(inode);
6496 goto out_unlock;
6497
Chris Mason39279cc2007-06-12 06:35:45 -04006498}
6499
6500static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6501 struct dentry *dentry)
6502{
6503 struct btrfs_trans_handle *trans;
6504 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006505 struct inode *inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006506 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006507 int err;
6508 int drop_inode = 0;
6509
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006510 /* do not allow sys_link's with other subvols of the same device */
6511 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006512 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006513
Mark Fashehf1863732012-08-08 11:32:27 -07006514 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006515 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006516
Chris Mason3de45862008-11-17 21:02:50 -05006517 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006518 if (err)
6519 goto fail;
6520
Yan, Zhenga22285a2010-05-16 10:48:46 -04006521 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006522 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006523 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006524 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006525 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006526 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006527 if (IS_ERR(trans)) {
6528 err = PTR_ERR(trans);
6529 goto fail;
6530 }
Chris Mason5f39d392007-10-15 16:14:19 -04006531
Miao Xie67de1172013-12-26 13:07:06 +08006532 /* There are several dir indexes for this inode, clear the cache. */
6533 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006534 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006535 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006536 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006537 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006538 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006539
Josef Bacika1b075d2010-11-19 20:36:11 +00006540 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006541
Yan, Zhenga5719522009-09-24 09:17:31 -04006542 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006543 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006544 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006545 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006546 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006547 if (err)
6548 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006549 if (inode->i_nlink == 1) {
6550 /*
6551 * If new hard link count is 1, it's a file created
6552 * with open(2) O_TMPFILE flag.
6553 */
6554 err = btrfs_orphan_del(trans, inode);
6555 if (err)
6556 goto fail;
6557 }
Al Viro08c422c2011-12-23 07:58:13 -05006558 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006559 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006560 }
Chris Mason39279cc2007-06-12 06:35:45 -04006561
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006562 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006563 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006564fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006565 if (drop_inode) {
6566 inode_dec_link_count(inode);
6567 iput(inode);
6568 }
Liu Bob53d3f52012-11-14 14:34:34 +00006569 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006570 return err;
6571}
6572
Al Viro18bb1db2011-07-26 01:41:39 -04006573static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006574{
Chris Masonb9d86662008-05-02 16:13:49 -04006575 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006576 struct btrfs_trans_handle *trans;
6577 struct btrfs_root *root = BTRFS_I(dir)->root;
6578 int err = 0;
6579 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006580 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006581 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006582
Josef Bacik9ed74f22009-09-11 16:12:44 -04006583 /*
6584 * 2 items for inode and ref
6585 * 2 items for dir items
6586 * 1 for xattr if selinux is on
6587 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006588 trans = btrfs_start_transaction(root, 5);
6589 if (IS_ERR(trans))
6590 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006591
Li Zefan581bb052011-04-20 10:06:11 +08006592 err = btrfs_find_free_ino(root, &objectid);
6593 if (err)
6594 goto out_fail;
6595
Josef Bacikaec74772008-07-24 12:12:38 -04006596 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006597 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006598 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006599 if (IS_ERR(inode)) {
6600 err = PTR_ERR(inode);
6601 goto out_fail;
6602 }
Chris Mason5f39d392007-10-15 16:14:19 -04006603
Chris Mason39279cc2007-06-12 06:35:45 -04006604 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006605 /* these must be set before we unlock the inode */
6606 inode->i_op = &btrfs_dir_inode_operations;
6607 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006608
Eric Paris2a7dba32011-02-01 11:05:39 -05006609 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006610 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006611 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006612
Chris Masondbe674a2008-07-17 12:54:05 -04006613 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006614 err = btrfs_update_inode(trans, root, inode);
6615 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006616 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006617
Josef Bacika1b075d2010-11-19 20:36:11 +00006618 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6619 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006620 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006621 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006622
Chris Mason39279cc2007-06-12 06:35:45 -04006623 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006624 /*
6625 * mkdir is special. We're unlocking after we call d_instantiate
6626 * to avoid a race with nfsd calling d_instantiate.
6627 */
6628 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006629 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006630
6631out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006632 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006633 if (drop_on_err) {
6634 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006635 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006636 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006637 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006638 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006639 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006640
6641out_fail_inode:
6642 unlock_new_inode(inode);
6643 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006644}
6645
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006646/* Find next extent map of a given extent map, caller needs to ensure locks */
6647static struct extent_map *next_extent_map(struct extent_map *em)
6648{
6649 struct rb_node *next;
6650
6651 next = rb_next(&em->rb_node);
6652 if (!next)
6653 return NULL;
6654 return container_of(next, struct extent_map, rb_node);
6655}
6656
6657static struct extent_map *prev_extent_map(struct extent_map *em)
6658{
6659 struct rb_node *prev;
6660
6661 prev = rb_prev(&em->rb_node);
6662 if (!prev)
6663 return NULL;
6664 return container_of(prev, struct extent_map, rb_node);
6665}
6666
Chris Masond352ac62008-09-29 15:18:18 -04006667/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006668 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006669 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006670 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006671 */
Chris Mason3b951512008-04-17 11:29:12 -04006672static int merge_extent_mapping(struct extent_map_tree *em_tree,
6673 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006674 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006675 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006676{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006677 struct extent_map *prev;
6678 struct extent_map *next;
6679 u64 start;
6680 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006681 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006682
Chris Masone6dcd2d2008-07-17 12:53:50 -04006683 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006684
6685 if (existing->start > map_start) {
6686 next = existing;
6687 prev = prev_extent_map(next);
6688 } else {
6689 prev = existing;
6690 next = next_extent_map(prev);
6691 }
6692
6693 start = prev ? extent_map_end(prev) : em->start;
6694 start = max_t(u64, start, em->start);
6695 end = next ? next->start : extent_map_end(em);
6696 end = min_t(u64, end, extent_map_end(em));
6697 start_diff = start - em->start;
6698 em->start = start;
6699 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006700 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6701 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006702 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006703 em->block_len -= start_diff;
6704 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006705 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006706}
6707
Chris Masonc8b97812008-10-29 14:49:59 -04006708static noinline int uncompress_inline(struct btrfs_path *path,
6709 struct inode *inode, struct page *page,
6710 size_t pg_offset, u64 extent_offset,
6711 struct btrfs_file_extent_item *item)
6712{
6713 int ret;
6714 struct extent_buffer *leaf = path->nodes[0];
6715 char *tmp;
6716 size_t max_size;
6717 unsigned long inline_size;
6718 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006719 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006720
6721 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006722 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006723 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6724 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006725 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006726 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006727 if (!tmp)
6728 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006729 ptr = btrfs_file_extent_inline_start(item);
6730
6731 read_extent_buffer(leaf, tmp, ptr, inline_size);
6732
Chris Mason5b050f02008-11-11 09:34:41 -05006733 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006734 ret = btrfs_decompress(compress_type, tmp, page,
6735 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006736 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006737 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006738}
6739
Chris Masond352ac62008-09-29 15:18:18 -04006740/*
6741 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006742 * the ugly parts come from merging extents from the disk with the in-ram
6743 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006744 * where the in-ram extents might be locked pending data=ordered completion.
6745 *
6746 * This also copies inline extents directly into the page.
6747 */
Chris Masond3977122009-01-05 21:25:51 -05006748
Chris Masona52d9a82007-08-27 16:49:44 -04006749struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006750 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006751 int create)
6752{
6753 int ret;
6754 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006755 u64 extent_start = 0;
6756 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006757 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006758 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006759 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006760 struct btrfs_root *root = BTRFS_I(inode)->root;
6761 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006762 struct extent_buffer *leaf;
6763 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006764 struct extent_map *em = NULL;
6765 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006766 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006767 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006768 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006769
Chris Masona52d9a82007-08-27 16:49:44 -04006770again:
Chris Mason890871b2009-09-02 16:24:52 -04006771 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006772 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006773 if (em)
6774 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006775 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006776
Chris Masona52d9a82007-08-27 16:49:44 -04006777 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006778 if (em->start > start || em->start + em->len <= start)
6779 free_extent_map(em);
6780 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006781 free_extent_map(em);
6782 else
6783 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006784 }
David Sterba172ddd62011-04-21 00:48:27 +02006785 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006786 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006787 err = -ENOMEM;
6788 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006789 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006790 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006791 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006792 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006793 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006794 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006795
6796 if (!path) {
6797 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006798 if (!path) {
6799 err = -ENOMEM;
6800 goto out;
6801 }
6802 /*
6803 * Chances are we'll be called again, so go ahead and do
6804 * readahead
6805 */
6806 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006807 }
6808
Chris Mason179e29e2007-11-01 11:28:41 -04006809 ret = btrfs_lookup_file_extent(trans, root, path,
6810 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006811 if (ret < 0) {
6812 err = ret;
6813 goto out;
6814 }
6815
6816 if (ret != 0) {
6817 if (path->slots[0] == 0)
6818 goto not_found;
6819 path->slots[0]--;
6820 }
6821
Chris Mason5f39d392007-10-15 16:14:19 -04006822 leaf = path->nodes[0];
6823 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006824 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006825 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006826 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006827 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006828 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006829 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006830 /*
6831 * If we backup past the first extent we want to move forward
6832 * and see if there is an extent in front of us, otherwise we'll
6833 * say there is a hole for our whole search range which can
6834 * cause problems.
6835 */
6836 extent_end = start;
6837 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006838 }
6839
Chris Mason5f39d392007-10-15 16:14:19 -04006840 found_type = btrfs_file_extent_type(leaf, item);
6841 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006842 if (found_type == BTRFS_FILE_EXTENT_REG ||
6843 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006844 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006845 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006846 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6847 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006848 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006849 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006850 }
Josef Bacik25a50342013-10-14 12:08:38 -04006851next:
Yan Zheng9036c102008-10-30 14:19:41 -04006852 if (start >= extent_end) {
6853 path->slots[0]++;
6854 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6855 ret = btrfs_next_leaf(root, path);
6856 if (ret < 0) {
6857 err = ret;
6858 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006859 }
Yan Zheng9036c102008-10-30 14:19:41 -04006860 if (ret > 0)
6861 goto not_found;
6862 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006863 }
Yan Zheng9036c102008-10-30 14:19:41 -04006864 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6865 if (found_key.objectid != objectid ||
6866 found_key.type != BTRFS_EXTENT_DATA_KEY)
6867 goto not_found;
6868 if (start + len <= found_key.offset)
6869 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006870 if (start > found_key.offset)
6871 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006872 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006873 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006874 em->len = found_key.offset - start;
6875 goto not_found_em;
6876 }
6877
Filipe Manana7ffbb592014-06-09 03:48:05 +01006878 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6879
Yan Zhengd899e052008-10-30 14:25:28 -04006880 if (found_type == BTRFS_FILE_EXTENT_REG ||
6881 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006882 goto insert;
6883 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006884 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006885 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006886 size_t size;
6887 size_t extent_offset;
6888 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006889
Filipe Manana7ffbb592014-06-09 03:48:05 +01006890 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006891 goto out;
Yan689f9342007-10-29 11:41:07 -04006892
Chris Mason514ac8a2014-01-03 21:07:00 -08006893 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006894 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006895 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006896 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006897 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006898 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006899 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006900 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006901 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006902 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006903 if (btrfs_file_extent_compression(leaf, item) !=
6904 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006905 ret = uncompress_inline(path, inode, page,
6906 pg_offset,
6907 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006908 if (ret) {
6909 err = ret;
6910 goto out;
6911 }
Chris Masonc8b97812008-10-29 14:49:59 -04006912 } else {
6913 map = kmap(page);
6914 read_extent_buffer(leaf, map + pg_offset, ptr,
6915 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006916 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6917 memset(map + pg_offset + copy_size, 0,
6918 PAGE_CACHE_SIZE - pg_offset -
6919 copy_size);
6920 }
Chris Masonc8b97812008-10-29 14:49:59 -04006921 kunmap(page);
6922 }
Chris Mason179e29e2007-11-01 11:28:41 -04006923 flush_dcache_page(page);
6924 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006925 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006926 if (!trans) {
6927 kunmap(page);
6928 free_extent_map(em);
6929 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006930
David Sterbab3b4aa72011-04-21 01:20:15 +02006931 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006932 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006933
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006934 if (IS_ERR(trans))
6935 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006936 goto again;
6937 }
Chris Masonc8b97812008-10-29 14:49:59 -04006938 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006939 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006940 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006941 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006942 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006943 }
Chris Masond1310b22008-01-24 16:13:08 -05006944 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006945 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006946 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006947 }
6948not_found:
6949 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006950 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006951 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006952not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006953 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006954 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006955insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006956 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006957 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006958 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006959 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006960 err = -EIO;
6961 goto out;
6962 }
Chris Masond1310b22008-01-24 16:13:08 -05006963
6964 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006965 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006966 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006967 /* it is possible that someone inserted the extent into the tree
6968 * while we had the lock dropped. It is also possible that
6969 * an overlapping map exists in the tree
6970 */
Chris Masona52d9a82007-08-27 16:49:44 -04006971 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006972 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006973
6974 ret = 0;
6975
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006976 existing = search_extent_mapping(em_tree, start, len);
6977 /*
6978 * existing will always be non-NULL, since there must be
6979 * extent causing the -EEXIST.
6980 */
6981 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006982 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006983 /*
6984 * The existing extent map is the one nearest to
6985 * the [start, start + len) range which overlaps
6986 */
6987 err = merge_extent_mapping(em_tree, existing,
6988 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006989 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006990 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006991 free_extent_map(em);
6992 em = NULL;
6993 }
6994 } else {
6995 free_extent_map(em);
6996 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006997 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006998 }
Chris Masona52d9a82007-08-27 16:49:44 -04006999 }
Chris Mason890871b2009-09-02 16:24:52 -04007000 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007001out:
liubo1abe9b82011-03-24 11:18:59 +00007002
Steven Rostedt4cd85872013-11-14 22:57:29 -05007003 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00007004
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007005 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007006 if (trans) {
7007 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05007008 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007009 err = ret;
7010 }
Chris Masona52d9a82007-08-27 16:49:44 -04007011 if (err) {
7012 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007013 return ERR_PTR(err);
7014 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007015 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007016 return em;
7017}
7018
Chris Masonec29ed52011-02-23 16:23:20 -05007019struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7020 size_t pg_offset, u64 start, u64 len,
7021 int create)
7022{
7023 struct extent_map *em;
7024 struct extent_map *hole_em = NULL;
7025 u64 range_start = start;
7026 u64 end;
7027 u64 found;
7028 u64 found_end;
7029 int err = 0;
7030
7031 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7032 if (IS_ERR(em))
7033 return em;
7034 if (em) {
7035 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007036 * if our em maps to
7037 * - a hole or
7038 * - a pre-alloc extent,
7039 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007040 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007041 if (em->block_start != EXTENT_MAP_HOLE &&
7042 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007043 return em;
7044 else
7045 hole_em = em;
7046 }
7047
7048 /* check to see if we've wrapped (len == -1 or similar) */
7049 end = start + len;
7050 if (end < start)
7051 end = (u64)-1;
7052 else
7053 end -= 1;
7054
7055 em = NULL;
7056
7057 /* ok, we didn't find anything, lets look for delalloc */
7058 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7059 end, len, EXTENT_DELALLOC, 1);
7060 found_end = range_start + found;
7061 if (found_end < range_start)
7062 found_end = (u64)-1;
7063
7064 /*
7065 * we didn't find anything useful, return
7066 * the original results from get_extent()
7067 */
7068 if (range_start > end || found_end <= start) {
7069 em = hole_em;
7070 hole_em = NULL;
7071 goto out;
7072 }
7073
7074 /* adjust the range_start to make sure it doesn't
7075 * go backwards from the start they passed in
7076 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307077 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007078 found = found_end - range_start;
7079
7080 if (found > 0) {
7081 u64 hole_start = start;
7082 u64 hole_len = len;
7083
David Sterba172ddd62011-04-21 00:48:27 +02007084 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007085 if (!em) {
7086 err = -ENOMEM;
7087 goto out;
7088 }
7089 /*
7090 * when btrfs_get_extent can't find anything it
7091 * returns one huge hole
7092 *
7093 * make sure what it found really fits our range, and
7094 * adjust to make sure it is based on the start from
7095 * the caller
7096 */
7097 if (hole_em) {
7098 u64 calc_end = extent_map_end(hole_em);
7099
7100 if (calc_end <= start || (hole_em->start > end)) {
7101 free_extent_map(hole_em);
7102 hole_em = NULL;
7103 } else {
7104 hole_start = max(hole_em->start, start);
7105 hole_len = calc_end - hole_start;
7106 }
7107 }
7108 em->bdev = NULL;
7109 if (hole_em && range_start > hole_start) {
7110 /* our hole starts before our delalloc, so we
7111 * have to return just the parts of the hole
7112 * that go until the delalloc starts
7113 */
7114 em->len = min(hole_len,
7115 range_start - hole_start);
7116 em->start = hole_start;
7117 em->orig_start = hole_start;
7118 /*
7119 * don't adjust block start at all,
7120 * it is fixed at EXTENT_MAP_HOLE
7121 */
7122 em->block_start = hole_em->block_start;
7123 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007124 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7125 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007126 } else {
7127 em->start = range_start;
7128 em->len = found;
7129 em->orig_start = range_start;
7130 em->block_start = EXTENT_MAP_DELALLOC;
7131 em->block_len = found;
7132 }
7133 } else if (hole_em) {
7134 return hole_em;
7135 }
7136out:
7137
7138 free_extent_map(hole_em);
7139 if (err) {
7140 free_extent_map(em);
7141 return ERR_PTR(err);
7142 }
7143 return em;
7144}
7145
Josef Bacik4b46fce2010-05-23 11:00:55 -04007146static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7147 u64 start, u64 len)
7148{
7149 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007150 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007151 struct btrfs_key ins;
7152 u64 alloc_hint;
7153 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007154
Josef Bacik4b46fce2010-05-23 11:00:55 -04007155 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04007156 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007157 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007158 if (ret)
7159 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007160
Josef Bacik70c8a912012-10-11 16:54:30 -04007161 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007162 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04007163 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08007164 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007165 return em;
7166 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007167
7168 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
7169 ins.offset, ins.offset, 0);
7170 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08007171 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007172 free_extent_map(em);
7173 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007174 }
Josef Bacik00361582013-08-14 14:02:47 -04007175
Josef Bacik4b46fce2010-05-23 11:00:55 -04007176 return em;
7177}
7178
Chris Mason46bfbb52010-05-26 11:04:10 -04007179/*
7180 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7181 * block must be cow'd
7182 */
Josef Bacik00361582013-08-14 14:02:47 -04007183noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007184 u64 *orig_start, u64 *orig_block_len,
7185 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007186{
Josef Bacik00361582013-08-14 14:02:47 -04007187 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007188 struct btrfs_path *path;
7189 int ret;
7190 struct extent_buffer *leaf;
7191 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007192 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007193 struct btrfs_file_extent_item *fi;
7194 struct btrfs_key key;
7195 u64 disk_bytenr;
7196 u64 backref_offset;
7197 u64 extent_end;
7198 u64 num_bytes;
7199 int slot;
7200 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007201 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007202
Chris Mason46bfbb52010-05-26 11:04:10 -04007203 path = btrfs_alloc_path();
7204 if (!path)
7205 return -ENOMEM;
7206
Josef Bacik00361582013-08-14 14:02:47 -04007207 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007208 offset, 0);
7209 if (ret < 0)
7210 goto out;
7211
7212 slot = path->slots[0];
7213 if (ret == 1) {
7214 if (slot == 0) {
7215 /* can't find the item, must cow */
7216 ret = 0;
7217 goto out;
7218 }
7219 slot--;
7220 }
7221 ret = 0;
7222 leaf = path->nodes[0];
7223 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007224 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007225 key.type != BTRFS_EXTENT_DATA_KEY) {
7226 /* not our file or wrong item type, must cow */
7227 goto out;
7228 }
7229
7230 if (key.offset > offset) {
7231 /* Wrong offset, must cow */
7232 goto out;
7233 }
7234
7235 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7236 found_type = btrfs_file_extent_type(leaf, fi);
7237 if (found_type != BTRFS_FILE_EXTENT_REG &&
7238 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7239 /* not a regular extent, must cow */
7240 goto out;
7241 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007242
7243 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7244 goto out;
7245
Miao Xiee77751a2013-12-27 21:11:50 +08007246 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7247 if (extent_end <= offset)
7248 goto out;
7249
Chris Mason46bfbb52010-05-26 11:04:10 -04007250 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007251 if (disk_bytenr == 0)
7252 goto out;
7253
7254 if (btrfs_file_extent_compression(leaf, fi) ||
7255 btrfs_file_extent_encryption(leaf, fi) ||
7256 btrfs_file_extent_other_encoding(leaf, fi))
7257 goto out;
7258
Chris Mason46bfbb52010-05-26 11:04:10 -04007259 backref_offset = btrfs_file_extent_offset(leaf, fi);
7260
Josef Bacik7ee9e442013-06-21 16:37:03 -04007261 if (orig_start) {
7262 *orig_start = key.offset - backref_offset;
7263 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7264 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7265 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007266
Chris Mason46bfbb52010-05-26 11:04:10 -04007267 if (btrfs_extent_readonly(root, disk_bytenr))
7268 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007269
7270 num_bytes = min(offset + *len, extent_end) - offset;
7271 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7272 u64 range_end;
7273
7274 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7275 ret = test_range_bit(io_tree, offset, range_end,
7276 EXTENT_DELALLOC, 0, NULL);
7277 if (ret) {
7278 ret = -EAGAIN;
7279 goto out;
7280 }
7281 }
7282
Josef Bacik1bda19e2013-10-18 12:10:36 -04007283 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007284
7285 /*
7286 * look for other files referencing this extent, if we
7287 * find any we must cow
7288 */
Josef Bacik00361582013-08-14 14:02:47 -04007289 trans = btrfs_join_transaction(root);
7290 if (IS_ERR(trans)) {
7291 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007292 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007293 }
7294
7295 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7296 key.offset - backref_offset, disk_bytenr);
7297 btrfs_end_transaction(trans, root);
7298 if (ret) {
7299 ret = 0;
7300 goto out;
7301 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007302
7303 /*
7304 * adjust disk_bytenr and num_bytes to cover just the bytes
7305 * in this extent we are about to write. If there
7306 * are any csums in that range we have to cow in order
7307 * to keep the csums correct
7308 */
7309 disk_bytenr += backref_offset;
7310 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007311 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7312 goto out;
7313 /*
7314 * all of the above have passed, it is safe to overwrite this extent
7315 * without cow
7316 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007317 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007318 ret = 1;
7319out:
7320 btrfs_free_path(path);
7321 return ret;
7322}
7323
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007324bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7325{
7326 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7327 int found = false;
7328 void **pagep = NULL;
7329 struct page *page = NULL;
7330 int start_idx;
7331 int end_idx;
7332
7333 start_idx = start >> PAGE_CACHE_SHIFT;
7334
7335 /*
7336 * end is the last byte in the last page. end == start is legal
7337 */
7338 end_idx = end >> PAGE_CACHE_SHIFT;
7339
7340 rcu_read_lock();
7341
7342 /* Most of the code in this while loop is lifted from
7343 * find_get_page. It's been modified to begin searching from a
7344 * page and return just the first page found in that range. If the
7345 * found idx is less than or equal to the end idx then we know that
7346 * a page exists. If no pages are found or if those pages are
7347 * outside of the range then we're fine (yay!) */
7348 while (page == NULL &&
7349 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7350 page = radix_tree_deref_slot(pagep);
7351 if (unlikely(!page))
7352 break;
7353
7354 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007355 if (radix_tree_deref_retry(page)) {
7356 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007357 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007358 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007359 /*
7360 * Otherwise, shmem/tmpfs must be storing a swap entry
7361 * here as an exceptional entry: so return it without
7362 * attempting to raise page count.
7363 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007364 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007365 break; /* TODO: Is this relevant for this use case? */
7366 }
7367
Filipe Manana91405152014-06-05 13:22:24 +01007368 if (!page_cache_get_speculative(page)) {
7369 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007370 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007371 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007372
7373 /*
7374 * Has the page moved?
7375 * This is part of the lockless pagecache protocol. See
7376 * include/linux/pagemap.h for details.
7377 */
7378 if (unlikely(page != *pagep)) {
7379 page_cache_release(page);
7380 page = NULL;
7381 }
7382 }
7383
7384 if (page) {
7385 if (page->index <= end_idx)
7386 found = true;
7387 page_cache_release(page);
7388 }
7389
7390 rcu_read_unlock();
7391 return found;
7392}
7393
Josef Bacikeb838e72012-07-31 16:28:48 -04007394static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7395 struct extent_state **cached_state, int writing)
7396{
7397 struct btrfs_ordered_extent *ordered;
7398 int ret = 0;
7399
7400 while (1) {
7401 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7402 0, cached_state);
7403 /*
7404 * We're concerned with the entire range that we're going to be
7405 * doing DIO to, so we need to make sure theres no ordered
7406 * extents in this range.
7407 */
7408 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7409 lockend - lockstart + 1);
7410
7411 /*
7412 * We need to make sure there are no buffered pages in this
7413 * range either, we could have raced between the invalidate in
7414 * generic_file_direct_write and locking the extent. The
7415 * invalidate needs to happen so that reads after a write do not
7416 * get stale data.
7417 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007418 if (!ordered &&
7419 (!writing ||
7420 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007421 break;
7422
7423 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7424 cached_state, GFP_NOFS);
7425
7426 if (ordered) {
7427 btrfs_start_ordered_extent(inode, ordered, 1);
7428 btrfs_put_ordered_extent(ordered);
7429 } else {
7430 /* Screw you mmap */
Filipe Manana728404d2014-10-10 09:43:11 +01007431 ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
Filipe Manana075bdbd2014-10-09 21:18:55 +01007432 if (ret)
7433 break;
7434 ret = filemap_fdatawait_range(inode->i_mapping,
7435 lockstart,
7436 lockend);
Josef Bacikeb838e72012-07-31 16:28:48 -04007437 if (ret)
7438 break;
7439
7440 /*
7441 * If we found a page that couldn't be invalidated just
7442 * fall back to buffered.
7443 */
7444 ret = invalidate_inode_pages2_range(inode->i_mapping,
7445 lockstart >> PAGE_CACHE_SHIFT,
7446 lockend >> PAGE_CACHE_SHIFT);
7447 if (ret)
7448 break;
7449 }
7450
7451 cond_resched();
7452 }
7453
7454 return ret;
7455}
7456
Josef Bacik69ffb542012-09-11 15:40:07 -04007457static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7458 u64 len, u64 orig_start,
7459 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007460 u64 orig_block_len, u64 ram_bytes,
7461 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007462{
7463 struct extent_map_tree *em_tree;
7464 struct extent_map *em;
7465 struct btrfs_root *root = BTRFS_I(inode)->root;
7466 int ret;
7467
7468 em_tree = &BTRFS_I(inode)->extent_tree;
7469 em = alloc_extent_map();
7470 if (!em)
7471 return ERR_PTR(-ENOMEM);
7472
7473 em->start = start;
7474 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007475 em->mod_start = start;
7476 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007477 em->len = len;
7478 em->block_len = block_len;
7479 em->block_start = block_start;
7480 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007481 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007482 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007483 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007484 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7485 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007486 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007487
7488 do {
7489 btrfs_drop_extent_cache(inode, em->start,
7490 em->start + em->len - 1, 0);
7491 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007492 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007493 write_unlock(&em_tree->lock);
7494 } while (ret == -EEXIST);
7495
7496 if (ret) {
7497 free_extent_map(em);
7498 return ERR_PTR(ret);
7499 }
7500
7501 return em;
7502}
7503
chandan50745b02015-08-28 21:10:13 +05307504struct btrfs_dio_data {
7505 u64 outstanding_extents;
7506 u64 reserve;
7507};
Josef Bacik69ffb542012-09-11 15:40:07 -04007508
Filipe Manana9c9464c2015-11-04 09:52:04 +00007509static void adjust_dio_outstanding_extents(struct inode *inode,
7510 struct btrfs_dio_data *dio_data,
7511 const u64 len)
7512{
7513 unsigned num_extents;
7514
7515 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7516 BTRFS_MAX_EXTENT_SIZE);
7517 /*
7518 * If we have an outstanding_extents count still set then we're
7519 * within our reservation, otherwise we need to adjust our inode
7520 * counter appropriately.
7521 */
7522 if (dio_data->outstanding_extents) {
7523 dio_data->outstanding_extents -= num_extents;
7524 } else {
7525 spin_lock(&BTRFS_I(inode)->lock);
7526 BTRFS_I(inode)->outstanding_extents += num_extents;
7527 spin_unlock(&BTRFS_I(inode)->lock);
7528 }
7529}
7530
Josef Bacik4b46fce2010-05-23 11:00:55 -04007531static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7532 struct buffer_head *bh_result, int create)
7533{
7534 struct extent_map *em;
7535 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007536 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307537 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007538 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007539 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007540 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007541 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007542 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007543
Miao Xie172a5042013-02-21 02:48:22 -07007544 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007545 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007546 else
Josef Bacikc3298612012-08-03 16:49:19 -04007547 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007548
Josef Bacikc3298612012-08-03 16:49:19 -04007549 lockstart = start;
7550 lockend = start + len - 1;
7551
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007552 if (current->journal_info) {
7553 /*
7554 * Need to pull our outstanding extents and set journal_info to NULL so
7555 * that anything that needs to check if there's a transction doesn't get
7556 * confused.
7557 */
chandan50745b02015-08-28 21:10:13 +05307558 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007559 current->journal_info = NULL;
7560 }
7561
Josef Bacikeb838e72012-07-31 16:28:48 -04007562 /*
7563 * If this errors out it's because we couldn't invalidate pagecache for
7564 * this range and we need to fallback to buffered.
7565 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007566 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7567 create)) {
7568 ret = -ENOTBLK;
7569 goto err;
7570 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007571
Josef Bacik4b46fce2010-05-23 11:00:55 -04007572 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007573 if (IS_ERR(em)) {
7574 ret = PTR_ERR(em);
7575 goto unlock_err;
7576 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007577
7578 /*
7579 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7580 * io. INLINE is special, and we could probably kludge it in here, but
7581 * it's still buffered so for safety lets just fall back to the generic
7582 * buffered path.
7583 *
7584 * For COMPRESSED we _have_ to read the entire extent in so we can
7585 * decompress it, so there will be buffering required no matter what we
7586 * do, so go ahead and fallback to buffered.
7587 *
7588 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7589 * to buffered IO. Don't blame me, this is the price we pay for using
7590 * the generic code.
7591 */
7592 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7593 em->block_start == EXTENT_MAP_INLINE) {
7594 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007595 ret = -ENOTBLK;
7596 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007597 }
7598
7599 /* Just a good old fashioned hole, return */
7600 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7601 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7602 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007603 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007604 }
7605
7606 /*
7607 * We don't allocate a new extent in the following cases
7608 *
7609 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7610 * existing extent.
7611 * 2) The extent is marked as PREALLOC. We're good to go here and can
7612 * just use the extent.
7613 *
7614 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007615 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007616 len = min(len, em->len - (start - em->start));
7617 lockstart = start + len;
7618 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007619 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007620
7621 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7622 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7623 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007624 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007625 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007626
7627 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7628 type = BTRFS_ORDERED_PREALLOC;
7629 else
7630 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007631 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007632 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007633
Josef Bacik00361582013-08-14 14:02:47 -04007634 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007635 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007636 if (type == BTRFS_ORDERED_PREALLOC) {
7637 free_extent_map(em);
7638 em = create_pinned_em(inode, start, len,
7639 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007640 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007641 orig_block_len,
7642 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007643 if (IS_ERR(em)) {
7644 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007645 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007646 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007647 }
7648
Chris Mason46bfbb52010-05-26 11:04:10 -04007649 ret = btrfs_add_ordered_extent_dio(inode, start,
7650 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007651 if (ret) {
7652 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007653 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007654 }
7655 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007656 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007657 }
Josef Bacik00361582013-08-14 14:02:47 -04007658
Chris Mason46bfbb52010-05-26 11:04:10 -04007659 /*
7660 * this will cow the extent, reset the len in case we changed
7661 * it above
7662 */
7663 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007664 free_extent_map(em);
7665 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007666 if (IS_ERR(em)) {
7667 ret = PTR_ERR(em);
7668 goto unlock_err;
7669 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007670 len = min(len, em->len - (start - em->start));
7671unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007672 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7673 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007674 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007675 bh_result->b_bdev = em->bdev;
7676 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007677 if (create) {
7678 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7679 set_buffer_new(bh_result);
7680
7681 /*
7682 * Need to update the i_size under the extent lock so buffered
7683 * readers will get the updated i_size when we unlock.
7684 */
7685 if (start + len > i_size_read(inode))
7686 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007687
Filipe Manana9c9464c2015-11-04 09:52:04 +00007688 adjust_dio_outstanding_extents(inode, dio_data, len);
Qu Wenruo7cf5b972015-09-08 17:25:55 +08007689 btrfs_free_reserved_data_space(inode, start, len);
chandan50745b02015-08-28 21:10:13 +05307690 WARN_ON(dio_data->reserve < len);
7691 dio_data->reserve -= len;
7692 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007693 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007694
Josef Bacikeb838e72012-07-31 16:28:48 -04007695 /*
7696 * In the case of write we need to clear and unlock the entire range,
7697 * in the case of read we need to unlock only the end area that we
7698 * aren't using if there is any left over space.
7699 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007700 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007701 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7702 lockend, unlock_bits, 1, 0,
7703 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007704 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007705 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007706 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007707
Josef Bacik4b46fce2010-05-23 11:00:55 -04007708 free_extent_map(em);
7709
7710 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007711
7712unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007713 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7714 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007715err:
chandan50745b02015-08-28 21:10:13 +05307716 if (dio_data)
7717 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007718 /*
7719 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7720 * write less data then expected, so that we don't underflow our inode's
7721 * outstanding extents counter.
7722 */
7723 if (create && dio_data)
7724 adjust_dio_outstanding_extents(inode, dio_data, len);
7725
Josef Bacikeb838e72012-07-31 16:28:48 -04007726 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007727}
7728
Miao Xie8b110e32014-09-12 18:44:03 +08007729static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7730 int rw, int mirror_num)
7731{
7732 struct btrfs_root *root = BTRFS_I(inode)->root;
7733 int ret;
7734
7735 BUG_ON(rw & REQ_WRITE);
7736
7737 bio_get(bio);
7738
7739 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7740 BTRFS_WQ_ENDIO_DIO_REPAIR);
7741 if (ret)
7742 goto err;
7743
7744 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7745err:
7746 bio_put(bio);
7747 return ret;
7748}
7749
7750static int btrfs_check_dio_repairable(struct inode *inode,
7751 struct bio *failed_bio,
7752 struct io_failure_record *failrec,
7753 int failed_mirror)
7754{
7755 int num_copies;
7756
7757 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7758 failrec->logical, failrec->len);
7759 if (num_copies == 1) {
7760 /*
7761 * we only have a single copy of the data, so don't bother with
7762 * all the retry and error correction code that follows. no
7763 * matter what the error is, it is very likely to persist.
7764 */
7765 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7766 num_copies, failrec->this_mirror, failed_mirror);
7767 return 0;
7768 }
7769
7770 failrec->failed_mirror = failed_mirror;
7771 failrec->this_mirror++;
7772 if (failrec->this_mirror == failed_mirror)
7773 failrec->this_mirror++;
7774
7775 if (failrec->this_mirror > num_copies) {
7776 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7777 num_copies, failrec->this_mirror, failed_mirror);
7778 return 0;
7779 }
7780
7781 return 1;
7782}
7783
7784static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7785 struct page *page, u64 start, u64 end,
7786 int failed_mirror, bio_end_io_t *repair_endio,
7787 void *repair_arg)
7788{
7789 struct io_failure_record *failrec;
7790 struct bio *bio;
7791 int isector;
7792 int read_mode;
7793 int ret;
7794
7795 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7796
7797 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7798 if (ret)
7799 return ret;
7800
7801 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7802 failed_mirror);
7803 if (!ret) {
7804 free_io_failure(inode, failrec);
7805 return -EIO;
7806 }
7807
7808 if (failed_bio->bi_vcnt > 1)
7809 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7810 else
7811 read_mode = READ_SYNC;
7812
7813 isector = start - btrfs_io_bio(failed_bio)->logical;
7814 isector >>= inode->i_sb->s_blocksize_bits;
7815 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7816 0, isector, repair_endio, repair_arg);
7817 if (!bio) {
7818 free_io_failure(inode, failrec);
7819 return -EIO;
7820 }
7821
7822 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7823 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7824 read_mode, failrec->this_mirror, failrec->in_validation);
7825
7826 ret = submit_dio_repair_bio(inode, bio, read_mode,
7827 failrec->this_mirror);
7828 if (ret) {
7829 free_io_failure(inode, failrec);
7830 bio_put(bio);
7831 }
7832
7833 return ret;
7834}
7835
7836struct btrfs_retry_complete {
7837 struct completion done;
7838 struct inode *inode;
7839 u64 start;
7840 int uptodate;
7841};
7842
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007843static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007844{
7845 struct btrfs_retry_complete *done = bio->bi_private;
7846 struct bio_vec *bvec;
7847 int i;
7848
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007849 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007850 goto end;
7851
7852 done->uptodate = 1;
7853 bio_for_each_segment_all(bvec, bio, i)
7854 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7855end:
7856 complete(&done->done);
7857 bio_put(bio);
7858}
7859
7860static int __btrfs_correct_data_nocsum(struct inode *inode,
7861 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007862{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007863 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007864 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007865 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007866 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007867 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007868
Miao Xiec1dc0892014-09-12 18:43:56 +08007869 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007870 done.inode = inode;
7871
7872 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7873try_again:
7874 done.uptodate = 0;
7875 done.start = start;
7876 init_completion(&done.done);
7877
7878 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7879 start + bvec->bv_len - 1,
7880 io_bio->mirror_num,
7881 btrfs_retry_endio_nocsum, &done);
7882 if (ret)
7883 return ret;
7884
7885 wait_for_completion(&done.done);
7886
7887 if (!done.uptodate) {
7888 /* We might have another mirror, so try again */
7889 goto try_again;
7890 }
7891
7892 start += bvec->bv_len;
7893 }
7894
7895 return 0;
7896}
7897
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007898static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007899{
7900 struct btrfs_retry_complete *done = bio->bi_private;
7901 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7902 struct bio_vec *bvec;
7903 int uptodate;
7904 int ret;
7905 int i;
7906
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007907 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007908 goto end;
7909
7910 uptodate = 1;
7911 bio_for_each_segment_all(bvec, bio, i) {
7912 ret = __readpage_endio_check(done->inode, io_bio, i,
7913 bvec->bv_page, 0,
7914 done->start, bvec->bv_len);
7915 if (!ret)
7916 clean_io_failure(done->inode, done->start,
7917 bvec->bv_page, 0);
7918 else
7919 uptodate = 0;
7920 }
7921
7922 done->uptodate = uptodate;
7923end:
7924 complete(&done->done);
7925 bio_put(bio);
7926}
7927
7928static int __btrfs_subio_endio_read(struct inode *inode,
7929 struct btrfs_io_bio *io_bio, int err)
7930{
7931 struct bio_vec *bvec;
7932 struct btrfs_retry_complete done;
7933 u64 start;
7934 u64 offset = 0;
7935 int i;
7936 int ret;
7937
7938 err = 0;
7939 start = io_bio->logical;
7940 done.inode = inode;
7941
Miao Xiec1dc0892014-09-12 18:43:56 +08007942 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007943 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7944 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007945 if (likely(!ret))
7946 goto next;
7947try_again:
7948 done.uptodate = 0;
7949 done.start = start;
7950 init_completion(&done.done);
7951
7952 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7953 start + bvec->bv_len - 1,
7954 io_bio->mirror_num,
7955 btrfs_retry_endio, &done);
7956 if (ret) {
7957 err = ret;
7958 goto next;
7959 }
7960
7961 wait_for_completion(&done.done);
7962
7963 if (!done.uptodate) {
7964 /* We might have another mirror, so try again */
7965 goto try_again;
7966 }
7967next:
7968 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007969 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007970 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007971
7972 return err;
7973}
7974
Miao Xie8b110e32014-09-12 18:44:03 +08007975static int btrfs_subio_endio_read(struct inode *inode,
7976 struct btrfs_io_bio *io_bio, int err)
7977{
7978 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7979
7980 if (skip_csum) {
7981 if (unlikely(err))
7982 return __btrfs_correct_data_nocsum(inode, io_bio);
7983 else
7984 return 0;
7985 } else {
7986 return __btrfs_subio_endio_read(inode, io_bio, err);
7987 }
7988}
7989
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007990static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08007991{
7992 struct btrfs_dio_private *dip = bio->bi_private;
7993 struct inode *inode = dip->inode;
7994 struct bio *dio_bio;
7995 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007996 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08007997
Miao Xie8b110e32014-09-12 18:44:03 +08007998 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7999 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008000
Josef Bacik4b46fce2010-05-23 11:00:55 -04008001 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008002 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008003 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008004
Josef Bacik4b46fce2010-05-23 11:00:55 -04008005 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008006
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008007 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008008
8009 if (io_bio->end_io)
8010 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008011 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008012}
8013
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008014static void btrfs_endio_direct_write(struct bio *bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008015{
8016 struct btrfs_dio_private *dip = bio->bi_private;
8017 struct inode *inode = dip->inode;
8018 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008019 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008020 u64 ordered_offset = dip->logical_offset;
8021 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04008022 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008023 int ret;
8024
Chris Mason163cf092010-11-28 19:56:33 -05008025again:
8026 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8027 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008028 ordered_bytes,
8029 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008030 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008031 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008032
Liu Bo9e0af232014-08-15 23:36:53 +08008033 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8034 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08008035 btrfs_queue_work(root->fs_info->endio_write_workers,
8036 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008037out_test:
8038 /*
8039 * our bio might span multiple ordered extents. If we haven't
8040 * completed the accounting for the whole dio, go back and try again
8041 */
8042 if (ordered_offset < dip->logical_offset + dip->bytes) {
8043 ordered_bytes = dip->logical_offset + dip->bytes -
8044 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008045 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008046 goto again;
8047 }
Chris Mason9be33952013-05-17 18:30:14 -04008048 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008049
Josef Bacik4b46fce2010-05-23 11:00:55 -04008050 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008051
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008052 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008053 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008054}
8055
Chris Masoneaf25d92010-05-25 09:48:28 -04008056static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
8057 struct bio *bio, int mirror_num,
8058 unsigned long bio_flags, u64 offset)
8059{
8060 int ret;
8061 struct btrfs_root *root = BTRFS_I(inode)->root;
8062 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008063 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008064 return 0;
8065}
8066
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008067static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008068{
8069 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008070 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008071
Miao Xie8b110e32014-09-12 18:44:03 +08008072 if (err)
8073 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
8074 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
8075 btrfs_ino(dip->inode), bio->bi_rw,
8076 (unsigned long long)bio->bi_iter.bi_sector,
8077 bio->bi_iter.bi_size, err);
8078
8079 if (dip->subio_endio)
8080 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008081
8082 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008083 dip->errors = 1;
8084
8085 /*
8086 * before atomic variable goto zero, we must make sure
8087 * dip->errors is perceived to be set.
8088 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008089 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008090 }
8091
8092 /* if there are more bios still pending for this dio, just exit */
8093 if (!atomic_dec_and_test(&dip->pending_bios))
8094 goto out;
8095
Chris Mason9be33952013-05-17 18:30:14 -04008096 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008097 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008098 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008099 dip->dio_bio->bi_error = 0;
8100 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008101 }
8102out:
8103 bio_put(bio);
8104}
8105
8106static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8107 u64 first_sector, gfp_t gfp_flags)
8108{
Chris Masonda2f0f72015-07-02 13:57:22 -07008109 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008110 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008111 if (bio)
8112 bio_associate_current(bio);
8113 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008114}
8115
Miao Xiec1dc0892014-09-12 18:43:56 +08008116static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8117 struct inode *inode,
8118 struct btrfs_dio_private *dip,
8119 struct bio *bio,
8120 u64 file_offset)
8121{
8122 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8123 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8124 int ret;
8125
8126 /*
8127 * We load all the csum data we need when we submit
8128 * the first bio to reduce the csum tree search and
8129 * contention.
8130 */
8131 if (dip->logical_offset == file_offset) {
8132 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8133 file_offset);
8134 if (ret)
8135 return ret;
8136 }
8137
8138 if (bio == dip->orig_bio)
8139 return 0;
8140
8141 file_offset -= dip->logical_offset;
8142 file_offset >>= inode->i_sb->s_blocksize_bits;
8143 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8144
8145 return 0;
8146}
8147
Miao Xiee65e1532010-11-22 03:04:43 +00008148static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8149 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008150 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008151{
Miao Xiefacc8a222013-07-25 19:22:34 +08008152 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00008153 int write = rw & REQ_WRITE;
8154 struct btrfs_root *root = BTRFS_I(inode)->root;
8155 int ret;
8156
Josef Bacikb812ce22012-11-16 13:56:32 -05008157 if (async_submit)
8158 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8159
Miao Xiee65e1532010-11-22 03:04:43 +00008160 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008161
8162 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008163 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8164 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008165 if (ret)
8166 goto err;
8167 }
Miao Xiee65e1532010-11-22 03:04:43 +00008168
Josef Bacik1ae39932011-04-06 14:41:34 -04008169 if (skip_sum)
8170 goto map;
8171
8172 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008173 ret = btrfs_wq_submit_bio(root->fs_info,
8174 inode, rw, bio, 0, 0,
8175 file_offset,
8176 __btrfs_submit_bio_start_direct_io,
8177 __btrfs_submit_bio_done);
8178 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008179 } else if (write) {
8180 /*
8181 * If we aren't doing async submit, calculate the csum of the
8182 * bio now.
8183 */
8184 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8185 if (ret)
8186 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008187 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008188 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8189 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008190 if (ret)
8191 goto err;
8192 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008193map:
8194 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008195err:
8196 bio_put(bio);
8197 return ret;
8198}
8199
8200static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8201 int skip_sum)
8202{
8203 struct inode *inode = dip->inode;
8204 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008205 struct bio *bio;
8206 struct bio *orig_bio = dip->orig_bio;
8207 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008208 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008209 u64 file_offset = dip->logical_offset;
8210 u64 submit_len = 0;
8211 u64 map_length;
8212 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08008213 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04008214 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008215
Kent Overstreet4f024f32013-10-11 15:44:27 -07008216 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008217 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008218 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008219 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008220 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008221
Kent Overstreet4f024f32013-10-11 15:44:27 -07008222 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008223 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008224 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008225 goto submit;
8226 }
8227
David Woodhouse53b381b2013-01-29 18:40:14 -05008228 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008229 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008230 async_submit = 0;
8231 else
8232 async_submit = 1;
8233
Josef Bacik02f57c72011-04-06 14:25:44 -04008234 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8235 if (!bio)
8236 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008237
Josef Bacik02f57c72011-04-06 14:25:44 -04008238 bio->bi_private = dip;
8239 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008240 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008241 atomic_inc(&dip->pending_bios);
8242
Miao Xiee65e1532010-11-22 03:04:43 +00008243 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02008244 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00008245 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02008246 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00008247 /*
8248 * inc the count before we submit the bio so
8249 * we know the end IO handler won't happen before
8250 * we inc the count. Otherwise, the dip might get freed
8251 * before we're done setting it up
8252 */
8253 atomic_inc(&dip->pending_bios);
8254 ret = __btrfs_submit_dio_bio(bio, inode, rw,
8255 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008256 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008257 if (ret) {
8258 bio_put(bio);
8259 atomic_dec(&dip->pending_bios);
8260 goto out_err;
8261 }
8262
Miao Xiee65e1532010-11-22 03:04:43 +00008263 start_sector += submit_len >> 9;
8264 file_offset += submit_len;
8265
8266 submit_len = 0;
8267 nr_pages = 0;
8268
8269 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8270 start_sector, GFP_NOFS);
8271 if (!bio)
8272 goto out_err;
8273 bio->bi_private = dip;
8274 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008275 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008276
Kent Overstreet4f024f32013-10-11 15:44:27 -07008277 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008278 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008279 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008280 &map_length, NULL, 0);
8281 if (ret) {
8282 bio_put(bio);
8283 goto out_err;
8284 }
8285 } else {
8286 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308287 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00008288 bvec++;
8289 }
8290 }
8291
Josef Bacik02f57c72011-04-06 14:25:44 -04008292submit:
Miao Xiee65e1532010-11-22 03:04:43 +00008293 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008294 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008295 if (!ret)
8296 return 0;
8297
8298 bio_put(bio);
8299out_err:
8300 dip->errors = 1;
8301 /*
8302 * before atomic variable goto zero, we must
8303 * make sure dip->errors is perceived to be set.
8304 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008305 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008306 if (atomic_dec_and_test(&dip->pending_bios))
8307 bio_io_error(dip->orig_bio);
8308
8309 /* bio_end_io() will handle error, so we needn't return it */
8310 return 0;
8311}
8312
Chris Mason9be33952013-05-17 18:30:14 -04008313static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8314 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008315{
Filipe Manana61de7182015-07-01 12:13:10 +01008316 struct btrfs_dio_private *dip = NULL;
8317 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008318 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008319 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02008320 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008321 int ret = 0;
8322
8323 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8324
Chris Mason9be33952013-05-17 18:30:14 -04008325 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008326 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008327 ret = -ENOMEM;
8328 goto free_ordered;
8329 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008330
Miao Xiec1dc0892014-09-12 18:43:56 +08008331 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008332 if (!dip) {
8333 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008334 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008335 }
8336
8337 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008338 dip->inode = inode;
8339 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008340 dip->bytes = dio_bio->bi_iter.bi_size;
8341 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008342 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008343 dip->orig_bio = io_bio;
8344 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008345 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008346 btrfs_bio = btrfs_io_bio(io_bio);
8347 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008348
Miao Xiec1dc0892014-09-12 18:43:56 +08008349 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008350 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008351 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008352 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008353 dip->subio_endio = btrfs_subio_endio_read;
8354 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008355
Miao Xiee65e1532010-11-22 03:04:43 +00008356 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8357 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008358 return;
Chris Mason9be33952013-05-17 18:30:14 -04008359
Miao Xie23ea8e52014-09-12 18:43:54 +08008360 if (btrfs_bio->end_io)
8361 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008362
Josef Bacik4b46fce2010-05-23 11:00:55 -04008363free_ordered:
8364 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008365 * If we arrived here it means either we failed to submit the dip
8366 * or we either failed to clone the dio_bio or failed to allocate the
8367 * dip. If we cloned the dio_bio and allocated the dip, we can just
8368 * call bio_endio against our io_bio so that we get proper resource
8369 * cleanup if we fail to submit the dip, otherwise, we must do the
8370 * same as btrfs_endio_direct_[write|read] because we can't call these
8371 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008372 */
Filipe Manana61de7182015-07-01 12:13:10 +01008373 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008374 io_bio->bi_error = -EIO;
8375 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008376 /*
8377 * The end io callbacks free our dip, do the final put on io_bio
8378 * and all the cleanup and final put for dio_bio (through
8379 * dio_end_io()).
8380 */
8381 dip = NULL;
8382 io_bio = NULL;
8383 } else {
8384 if (write) {
8385 struct btrfs_ordered_extent *ordered;
8386
8387 ordered = btrfs_lookup_ordered_extent(inode,
8388 file_offset);
8389 set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
8390 /*
8391 * Decrements our ref on the ordered extent and removes
8392 * the ordered extent from the inode's ordered tree,
8393 * doing all the proper resource cleanup such as for the
8394 * reserved space and waking up any waiters for this
8395 * ordered extent (through btrfs_remove_ordered_extent).
8396 */
8397 btrfs_finish_ordered_io(ordered);
8398 } else {
8399 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8400 file_offset + dio_bio->bi_iter.bi_size - 1);
8401 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008402 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008403 /*
8404 * Releases and cleans up our dio_bio, no need to bio_put()
8405 * nor bio_endio()/bio_io_error() against dio_bio.
8406 */
8407 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008408 }
Filipe Manana61de7182015-07-01 12:13:10 +01008409 if (io_bio)
8410 bio_put(io_bio);
8411 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008412}
8413
Omar Sandoval6f673762015-03-16 04:33:52 -07008414static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008415 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008416{
8417 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008418 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008419 unsigned blocksize_mask = root->sectorsize - 1;
8420 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008421
8422 if (offset & blocksize_mask)
8423 goto out;
8424
Al Viro28060d52014-03-22 05:15:17 -04008425 if (iov_iter_alignment(iter) & blocksize_mask)
8426 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008427
Al Viro28060d52014-03-22 05:15:17 -04008428 /* If this is a write we don't need to check anymore */
Omar Sandoval6f673762015-03-16 04:33:52 -07008429 if (iov_iter_rw(iter) == WRITE)
Al Viro28060d52014-03-22 05:15:17 -04008430 return 0;
8431 /*
8432 * Check to make sure we don't have duplicate iov_base's in this
8433 * iovec, if so return EINVAL, otherwise we'll get csum errors
8434 * when reading back.
8435 */
8436 for (seg = 0; seg < iter->nr_segs; seg++) {
8437 for (i = seg + 1; i < iter->nr_segs; i++) {
8438 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008439 goto out;
8440 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008441 }
8442 retval = 0;
8443out:
8444 return retval;
8445}
Josef Bacikeb838e72012-07-31 16:28:48 -04008446
Omar Sandoval22c61862015-03-16 04:33:53 -07008447static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
8448 loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008449{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008450 struct file *file = iocb->ki_filp;
8451 struct inode *inode = file->f_mapping->host;
chandan50745b02015-08-28 21:10:13 +05308452 struct btrfs_root *root = BTRFS_I(inode)->root;
8453 struct btrfs_dio_data dio_data = { 0 };
Miao Xie09348562013-02-07 10:12:07 +00008454 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008455 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008456 bool wakeup = true;
8457 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008458 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008459
Omar Sandoval6f673762015-03-16 04:33:52 -07008460 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008461 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008462
Jens Axboefe0f07d2015-04-15 17:05:48 -06008463 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008464 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008465
Josef Bacik0e267c42013-07-02 10:38:02 -04008466 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008467 * The generic stuff only does filemap_write_and_wait_range, which
8468 * isn't enough if we've written compressed pages to this area, so
8469 * we need to flush the dirty pages again to make absolutely sure
8470 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008471 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008472 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008473 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8474 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008475 filemap_fdatawrite_range(inode->i_mapping, offset,
8476 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008477
Omar Sandoval6f673762015-03-16 04:33:52 -07008478 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008479 /*
8480 * If the write DIO is beyond the EOF, we need update
8481 * the isize, but it is protected by i_mutex. So we can
8482 * not unlock the i_mutex at this case.
8483 */
8484 if (offset + count <= inode->i_size) {
8485 mutex_unlock(&inode->i_mutex);
8486 relock = true;
8487 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008488 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008489 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008490 goto out;
chandan50745b02015-08-28 21:10:13 +05308491 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008492 BTRFS_MAX_EXTENT_SIZE - 1,
8493 BTRFS_MAX_EXTENT_SIZE);
8494
8495 /*
8496 * We need to know how many extents we reserved so that we can
8497 * do the accounting properly if we go over the number we
8498 * originally calculated. Abuse current->journal_info for this.
8499 */
chandan50745b02015-08-28 21:10:13 +05308500 dio_data.reserve = round_up(count, root->sectorsize);
8501 current->journal_info = &dio_data;
David Sterbaee39b432014-09-30 01:33:33 +02008502 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8503 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008504 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008505 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8506 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008507 }
8508
Omar Sandoval17f8c842015-03-16 04:33:50 -07008509 ret = __blockdev_direct_IO(iocb, inode,
8510 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
8511 iter, offset, btrfs_get_blocks_direct, NULL,
8512 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008513 if (iov_iter_rw(iter) == WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008514 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008515 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308516 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008517 btrfs_delalloc_release_space(inode, offset,
8518 dio_data.reserve);
Liu Boddba1bf2015-06-17 16:59:58 +08008519 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008520 btrfs_delalloc_release_space(inode, offset,
8521 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008522 }
Miao Xie38851cc2013-02-08 07:04:11 +00008523out:
Miao Xie2e60a512013-02-08 07:01:08 +00008524 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008525 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008526 if (relock)
8527 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008528
8529 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008530}
8531
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008532#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8533
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008534static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8535 __u64 start, __u64 len)
8536{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008537 int ret;
8538
8539 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8540 if (ret)
8541 return ret;
8542
Chris Masonec29ed52011-02-23 16:23:20 -05008543 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008544}
8545
Chris Mason9ebefb182007-06-15 13:50:00 -04008546int btrfs_readpage(struct file *file, struct page *page)
8547{
Chris Masond1310b22008-01-24 16:13:08 -05008548 struct extent_io_tree *tree;
8549 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008550 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008551}
Chris Mason1832a6d2007-12-21 16:27:21 -05008552
Chris Mason39279cc2007-06-12 06:35:45 -04008553static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8554{
Chris Masond1310b22008-01-24 16:13:08 -05008555 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04008556
8557
8558 if (current->flags & PF_MEMALLOC) {
8559 redirty_page_for_writepage(wbc, page);
8560 unlock_page(page);
8561 return 0;
8562 }
Chris Masond1310b22008-01-24 16:13:08 -05008563 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008564 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8565}
Chris Mason39279cc2007-06-12 06:35:45 -04008566
Eric Sandeen48a3b632013-04-25 20:41:01 +00008567static int btrfs_writepages(struct address_space *mapping,
8568 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008569{
Chris Masond1310b22008-01-24 16:13:08 -05008570 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008571
Chris Masond1310b22008-01-24 16:13:08 -05008572 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008573 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8574}
8575
Chris Mason3ab2fb52007-11-08 10:59:22 -05008576static int
8577btrfs_readpages(struct file *file, struct address_space *mapping,
8578 struct list_head *pages, unsigned nr_pages)
8579{
Chris Masond1310b22008-01-24 16:13:08 -05008580 struct extent_io_tree *tree;
8581 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008582 return extent_readpages(tree, mapping, pages, nr_pages,
8583 btrfs_get_extent);
8584}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008585static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008586{
Chris Masond1310b22008-01-24 16:13:08 -05008587 struct extent_io_tree *tree;
8588 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008589 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008590
Chris Masond1310b22008-01-24 16:13:08 -05008591 tree = &BTRFS_I(page->mapping->host)->io_tree;
8592 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008593 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008594 if (ret == 1) {
8595 ClearPagePrivate(page);
8596 set_page_private(page, 0);
8597 page_cache_release(page);
8598 }
8599 return ret;
8600}
Chris Mason39279cc2007-06-12 06:35:45 -04008601
Chris Masone6dcd2d2008-07-17 12:53:50 -04008602static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8603{
Chris Mason98509cf2008-09-11 15:51:43 -04008604 if (PageWriteback(page) || PageDirty(page))
8605 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008606 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008607}
8608
Lukas Czernerd47992f2013-05-21 23:17:23 -04008609static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8610 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008611{
Josef Bacik5fd02042012-05-02 14:00:54 -04008612 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008613 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008614 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008615 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008616 u64 page_start = page_offset(page);
8617 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008618 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008619
Chris Mason8b62b722009-09-02 16:53:46 -04008620 /*
8621 * we have the page locked, so new writeback can't start,
8622 * and the dirty bit won't be cleared while we are here.
8623 *
8624 * Wait for IO on this page so that we can safely clear
8625 * the PagePrivate2 bit and do ordered accounting
8626 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008627 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008628
Josef Bacik5fd02042012-05-02 14:00:54 -04008629 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008630 if (offset) {
8631 btrfs_releasepage(page, GFP_NOFS);
8632 return;
8633 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008634
8635 if (!inode_evicting)
8636 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8637 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008638 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008639 /*
8640 * IO on this page will never be started, so we need
8641 * to account for any ordered extents now
8642 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008643 if (!inode_evicting)
8644 clear_extent_bit(tree, page_start, page_end,
8645 EXTENT_DIRTY | EXTENT_DELALLOC |
8646 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8647 EXTENT_DEFRAG, 1, 0, &cached_state,
8648 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008649 /*
8650 * whoever cleared the private bit is responsible
8651 * for the finish_ordered_io
8652 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008653 if (TestClearPagePrivate2(page)) {
8654 struct btrfs_ordered_inode_tree *tree;
8655 u64 new_len;
8656
8657 tree = &BTRFS_I(inode)->ordered_tree;
8658
8659 spin_lock_irq(&tree->lock);
8660 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8661 new_len = page_start - ordered->file_offset;
8662 if (new_len < ordered->truncated_len)
8663 ordered->truncated_len = new_len;
8664 spin_unlock_irq(&tree->lock);
8665
8666 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8667 page_start,
8668 PAGE_CACHE_SIZE, 1))
8669 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008670 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008671 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008672 if (!inode_evicting) {
8673 cached_state = NULL;
8674 lock_extent_bits(tree, page_start, page_end, 0,
8675 &cached_state);
8676 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008677 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008678
Qu Wenruob9d0b382015-09-29 10:35:16 +08008679 /*
8680 * Qgroup reserved space handler
8681 * Page here will be either
8682 * 1) Already written to disk
8683 * In this case, its reserved space is released from data rsv map
8684 * and will be freed by delayed_ref handler finally.
8685 * So even we call qgroup_free_data(), it won't decrease reserved
8686 * space.
8687 * 2) Not written to disk
8688 * This means the reserved space should be freed here.
8689 */
8690 btrfs_qgroup_free_data(inode, page_start, PAGE_CACHE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008691 if (!inode_evicting) {
8692 clear_extent_bit(tree, page_start, page_end,
8693 EXTENT_LOCKED | EXTENT_DIRTY |
8694 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8695 EXTENT_DEFRAG, 1, 1,
8696 &cached_state, GFP_NOFS);
8697
8698 __btrfs_releasepage(page, GFP_NOFS);
8699 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008700
Chris Mason4a096752008-07-21 10:29:44 -04008701 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008702 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008703 ClearPagePrivate(page);
8704 set_page_private(page, 0);
8705 page_cache_release(page);
8706 }
Chris Mason39279cc2007-06-12 06:35:45 -04008707}
8708
Chris Mason9ebefb182007-06-15 13:50:00 -04008709/*
8710 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8711 * called from a page fault handler when a page is first dirtied. Hence we must
8712 * be careful to check for EOF conditions here. We set the page up correctly
8713 * for a written page which means we get ENOSPC checking when writing into
8714 * holes and correct delalloc and unwritten extent mapping on filesystems that
8715 * support these features.
8716 *
8717 * We are not allowed to take the i_mutex here so we have to play games to
8718 * protect against truncate races as the page could now be beyond EOF. Because
8719 * vmtruncate() writes the inode size before removing pages, once we have the
8720 * page lock we can determine safely if the page is beyond EOF. If it is not
8721 * beyond EOF, then the page is guaranteed safe against truncation until we
8722 * unlock the page.
8723 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008724int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008725{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008726 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008727 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008728 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008729 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8730 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008731 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008732 char *kaddr;
8733 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008734 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008735 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008736 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008737 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008738 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008739
Jan Karab2b5ef52012-06-12 16:20:45 +02008740 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008741 page_start = page_offset(page);
8742 page_end = page_start + PAGE_CACHE_SIZE - 1;
8743
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008744 ret = btrfs_delalloc_reserve_space(inode, page_start,
8745 PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008746 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008747 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008748 reserved = 1;
8749 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008750 if (ret) {
8751 if (ret == -ENOMEM)
8752 ret = VM_FAULT_OOM;
8753 else /* -ENOSPC, -EIO, etc */
8754 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008755 if (reserved)
8756 goto out;
8757 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008758 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008759
Nick Piggin56a76f82009-03-31 15:23:23 -07008760 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008761again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008762 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008763 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008764
Chris Mason9ebefb182007-06-15 13:50:00 -04008765 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008766 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008767 /* page got truncated out from underneath us */
8768 goto out_unlock;
8769 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008770 wait_on_page_writeback(page);
8771
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008772 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008773 set_page_extent_mapped(page);
8774
Chris Masoneb84ae02008-07-17 13:53:27 -04008775 /*
8776 * we can't set the delalloc bits if there are pending ordered
8777 * extents. Drop our locks and wait for them to finish
8778 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008779 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8780 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008781 unlock_extent_cached(io_tree, page_start, page_end,
8782 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008783 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008784 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008785 btrfs_put_ordered_extent(ordered);
8786 goto again;
8787 }
8788
Josef Bacikfbf19082009-10-01 17:10:23 -04008789 /*
8790 * XXX - page_mkwrite gets called every time the page is dirtied, even
8791 * if it was already dirty, so for space accounting reasons we need to
8792 * clear any delalloc bits for the range we are fixing to save. There
8793 * is probably a better way to do this, but for now keep consistent with
8794 * prepare_pages in the normal write path.
8795 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008796 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008797 EXTENT_DIRTY | EXTENT_DELALLOC |
8798 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008799 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008800
Josef Bacik2ac55d42010-02-03 19:33:23 +00008801 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8802 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008803 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008804 unlock_extent_cached(io_tree, page_start, page_end,
8805 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008806 ret = VM_FAULT_SIGBUS;
8807 goto out_unlock;
8808 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008809 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008810
8811 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008812 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008813 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008814 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008815 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008816
Chris Masone6dcd2d2008-07-17 12:53:50 -04008817 if (zero_start != PAGE_CACHE_SIZE) {
8818 kaddr = kmap(page);
8819 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8820 flush_dcache_page(page);
8821 kunmap(page);
8822 }
Chris Mason247e7432008-07-17 12:53:51 -04008823 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008824 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008825 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008826
Chris Mason257c62e2009-10-13 13:21:08 -04008827 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8828 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008829 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008830
Josef Bacik2ac55d42010-02-03 19:33:23 +00008831 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008832
8833out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008834 if (!ret) {
8835 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008836 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008837 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008838 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008839out:
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008840 btrfs_delalloc_release_space(inode, page_start, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008841out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008842 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008843 return ret;
8844}
8845
Josef Bacika41ad392011-01-31 15:30:16 -05008846static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008847{
8848 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008849 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008850 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008851 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008852 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008853 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008854 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008855
Josef Bacik0ef8b722013-10-25 16:13:35 -04008856 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8857 (u64)-1);
8858 if (ret)
8859 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008860
Josef Bacikfcb80c22011-05-03 10:40:22 -04008861 /*
8862 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8863 * 3 things going on here
8864 *
8865 * 1) We need to reserve space for our orphan item and the space to
8866 * delete our orphan item. Lord knows we don't want to have a dangling
8867 * orphan item because we didn't reserve space to remove it.
8868 *
8869 * 2) We need to reserve space to update our inode.
8870 *
8871 * 3) We need to have something to cache all the space that is going to
8872 * be free'd up by the truncate operation, but also have some slack
8873 * space reserved in case it uses space during the truncate (thank you
8874 * very much snapshotting).
8875 *
8876 * And we need these to all be seperate. The fact is we can use alot of
8877 * space doing the truncate, and we have no earthly idea how much space
8878 * we will use, so we need the truncate reservation to be seperate so it
8879 * doesn't end up using space reserved for updating the inode or
8880 * removing the orphan item. We also need to be able to stop the
8881 * transaction and start a new one, which means we need to be able to
8882 * update the inode several times, and we have no idea of knowing how
8883 * many times that will be, so we can't just reserve 1 item for the
8884 * entirety of the opration, so that has to be done seperately as well.
8885 * Then there is the orphan item, which does indeed need to be held on
8886 * to for the whole operation, and we need nobody to touch this reserved
8887 * space except the orphan code.
8888 *
8889 * So that leaves us with
8890 *
8891 * 1) root->orphan_block_rsv - for the orphan deletion.
8892 * 2) rsv - for the truncate reservation, which we will steal from the
8893 * transaction reservation.
8894 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8895 * updating the inode.
8896 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008897 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008898 if (!rsv)
8899 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008900 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008901 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008902
Josef Bacik907cbce2011-08-08 13:46:15 -04008903 /*
Josef Bacik07127182011-08-19 10:29:59 -04008904 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008905 * 1 for updating the inode.
8906 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008907 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008908 if (IS_ERR(trans)) {
8909 err = PTR_ERR(trans);
8910 goto out;
8911 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008912
Josef Bacik907cbce2011-08-08 13:46:15 -04008913 /* Migrate the slack space for the truncate to our reserve */
8914 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8915 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008916 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008917
Chris Mason5a3f23d2009-03-31 13:27:11 -04008918 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008919 * So if we truncate and then write and fsync we normally would just
8920 * write the extents that changed, which is a problem if we need to
8921 * first truncate that entire inode. So set this flag so we write out
8922 * all of the extents in the inode to the sync log so we're completely
8923 * safe.
8924 */
8925 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008926 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008927
Yan, Zheng80825102009-11-12 09:35:36 +00008928 while (1) {
8929 ret = btrfs_truncate_inode_items(trans, root, inode,
8930 inode->i_size,
8931 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08008932 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05008933 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008934 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008935 }
Chris Mason39279cc2007-06-12 06:35:45 -04008936
Josef Bacikfcb80c22011-05-03 10:40:22 -04008937 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008938 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008939 if (ret) {
8940 err = ret;
8941 break;
8942 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008943
Yan, Zheng80825102009-11-12 09:35:36 +00008944 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008945 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008946
8947 trans = btrfs_start_transaction(root, 2);
8948 if (IS_ERR(trans)) {
8949 ret = err = PTR_ERR(trans);
8950 trans = NULL;
8951 break;
8952 }
8953
8954 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8955 rsv, min_size);
8956 BUG_ON(ret); /* shouldn't happen */
8957 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008958 }
8959
8960 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008961 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008962 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008963 if (ret)
8964 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008965 }
8966
Chris Mason917c16b2011-11-08 14:49:59 -05008967 if (trans) {
8968 trans->block_rsv = &root->fs_info->trans_block_rsv;
8969 ret = btrfs_update_inode(trans, root, inode);
8970 if (ret && !err)
8971 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008972
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008973 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008974 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008975 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008976
8977out:
8978 btrfs_free_block_rsv(root, rsv);
8979
Josef Bacik3893e332011-01-31 16:03:11 -05008980 if (ret && !err)
8981 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008982
Josef Bacik3893e332011-01-31 16:03:11 -05008983 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008984}
8985
Sven Wegener3b963622008-06-09 21:57:42 -04008986/*
Chris Masond352ac62008-09-29 15:18:18 -04008987 * create a new subvolume directory/inode (helper for the ioctl).
8988 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008989int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008990 struct btrfs_root *new_root,
8991 struct btrfs_root *parent_root,
8992 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008993{
Chris Mason39279cc2007-06-12 06:35:45 -04008994 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008995 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008996 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008997
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008998 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8999 new_dirid, new_dirid,
9000 S_IFDIR | (~current_umask() & S_IRWXUGO),
9001 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009002 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009003 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009004 inode->i_op = &btrfs_dir_inode_operations;
9005 inode->i_fop = &btrfs_dir_file_operations;
9006
Miklos Szeredibfe86842011-10-28 14:13:29 +02009007 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009008 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009009 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009010
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009011 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9012 if (err)
9013 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009014 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009015 new_root->root_key.objectid, err);
9016
Yan, Zheng76dda932009-09-21 16:00:26 -04009017 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009018
Yan, Zheng76dda932009-09-21 16:00:26 -04009019 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009020 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009021}
9022
Chris Mason39279cc2007-06-12 06:35:45 -04009023struct inode *btrfs_alloc_inode(struct super_block *sb)
9024{
9025 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009026 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009027
9028 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9029 if (!ei)
9030 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009031
9032 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009033 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009034 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009035 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009036 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009037 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009038 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009039 ei->disk_i_size = 0;
9040 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009041 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009042 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009043 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009044 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009045 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009046
Josef Bacik9e0baf62011-07-15 15:16:44 +00009047 spin_lock_init(&ei->lock);
9048 ei->outstanding_extents = 0;
9049 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009050
Josef Bacik72ac3c02012-05-23 14:13:11 -04009051 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009052 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009053
Miao Xie16cdcec2011-04-22 18:12:22 +08009054 ei->delayed_node = NULL;
9055
chandan r9cc97d62012-07-04 12:48:07 +05309056 ei->i_otime.tv_sec = 0;
9057 ei->i_otime.tv_nsec = 0;
9058
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009059 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009060 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009061 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9062 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009063 ei->io_tree.track_uptodate = 1;
9064 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009065 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009066 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009067 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009068 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009069 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009070 RB_CLEAR_NODE(&ei->rb_node);
9071
9072 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009073}
9074
Josef Bacikaaedb552013-10-11 14:44:09 -04009075#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9076void btrfs_test_destroy_inode(struct inode *inode)
9077{
9078 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9079 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9080}
9081#endif
9082
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009083static void btrfs_i_callback(struct rcu_head *head)
9084{
9085 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009086 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9087}
9088
Chris Mason39279cc2007-06-12 06:35:45 -04009089void btrfs_destroy_inode(struct inode *inode)
9090{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009091 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009092 struct btrfs_root *root = BTRFS_I(inode)->root;
9093
Al Virob3d9b7a2012-06-09 13:51:19 -04009094 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009095 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009096 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9097 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009098 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9099 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009100 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009101
Chris Mason5a3f23d2009-03-31 13:27:11 -04009102 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009103 * This can happen where we create an inode, but somebody else also
9104 * created the same inode and we need to destroy the one we already
9105 * created.
9106 */
9107 if (!root)
9108 goto free;
9109
Josef Bacik8a35d952012-05-23 14:26:42 -04009110 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9111 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009112 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009113 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009114 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009115 }
Josef Bacik7b128762008-07-24 12:17:14 -04009116
Chris Masond3977122009-01-05 21:25:51 -05009117 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009118 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9119 if (!ordered)
9120 break;
9121 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009122 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009123 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009124 btrfs_remove_ordered_extent(inode, ordered);
9125 btrfs_put_ordered_extent(ordered);
9126 btrfs_put_ordered_extent(ordered);
9127 }
9128 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009129 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009130 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009131 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009132free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009133 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009134}
9135
Al Viro45321ac2010-06-07 13:43:19 -04009136int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009137{
9138 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009139
Naohiro Aota6379ef92013-06-06 09:56:34 +00009140 if (root == NULL)
9141 return 1;
9142
Liu Bofa6ac872013-02-20 14:10:23 +00009143 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009144 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009145 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009146 else
Al Viro45321ac2010-06-07 13:43:19 -04009147 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009148}
9149
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009150static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009151{
9152 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9153
9154 inode_init_once(&ei->vfs_inode);
9155}
9156
9157void btrfs_destroy_cachep(void)
9158{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009159 /*
9160 * Make sure all delayed rcu free inodes are flushed before we
9161 * destroy cache.
9162 */
9163 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04009164 if (btrfs_inode_cachep)
9165 kmem_cache_destroy(btrfs_inode_cachep);
9166 if (btrfs_trans_handle_cachep)
9167 kmem_cache_destroy(btrfs_trans_handle_cachep);
9168 if (btrfs_transaction_cachep)
9169 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009170 if (btrfs_path_cachep)
9171 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05009172 if (btrfs_free_space_cachep)
9173 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009174 if (btrfs_delalloc_work_cachep)
9175 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009176}
9177
9178int btrfs_init_cachep(void)
9179{
David Sterba837e1972012-09-07 03:00:48 -06009180 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009181 sizeof(struct btrfs_inode), 0,
9182 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009183 if (!btrfs_inode_cachep)
9184 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009185
David Sterba837e1972012-09-07 03:00:48 -06009186 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009187 sizeof(struct btrfs_trans_handle), 0,
9188 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009189 if (!btrfs_trans_handle_cachep)
9190 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009191
David Sterba837e1972012-09-07 03:00:48 -06009192 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009193 sizeof(struct btrfs_transaction), 0,
9194 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009195 if (!btrfs_transaction_cachep)
9196 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009197
David Sterba837e1972012-09-07 03:00:48 -06009198 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009199 sizeof(struct btrfs_path), 0,
9200 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009201 if (!btrfs_path_cachep)
9202 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009203
David Sterba837e1972012-09-07 03:00:48 -06009204 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009205 sizeof(struct btrfs_free_space), 0,
9206 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
9207 if (!btrfs_free_space_cachep)
9208 goto fail;
9209
Miao Xie8ccf6f192012-10-25 09:28:04 +00009210 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
9211 sizeof(struct btrfs_delalloc_work), 0,
9212 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
9213 NULL);
9214 if (!btrfs_delalloc_work_cachep)
9215 goto fail;
9216
Chris Mason39279cc2007-06-12 06:35:45 -04009217 return 0;
9218fail:
9219 btrfs_destroy_cachep();
9220 return -ENOMEM;
9221}
9222
9223static int btrfs_getattr(struct vfsmount *mnt,
9224 struct dentry *dentry, struct kstat *stat)
9225{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009226 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009227 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009228 u32 blocksize = inode->i_sb->s_blocksize;
9229
Chris Mason39279cc2007-06-12 06:35:45 -04009230 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009231 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05009232 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009233
9234 spin_lock(&BTRFS_I(inode)->lock);
9235 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9236 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009237 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009238 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009239 return 0;
9240}
9241
Chris Masond3977122009-01-05 21:25:51 -05009242static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9243 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009244{
9245 struct btrfs_trans_handle *trans;
9246 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009247 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009248 struct inode *new_inode = d_inode(new_dentry);
9249 struct inode *old_inode = d_inode(old_dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04009250 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009251 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009252 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009253 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009254 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009255
Li Zefan33345d012011-04-20 10:31:50 +08009256 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009257 return -EPERM;
9258
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009259 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009260 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009261 return -EXDEV;
9262
Li Zefan33345d012011-04-20 10:31:50 +08009263 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9264 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009265 return -ENOTEMPTY;
9266
Chris Mason39279cc2007-06-12 06:35:45 -04009267 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009268 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009269 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009270
9271
9272 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009273 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009274 new_dentry->d_name.name,
9275 new_dentry->d_name.len);
9276
9277 if (ret) {
9278 if (ret == -EEXIST) {
9279 /* we shouldn't get
9280 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309281 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009282 return ret;
9283 }
9284 } else {
9285 /* maybe -EOVERFLOW */
9286 return ret;
9287 }
9288 }
9289 ret = 0;
9290
Chris Mason5a3f23d2009-03-31 13:27:11 -04009291 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009292 * we're using rename to replace one file with another. Start IO on it
9293 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009294 */
Chris Mason8d875f92014-08-12 10:47:42 -07009295 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009296 filemap_flush(old_inode->i_mapping);
9297
Yan, Zheng76dda932009-09-21 16:00:26 -04009298 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009299 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009300 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009301 /*
9302 * We want to reserve the absolute worst case amount of items. So if
9303 * both inodes are subvols and we need to unlink them then that would
9304 * require 4 item modifications, but if they are both normal inodes it
9305 * would require 5 item modifications, so we'll assume their normal
9306 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9307 * should cover the worst case number of items we'll modify.
9308 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04009309 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009310 if (IS_ERR(trans)) {
9311 ret = PTR_ERR(trans);
9312 goto out_notrans;
9313 }
Chris Mason5f39d392007-10-15 16:14:19 -04009314
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009315 if (dest != root)
9316 btrfs_record_root_in_trans(trans, dest);
9317
Yan, Zhenga5719522009-09-24 09:17:31 -04009318 ret = btrfs_set_inode_index(new_dir, &index);
9319 if (ret)
9320 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009321
Miao Xie67de1172013-12-26 13:07:06 +08009322 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009323 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009324 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009325 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009326 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009327 ret = btrfs_insert_inode_ref(trans, dest,
9328 new_dentry->d_name.name,
9329 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009330 old_ino,
9331 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009332 if (ret)
9333 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009334 /*
9335 * this is an ugly little race, but the rename is required
9336 * to make sure that if we crash, the inode is either at the
9337 * old name or the new one. pinning the log transaction lets
9338 * us make sure we don't allow a log commit to come in after
9339 * we unlink the name but before we add the new name back in.
9340 */
9341 btrfs_pin_log_trans(root);
9342 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009343
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009344 inode_inc_iversion(old_dir);
9345 inode_inc_iversion(new_dir);
9346 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009347 old_dir->i_ctime = old_dir->i_mtime = ctime;
9348 new_dir->i_ctime = new_dir->i_mtime = ctime;
9349 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04009350
Chris Mason12fcfd22009-03-24 10:24:20 -04009351 if (old_dentry->d_parent != new_dentry->d_parent)
9352 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9353
Li Zefan33345d012011-04-20 10:31:50 +08009354 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009355 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9356 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9357 old_dentry->d_name.name,
9358 old_dentry->d_name.len);
9359 } else {
Al Viro92986792011-03-04 17:14:37 +00009360 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009361 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009362 old_dentry->d_name.name,
9363 old_dentry->d_name.len);
9364 if (!ret)
9365 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009366 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009367 if (ret) {
9368 btrfs_abort_transaction(trans, root, ret);
9369 goto out_fail;
9370 }
Chris Mason39279cc2007-06-12 06:35:45 -04009371
9372 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009373 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009374 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08009375 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009376 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9377 root_objectid = BTRFS_I(new_inode)->location.objectid;
9378 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9379 root_objectid,
9380 new_dentry->d_name.name,
9381 new_dentry->d_name.len);
9382 BUG_ON(new_inode->i_nlink == 0);
9383 } else {
9384 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009385 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009386 new_dentry->d_name.name,
9387 new_dentry->d_name.len);
9388 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009389 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009390 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009391 if (ret) {
9392 btrfs_abort_transaction(trans, root, ret);
9393 goto out_fail;
9394 }
Chris Mason39279cc2007-06-12 06:35:45 -04009395 }
Josef Bacikaec74772008-07-24 12:12:38 -04009396
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009397 ret = btrfs_add_link(trans, new_dir, old_inode,
9398 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009399 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009400 if (ret) {
9401 btrfs_abort_transaction(trans, root, ret);
9402 goto out_fail;
9403 }
Chris Mason39279cc2007-06-12 06:35:45 -04009404
Miao Xie67de1172013-12-26 13:07:06 +08009405 if (old_inode->i_nlink == 1)
9406 BTRFS_I(old_inode)->dir_index = index;
9407
Li Zefan33345d012011-04-20 10:31:50 +08009408 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04009409 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00009410 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009411 btrfs_end_log_trans(root);
9412 }
Chris Mason39279cc2007-06-12 06:35:45 -04009413out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009414 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009415out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009416 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009417 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009418
Chris Mason39279cc2007-06-12 06:35:45 -04009419 return ret;
9420}
9421
Miklos Szeredi80ace852014-07-23 15:15:32 +02009422static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9423 struct inode *new_dir, struct dentry *new_dentry,
9424 unsigned int flags)
9425{
9426 if (flags & ~RENAME_NOREPLACE)
9427 return -EINVAL;
9428
9429 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9430}
9431
Miao Xie8ccf6f192012-10-25 09:28:04 +00009432static void btrfs_run_delalloc_work(struct btrfs_work *work)
9433{
9434 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009435 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009436
9437 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9438 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009439 inode = delalloc_work->inode;
9440 if (delalloc_work->wait) {
9441 btrfs_wait_ordered_range(inode, 0, (u64)-1);
9442 } else {
9443 filemap_flush(inode->i_mapping);
9444 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9445 &BTRFS_I(inode)->runtime_flags))
9446 filemap_flush(inode->i_mapping);
9447 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00009448
9449 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009450 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009451 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009452 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009453 complete(&delalloc_work->completion);
9454}
9455
9456struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9457 int wait, int delay_iput)
9458{
9459 struct btrfs_delalloc_work *work;
9460
9461 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
9462 if (!work)
9463 return NULL;
9464
9465 init_completion(&work->completion);
9466 INIT_LIST_HEAD(&work->list);
9467 work->inode = inode;
9468 work->wait = wait;
9469 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009470 WARN_ON_ONCE(!inode);
9471 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9472 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009473
9474 return work;
9475}
9476
9477void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9478{
9479 wait_for_completion(&work->completion);
9480 kmem_cache_free(btrfs_delalloc_work_cachep, work);
9481}
9482
Chris Masond352ac62008-09-29 15:18:18 -04009483/*
9484 * some fairly slow code that needs optimization. This walks the list
9485 * of all the inodes with pending delalloc and forces them to disk.
9486 */
Miao Xie6c255e62014-03-06 13:55:01 +08009487static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9488 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009489{
Chris Masonea8c2812008-08-04 23:17:27 -04009490 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009491 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009492 struct btrfs_delalloc_work *work, *next;
9493 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009494 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009495 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009496
Miao Xie8ccf6f192012-10-25 09:28:04 +00009497 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009498 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009499
Miao Xie573bfb72014-03-06 13:55:03 +08009500 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009501 spin_lock(&root->delalloc_lock);
9502 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009503 while (!list_empty(&splice)) {
9504 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009505 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009506
Miao Xieeb73c1b2013-05-15 07:48:22 +00009507 list_move_tail(&binode->delalloc_inodes,
9508 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009509 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009510 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009511 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009512 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009513 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009514 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009515
9516 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009517 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009518 if (delay_iput)
9519 btrfs_add_delayed_iput(inode);
9520 else
9521 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009522 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009523 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009524 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009525 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009526 btrfs_queue_work(root->fs_info->flush_workers,
9527 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009528 ret++;
9529 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009530 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009531 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009532 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009533 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009534 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009535
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009536out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009537 list_for_each_entry_safe(work, next, &works, list) {
9538 list_del_init(&work->list);
9539 btrfs_wait_and_free_delalloc_work(work);
9540 }
9541
Miao Xieeb73c1b2013-05-15 07:48:22 +00009542 if (!list_empty_careful(&splice)) {
9543 spin_lock(&root->delalloc_lock);
9544 list_splice_tail(&splice, &root->delalloc_inodes);
9545 spin_unlock(&root->delalloc_lock);
9546 }
Miao Xie573bfb72014-03-06 13:55:03 +08009547 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009548 return ret;
9549}
9550
9551int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9552{
9553 int ret;
9554
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009555 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009556 return -EROFS;
9557
Miao Xie6c255e62014-03-06 13:55:01 +08009558 ret = __start_delalloc_inodes(root, delay_iput, -1);
9559 if (ret > 0)
9560 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009561 /*
9562 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009563 * we have to make sure the IO is actually started and that
9564 * ordered extents get created before we return
9565 */
9566 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009567 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009568 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009569 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009570 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9571 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009572 }
9573 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009574 return ret;
9575}
9576
Miao Xie6c255e62014-03-06 13:55:01 +08009577int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9578 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009579{
9580 struct btrfs_root *root;
9581 struct list_head splice;
9582 int ret;
9583
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009584 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009585 return -EROFS;
9586
9587 INIT_LIST_HEAD(&splice);
9588
Miao Xie573bfb72014-03-06 13:55:03 +08009589 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009590 spin_lock(&fs_info->delalloc_root_lock);
9591 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009592 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009593 root = list_first_entry(&splice, struct btrfs_root,
9594 delalloc_root);
9595 root = btrfs_grab_fs_root(root);
9596 BUG_ON(!root);
9597 list_move_tail(&root->delalloc_root,
9598 &fs_info->delalloc_roots);
9599 spin_unlock(&fs_info->delalloc_root_lock);
9600
Miao Xie6c255e62014-03-06 13:55:01 +08009601 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009602 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009603 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009604 goto out;
9605
Miao Xie6c255e62014-03-06 13:55:01 +08009606 if (nr != -1) {
9607 nr -= ret;
9608 WARN_ON(nr < 0);
9609 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009610 spin_lock(&fs_info->delalloc_root_lock);
9611 }
9612 spin_unlock(&fs_info->delalloc_root_lock);
9613
Miao Xie6c255e62014-03-06 13:55:01 +08009614 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009615 atomic_inc(&fs_info->async_submit_draining);
9616 while (atomic_read(&fs_info->nr_async_submits) ||
9617 atomic_read(&fs_info->async_delalloc_pages)) {
9618 wait_event(fs_info->async_submit_wait,
9619 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9620 atomic_read(&fs_info->async_delalloc_pages) == 0));
9621 }
9622 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009623out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009624 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009625 spin_lock(&fs_info->delalloc_root_lock);
9626 list_splice_tail(&splice, &fs_info->delalloc_roots);
9627 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009628 }
Miao Xie573bfb72014-03-06 13:55:03 +08009629 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009630 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009631}
9632
Chris Mason39279cc2007-06-12 06:35:45 -04009633static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9634 const char *symname)
9635{
9636 struct btrfs_trans_handle *trans;
9637 struct btrfs_root *root = BTRFS_I(dir)->root;
9638 struct btrfs_path *path;
9639 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009640 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009641 int err;
9642 int drop_inode = 0;
9643 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309644 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009645 int name_len;
9646 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009647 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009648 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009649 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009650
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009651 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009652 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9653 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009654
Josef Bacik9ed74f22009-09-11 16:12:44 -04009655 /*
9656 * 2 items for inode item and ref
9657 * 2 items for dir items
9658 * 1 item for xattr if selinux is on
9659 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009660 trans = btrfs_start_transaction(root, 5);
9661 if (IS_ERR(trans))
9662 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009663
Li Zefan581bb052011-04-20 10:06:11 +08009664 err = btrfs_find_free_ino(root, &objectid);
9665 if (err)
9666 goto out_unlock;
9667
Josef Bacikaec74772008-07-24 12:12:38 -04009668 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009669 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04009670 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009671 if (IS_ERR(inode)) {
9672 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009673 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009674 }
Chris Mason39279cc2007-06-12 06:35:45 -04009675
Casey Schauflerad19db72011-12-15 10:09:07 -05009676 /*
9677 * If the active LSM wants to access the inode during
9678 * d_instantiate it needs these. Smack checks to see
9679 * if the filesystem supports xattrs by looking at the
9680 * ops vector.
9681 */
9682 inode->i_fop = &btrfs_file_operations;
9683 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009684 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009685 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9686
9687 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9688 if (err)
9689 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009690
Josef Bacika1b075d2010-11-19 20:36:11 +00009691 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009692 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009693 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009694
9695 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009696 if (!path) {
9697 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009698 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009699 }
Li Zefan33345d012011-04-20 10:31:50 +08009700 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009701 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009702 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009703 datasize = btrfs_file_extent_calc_inline_size(name_len);
9704 err = btrfs_insert_empty_item(trans, root, path, &key,
9705 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009706 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009707 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009708 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009709 }
Chris Mason5f39d392007-10-15 16:14:19 -04009710 leaf = path->nodes[0];
9711 ei = btrfs_item_ptr(leaf, path->slots[0],
9712 struct btrfs_file_extent_item);
9713 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9714 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009715 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009716 btrfs_set_file_extent_encryption(leaf, ei, 0);
9717 btrfs_set_file_extent_compression(leaf, ei, 0);
9718 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9719 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9720
Chris Mason39279cc2007-06-12 06:35:45 -04009721 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009722 write_extent_buffer(leaf, symname, ptr, name_len);
9723 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009724 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009725
Chris Mason39279cc2007-06-12 06:35:45 -04009726 inode->i_op = &btrfs_symlink_inode_operations;
9727 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -04009728 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009729 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009730 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009731 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009732 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009733 goto out_unlock_inode;
9734 }
9735
9736 unlock_new_inode(inode);
9737 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009738
9739out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009740 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009741 if (drop_inode) {
9742 inode_dec_link_count(inode);
9743 iput(inode);
9744 }
Liu Bob53d3f52012-11-14 14:34:34 +00009745 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009746 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009747
9748out_unlock_inode:
9749 drop_inode = 1;
9750 unlock_new_inode(inode);
9751 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009752}
Chris Mason16432982008-04-10 10:23:21 -04009753
Josef Bacik0af3d002010-06-21 14:48:16 -04009754static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9755 u64 start, u64 num_bytes, u64 min_size,
9756 loff_t actual_len, u64 *alloc_hint,
9757 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009758{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009759 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9760 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009761 struct btrfs_root *root = BTRFS_I(inode)->root;
9762 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009763 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009764 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009765 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009766 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009767 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009768 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009769
Josef Bacik0af3d002010-06-21 14:48:16 -04009770 if (trans)
9771 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009772 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009773 if (own_trans) {
9774 trans = btrfs_start_transaction(root, 3);
9775 if (IS_ERR(trans)) {
9776 ret = PTR_ERR(trans);
9777 break;
9778 }
Yan Zhengd899e052008-10-30 14:25:28 -04009779 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009780
Chris Mason154ea282013-03-05 11:11:26 -05009781 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9782 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009783 /*
9784 * If we are severely fragmented we could end up with really
9785 * small allocations, so if the allocator is returning small
9786 * chunks lets make its job easier by only searching for those
9787 * sized chunks.
9788 */
9789 cur_bytes = min(cur_bytes, last_alloc);
Josef Bacik00361582013-08-14 14:02:47 -04009790 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009791 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009792 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009793 if (own_trans)
9794 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009795 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009796 }
9797
Josef Bacik0b670dc2015-09-23 17:11:16 -04009798 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04009799 ret = insert_reserved_file_extent(trans, inode,
9800 cur_offset, ins.objectid,
9801 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009802 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009803 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009804 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009805 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009806 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009807 btrfs_abort_transaction(trans, root, ret);
9808 if (own_trans)
9809 btrfs_end_transaction(trans, root);
9810 break;
9811 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009812
Chris Masona1ed8352009-09-11 12:27:37 -04009813 btrfs_drop_extent_cache(inode, cur_offset,
9814 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009815
Josef Bacik5dc562c2012-08-17 13:14:17 -04009816 em = alloc_extent_map();
9817 if (!em) {
9818 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9819 &BTRFS_I(inode)->runtime_flags);
9820 goto next;
9821 }
9822
9823 em->start = cur_offset;
9824 em->orig_start = cur_offset;
9825 em->len = ins.offset;
9826 em->block_start = ins.objectid;
9827 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009828 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009829 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009830 em->bdev = root->fs_info->fs_devices->latest_bdev;
9831 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9832 em->generation = trans->transid;
9833
9834 while (1) {
9835 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009836 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009837 write_unlock(&em_tree->lock);
9838 if (ret != -EEXIST)
9839 break;
9840 btrfs_drop_extent_cache(inode, cur_offset,
9841 cur_offset + ins.offset - 1,
9842 0);
9843 }
9844 free_extent_map(em);
9845next:
Yan Zhengd899e052008-10-30 14:25:28 -04009846 num_bytes -= ins.offset;
9847 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009848 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009849
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009850 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009851 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009852 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009853 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009854 (actual_len > inode->i_size) &&
9855 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009856 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009857 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009858 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009859 i_size = cur_offset;
9860 i_size_write(inode, i_size);
9861 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009862 }
9863
Yan Zhengd899e052008-10-30 14:25:28 -04009864 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009865
9866 if (ret) {
9867 btrfs_abort_transaction(trans, root, ret);
9868 if (own_trans)
9869 btrfs_end_transaction(trans, root);
9870 break;
9871 }
Yan Zhengd899e052008-10-30 14:25:28 -04009872
Josef Bacik0af3d002010-06-21 14:48:16 -04009873 if (own_trans)
9874 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009875 }
Yan Zhengd899e052008-10-30 14:25:28 -04009876 return ret;
9877}
9878
Josef Bacik0af3d002010-06-21 14:48:16 -04009879int btrfs_prealloc_file_range(struct inode *inode, int mode,
9880 u64 start, u64 num_bytes, u64 min_size,
9881 loff_t actual_len, u64 *alloc_hint)
9882{
9883 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9884 min_size, actual_len, alloc_hint,
9885 NULL);
9886}
9887
9888int btrfs_prealloc_file_range_trans(struct inode *inode,
9889 struct btrfs_trans_handle *trans, int mode,
9890 u64 start, u64 num_bytes, u64 min_size,
9891 loff_t actual_len, u64 *alloc_hint)
9892{
9893 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9894 min_size, actual_len, alloc_hint, trans);
9895}
9896
Chris Masone6dcd2d2008-07-17 12:53:50 -04009897static int btrfs_set_page_dirty(struct page *page)
9898{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009899 return __set_page_dirty_nobuffers(page);
9900}
9901
Al Viro10556cb22011-06-20 19:28:19 -04009902static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009903{
Li Zefanb83cc962010-12-20 16:04:08 +08009904 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009905 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009906
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009907 if (mask & MAY_WRITE &&
9908 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9909 if (btrfs_root_readonly(root))
9910 return -EROFS;
9911 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9912 return -EACCES;
9913 }
Al Viro2830ba72011-06-20 19:16:29 -04009914 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009915}
Chris Mason39279cc2007-06-12 06:35:45 -04009916
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009917static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9918{
9919 struct btrfs_trans_handle *trans;
9920 struct btrfs_root *root = BTRFS_I(dir)->root;
9921 struct inode *inode = NULL;
9922 u64 objectid;
9923 u64 index;
9924 int ret = 0;
9925
9926 /*
9927 * 5 units required for adding orphan entry
9928 */
9929 trans = btrfs_start_transaction(root, 5);
9930 if (IS_ERR(trans))
9931 return PTR_ERR(trans);
9932
9933 ret = btrfs_find_free_ino(root, &objectid);
9934 if (ret)
9935 goto out;
9936
9937 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9938 btrfs_ino(dir), objectid, mode, &index);
9939 if (IS_ERR(inode)) {
9940 ret = PTR_ERR(inode);
9941 inode = NULL;
9942 goto out;
9943 }
9944
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009945 inode->i_fop = &btrfs_file_operations;
9946 inode->i_op = &btrfs_file_inode_operations;
9947
9948 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009949 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9950
Chris Masonb0d5d102014-09-08 13:08:51 -07009951 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9952 if (ret)
9953 goto out_inode;
9954
9955 ret = btrfs_update_inode(trans, root, inode);
9956 if (ret)
9957 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009958 ret = btrfs_orphan_add(trans, inode);
9959 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009960 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009961
Filipe Manana5762b5c2014-08-01 00:10:32 +01009962 /*
9963 * We set number of links to 0 in btrfs_new_inode(), and here we set
9964 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9965 * through:
9966 *
9967 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9968 */
9969 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009970 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009971 d_tmpfile(dentry, inode);
9972 mark_inode_dirty(inode);
9973
9974out:
9975 btrfs_end_transaction(trans, root);
9976 if (ret)
9977 iput(inode);
9978 btrfs_balance_delayed_items(root);
9979 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009980 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009981
9982out_inode:
9983 unlock_new_inode(inode);
9984 goto out;
9985
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009986}
9987
Filipe Mananab38ef712014-11-13 17:01:45 +00009988/* Inspired by filemap_check_errors() */
9989int btrfs_inode_check_errors(struct inode *inode)
9990{
9991 int ret = 0;
9992
9993 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
9994 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
9995 ret = -ENOSPC;
9996 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
9997 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
9998 ret = -EIO;
9999
10000 return ret;
10001}
10002
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010003static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010004 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010005 .lookup = btrfs_lookup,
10006 .create = btrfs_create,
10007 .unlink = btrfs_unlink,
10008 .link = btrfs_link,
10009 .mkdir = btrfs_mkdir,
10010 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +020010011 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010012 .symlink = btrfs_symlink,
10013 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010014 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010015 .setxattr = btrfs_setxattr,
10016 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010017 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010018 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -050010019 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010020 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010021 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010022 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010023 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010024};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010025static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010026 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010027 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010028 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010029 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010030 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010031};
Yan, Zheng76dda932009-09-21 16:00:26 -040010032
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010033static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010034 .llseek = generic_file_llseek,
10035 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -040010036 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010037 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010038#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010039 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010040#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010041 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010042 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010043};
10044
Chris Masond1310b22008-01-24 16:13:08 -050010045static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010046 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010047 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010048 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010049 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010050 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010051 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010052 .set_bit_hook = btrfs_set_bit_hook,
10053 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010054 .merge_extent_hook = btrfs_merge_extent_hook,
10055 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010056};
10057
Chris Mason35054392009-01-21 13:11:13 -050010058/*
10059 * btrfs doesn't support the bmap operation because swapfiles
10060 * use bmap to make a mapping of extents in the file. They assume
10061 * these extents won't change over the life of the file and they
10062 * use the bmap result to do IO directly to the drive.
10063 *
10064 * the btrfs bmap call would return logical addresses that aren't
10065 * suitable for IO and they also will change frequently as COW
10066 * operations happen. So, swapfile + btrfs == corruption.
10067 *
10068 * For now we're avoiding this by dropping bmap.
10069 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010070static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010071 .readpage = btrfs_readpage,
10072 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010073 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010074 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010075 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010076 .invalidatepage = btrfs_invalidatepage,
10077 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010078 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010079 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010080};
10081
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010082static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010083 .readpage = btrfs_readpage,
10084 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010085 .invalidatepage = btrfs_invalidatepage,
10086 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010087};
10088
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010089static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010090 .getattr = btrfs_getattr,
10091 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010092 .setxattr = btrfs_setxattr,
10093 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010094 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010095 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -050010096 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010097 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010098 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010099 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010100 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010101};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010102static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010103 .getattr = btrfs_getattr,
10104 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010105 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010106 .setxattr = btrfs_setxattr,
10107 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -040010108 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010109 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010110 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010111 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010112 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010113};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010114static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010115 .readlink = generic_readlink,
10116 .follow_link = page_follow_link_light,
10117 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +000010118 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010119 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010120 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010121 .setxattr = btrfs_setxattr,
10122 .getxattr = btrfs_getxattr,
10123 .listxattr = btrfs_listxattr,
10124 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010125 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010126};
Yan, Zheng76dda932009-09-21 16:00:26 -040010127
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010128const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010129 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010130 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010131};