blob: a3942a70f97dd3524b3a5e3fe7810516fe5d09c2 [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:
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000313 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400314 btrfs_end_transaction(trans, root);
315 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400316}
317
Chris Mason771ed682008-11-06 22:02:51 -0500318struct async_extent {
319 u64 start;
320 u64 ram_size;
321 u64 compressed_size;
322 struct page **pages;
323 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800324 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500325 struct list_head list;
326};
327
328struct async_cow {
329 struct inode *inode;
330 struct btrfs_root *root;
331 struct page *locked_page;
332 u64 start;
333 u64 end;
334 struct list_head extents;
335 struct btrfs_work work;
336};
337
338static noinline int add_async_extent(struct async_cow *cow,
339 u64 start, u64 ram_size,
340 u64 compressed_size,
341 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800342 unsigned long nr_pages,
343 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500344{
345 struct async_extent *async_extent;
346
347 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500349 async_extent->start = start;
350 async_extent->ram_size = ram_size;
351 async_extent->compressed_size = compressed_size;
352 async_extent->pages = pages;
353 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800354 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500355 list_add_tail(&async_extent->list, &cow->extents);
356 return 0;
357}
358
Wang Shilongf79707b2014-07-17 11:44:09 +0800359static inline int inode_need_compress(struct inode *inode)
360{
361 struct btrfs_root *root = BTRFS_I(inode)->root;
362
363 /* force compress */
364 if (btrfs_test_opt(root, FORCE_COMPRESS))
365 return 1;
366 /* bad compression ratios */
367 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
368 return 0;
369 if (btrfs_test_opt(root, COMPRESS) ||
370 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
371 BTRFS_I(inode)->force_compress)
372 return 1;
373 return 0;
374}
375
Chris Masonc8b97812008-10-29 14:49:59 -0400376/*
Chris Mason771ed682008-11-06 22:02:51 -0500377 * we create compressed extents in two phases. The first
378 * phase compresses a range of pages that have already been
379 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400380 *
Chris Mason771ed682008-11-06 22:02:51 -0500381 * This is done inside an ordered work queue, and the compression
382 * is spread across many cpus. The actual IO submission is step
383 * two, and the ordered work queue takes care of making sure that
384 * happens in the same order things were put onto the queue by
385 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400386 *
Chris Mason771ed682008-11-06 22:02:51 -0500387 * If this code finds it can't get good compression, it puts an
388 * entry onto the work queue to write the uncompressed bytes. This
389 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300390 * are written in the same order that the flusher thread sent them
391 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400392 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100393static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500394 struct page *locked_page,
395 u64 start, u64 end,
396 struct async_cow *async_cow,
397 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400398{
399 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400400 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400401 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400402 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500403 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400404 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400405 struct page **pages = NULL;
406 unsigned long nr_pages;
407 unsigned long nr_pages_ret = 0;
408 unsigned long total_compressed = 0;
409 unsigned long total_in = 0;
410 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500411 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400412 int i;
413 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800414 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400415 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400416
Liu Bo4cb13e52012-03-29 09:57:45 -0400417 /* if this is a small write inside eof, kick off a defrag */
418 if ((end - start + 1) < 16 * 1024 &&
419 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400420 btrfs_add_inode_defrag(NULL, inode);
421
Chris Mason42dc7ba2008-12-15 11:44:56 -0500422 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400423again:
424 will_compress = 0;
425 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
426 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
427
Chris Masonf03d9301f2009-02-04 09:31:06 -0500428 /*
429 * we don't want to send crud past the end of i_size through
430 * compression, that's just a waste of CPU time. So, if the
431 * end of the file is before the start of our current
432 * requested range of bytes, we bail out to the uncompressed
433 * cleanup code that can deal with all of this.
434 *
435 * It isn't really the fastest way to fix things, but this is a
436 * very uncommon corner.
437 */
438 if (actual_end <= start)
439 goto cleanup_and_bail_uncompressed;
440
Chris Masonc8b97812008-10-29 14:49:59 -0400441 total_compressed = actual_end - start;
442
Shilong Wang4bcbb332014-10-07 18:44:35 -0400443 /*
444 * skip compression for a small file range(<=blocksize) that
445 * isn't an inline extent, since it dosen't save disk space at all.
446 */
447 if (total_compressed <= blocksize &&
448 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
449 goto cleanup_and_bail_uncompressed;
450
Chris Masonc8b97812008-10-29 14:49:59 -0400451 /* we want to make sure that amount of ram required to uncompress
452 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500453 * of a compressed extent to 128k. This is a crucial number
454 * because it also controls how easily we can spread reads across
455 * cpus for decompression.
456 *
457 * We also want to make sure the amount of IO required to do
458 * a random read is reasonably small, so we limit the size of
459 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400460 */
461 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000462 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500463 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400464 total_in = 0;
465 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400466
Chris Mason771ed682008-11-06 22:02:51 -0500467 /*
468 * we do compression for mount -o compress and when the
469 * inode has not been flagged as nocompress. This flag can
470 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400471 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800472 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400473 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100474 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800475 if (!pages) {
476 /* just bail out to the uncompressed code */
477 goto cont;
478 }
Chris Mason179e29e2007-11-01 11:28:41 -0400479
Li Zefan261507a02010-12-17 14:21:50 +0800480 if (BTRFS_I(inode)->force_compress)
481 compress_type = BTRFS_I(inode)->force_compress;
482
Chris Mason4adaa612013-03-26 13:07:00 -0400483 /*
484 * we need to call clear_page_dirty_for_io on each
485 * page in the range. Otherwise applications with the file
486 * mmap'd can wander in and change the page contents while
487 * we are compressing them.
488 *
489 * If the compression fails for any reason, we set the pages
490 * dirty again later on.
491 */
492 extent_range_clear_dirty_for_io(inode, start, end);
493 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800494 ret = btrfs_compress_pages(compress_type,
495 inode->i_mapping, start,
496 total_compressed, pages,
497 nr_pages, &nr_pages_ret,
498 &total_in,
499 &total_compressed,
500 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400501
502 if (!ret) {
503 unsigned long offset = total_compressed &
504 (PAGE_CACHE_SIZE - 1);
505 struct page *page = pages[nr_pages_ret - 1];
506 char *kaddr;
507
508 /* zero the tail end of the last page, we might be
509 * sending it down to disk
510 */
511 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800512 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400513 memset(kaddr + offset, 0,
514 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800515 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400516 }
517 will_compress = 1;
518 }
519 }
Li Zefan560f7d72011-09-08 10:22:01 +0800520cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400521 if (start == 0) {
522 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500523 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400524 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500525 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400526 */
Josef Bacik00361582013-08-14 14:02:47 -0400527 ret = cow_file_range_inline(root, inode, start, end,
528 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400529 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500530 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400531 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000532 total_compressed,
533 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400534 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100535 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400536 unsigned long clear_flags = EXTENT_DELALLOC |
537 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100538 unsigned long page_error_op;
539
Josef Bacik151a41b2013-07-29 13:22:24 -0400540 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100541 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400542
Chris Mason771ed682008-11-06 22:02:51 -0500543 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100544 * inline extent creation worked or returned error,
545 * we don't need to create any more async work items.
546 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500547 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400548 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400549 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400550 PAGE_CLEAR_DIRTY |
551 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100552 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400553 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400554 goto free_pages_out;
555 }
556 }
557
558 if (will_compress) {
559 /*
560 * we aren't doing an inline extent round the compressed size
561 * up to a block size boundary so the allocator does sane
562 * things
563 */
Qu Wenruofda28322013-02-26 08:10:22 +0000564 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400565
566 /*
567 * one last check to make sure the compression is really a
568 * win, compare the page count read with the blocks on disk
569 */
Qu Wenruofda28322013-02-26 08:10:22 +0000570 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400571 if (total_compressed >= total_in) {
572 will_compress = 0;
573 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400574 num_bytes = total_in;
575 }
576 }
577 if (!will_compress && pages) {
578 /*
579 * the compression code ran but failed to make things smaller,
580 * free any pages it allocated and our page pointer array
581 */
582 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400583 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400584 page_cache_release(pages[i]);
585 }
586 kfree(pages);
587 pages = NULL;
588 total_compressed = 0;
589 nr_pages_ret = 0;
590
591 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500592 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
593 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500594 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500595 }
Chris Masonc8b97812008-10-29 14:49:59 -0400596 }
Chris Mason771ed682008-11-06 22:02:51 -0500597 if (will_compress) {
598 *num_added += 1;
599
600 /* the async work queues will take care of doing actual
601 * allocation on disk for these compressed pages,
602 * and will submit them to the elevator.
603 */
604 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800605 total_compressed, pages, nr_pages_ret,
606 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500607
Yan, Zheng24ae6362010-12-06 07:02:36 +0000608 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500609 start += num_bytes;
610 pages = NULL;
611 cond_resched();
612 goto again;
613 }
614 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500615cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500616 /*
617 * No compression, but we still need to write the pages in
618 * the file we've been given so far. redirty the locked
619 * page if it corresponds to our extent and set things up
620 * for the async work queue to run cow_file_range to do
621 * the normal delalloc dance
622 */
623 if (page_offset(locked_page) >= start &&
624 page_offset(locked_page) <= end) {
625 __set_page_dirty_nobuffers(locked_page);
626 /* unlocked later on in the async handlers */
627 }
Chris Mason4adaa612013-03-26 13:07:00 -0400628 if (redirty)
629 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800630 add_async_extent(async_cow, start, end - start + 1,
631 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500632 *num_added += 1;
633 }
634
Filipe Mananac44f6492014-10-09 21:15:44 +0100635 return;
Chris Mason771ed682008-11-06 22:02:51 -0500636
637free_pages_out:
638 for (i = 0; i < nr_pages_ret; i++) {
639 WARN_ON(pages[i]->mapping);
640 page_cache_release(pages[i]);
641 }
Chris Masond3977122009-01-05 21:25:51 -0500642 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500643}
Chris Mason771ed682008-11-06 22:02:51 -0500644
Filipe Manana40ae8372014-10-06 22:14:24 +0100645static void free_async_extent_pages(struct async_extent *async_extent)
646{
647 int i;
648
649 if (!async_extent->pages)
650 return;
651
652 for (i = 0; i < async_extent->nr_pages; i++) {
653 WARN_ON(async_extent->pages[i]->mapping);
654 page_cache_release(async_extent->pages[i]);
655 }
656 kfree(async_extent->pages);
657 async_extent->nr_pages = 0;
658 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500659}
660
661/*
662 * phase two of compressed writeback. This is the ordered portion
663 * of the code, which only gets called in the order the work was
664 * queued. We walk all the async extents created by compress_file_range
665 * and send them down to the disk.
666 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100667static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500668 struct async_cow *async_cow)
669{
670 struct async_extent *async_extent;
671 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500672 struct btrfs_key ins;
673 struct extent_map *em;
674 struct btrfs_root *root = BTRFS_I(inode)->root;
675 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
676 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500677 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500678
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500679again:
Chris Masond3977122009-01-05 21:25:51 -0500680 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500681 async_extent = list_entry(async_cow->extents.next,
682 struct async_extent, list);
683 list_del(&async_extent->list);
684
685 io_tree = &BTRFS_I(inode)->io_tree;
686
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500687retry:
Chris Mason771ed682008-11-06 22:02:51 -0500688 /* did the compression code fall back to uncompressed IO? */
689 if (!async_extent->pages) {
690 int page_started = 0;
691 unsigned long nr_written = 0;
692
693 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000694 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100695 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500696
697 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500698 ret = cow_file_range(inode, async_cow->locked_page,
699 async_extent->start,
700 async_extent->start +
701 async_extent->ram_size - 1,
702 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500703
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100704 /* JDM XXX */
705
Chris Mason771ed682008-11-06 22:02:51 -0500706 /*
707 * if page_started, cow_file_range inserted an
708 * inline extent and took care of all the unlocking
709 * and IO for us. Otherwise, we need to submit
710 * all those pages down to the drive.
711 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500712 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500713 extent_write_locked_range(io_tree,
714 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500715 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500716 async_extent->ram_size - 1,
717 btrfs_get_extent,
718 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500719 else if (ret)
720 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500721 kfree(async_extent);
722 cond_resched();
723 continue;
724 }
725
726 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100727 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500728
Josef Bacik00361582013-08-14 14:02:47 -0400729 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500730 async_extent->compressed_size,
731 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800732 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500733 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100734 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500735
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400736 if (ret == -ENOSPC) {
737 unlock_extent(io_tree, async_extent->start,
738 async_extent->start +
739 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800740
741 /*
742 * we need to redirty the pages if we decide to
743 * fallback to uncompressed IO, otherwise we
744 * will not submit these pages down to lower
745 * layers.
746 */
747 extent_range_redirty_for_io(inode,
748 async_extent->start,
749 async_extent->start +
750 async_extent->ram_size - 1);
751
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100752 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400753 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500754 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500755 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000756 /*
757 * here we're doing allocation and writeback of the
758 * compressed pages
759 */
760 btrfs_drop_extent_cache(inode, async_extent->start,
761 async_extent->start +
762 async_extent->ram_size - 1, 0);
763
David Sterba172ddd62011-04-21 00:48:27 +0200764 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000765 if (!em) {
766 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500767 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000768 }
Chris Mason771ed682008-11-06 22:02:51 -0500769 em->start = async_extent->start;
770 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500771 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400772 em->mod_start = em->start;
773 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500774
775 em->block_start = ins.objectid;
776 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500777 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400778 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500779 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800780 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500781 set_bit(EXTENT_FLAG_PINNED, &em->flags);
782 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400783 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500784
Chris Masond3977122009-01-05 21:25:51 -0500785 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400786 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400787 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400788 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500789 if (ret != -EEXIST) {
790 free_extent_map(em);
791 break;
792 }
793 btrfs_drop_extent_cache(inode, async_extent->start,
794 async_extent->start +
795 async_extent->ram_size - 1, 0);
796 }
797
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500798 if (ret)
799 goto out_free_reserve;
800
Li Zefan261507a02010-12-17 14:21:50 +0800801 ret = btrfs_add_ordered_extent_compress(inode,
802 async_extent->start,
803 ins.objectid,
804 async_extent->ram_size,
805 ins.offset,
806 BTRFS_ORDERED_COMPRESSED,
807 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100808 if (ret) {
809 btrfs_drop_extent_cache(inode, async_extent->start,
810 async_extent->start +
811 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500812 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100813 }
Chris Mason771ed682008-11-06 22:02:51 -0500814
Chris Mason771ed682008-11-06 22:02:51 -0500815 /*
816 * clear dirty, set writeback and unlock the pages.
817 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400818 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400819 async_extent->start +
820 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400821 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
822 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400823 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500824 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500825 async_extent->start,
826 async_extent->ram_size,
827 ins.objectid,
828 ins.offset, async_extent->pages,
829 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100830 if (ret) {
831 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
832 struct page *p = async_extent->pages[0];
833 const u64 start = async_extent->start;
834 const u64 end = start + async_extent->ram_size - 1;
835
836 p->mapping = inode->i_mapping;
837 tree->ops->writepage_end_io_hook(p, start, end,
838 NULL, 0);
839 p->mapping = NULL;
840 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
841 PAGE_END_WRITEBACK |
842 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100843 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100844 }
Chris Mason771ed682008-11-06 22:02:51 -0500845 alloc_hint = ins.objectid + ins.offset;
846 kfree(async_extent);
847 cond_resched();
848 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100849 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500850out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800851 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100852out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400853 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500854 async_extent->start +
855 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400856 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400857 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
858 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100859 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
860 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100861 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100862 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500863 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500864}
865
Josef Bacik4b46fce2010-05-23 11:00:55 -0400866static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
867 u64 num_bytes)
868{
869 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
870 struct extent_map *em;
871 u64 alloc_hint = 0;
872
873 read_lock(&em_tree->lock);
874 em = search_extent_mapping(em_tree, start, num_bytes);
875 if (em) {
876 /*
877 * if block start isn't an actual block number then find the
878 * first block in this inode and use that as a hint. If that
879 * block is also bogus then just don't worry about it.
880 */
881 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
882 free_extent_map(em);
883 em = search_extent_mapping(em_tree, 0, 0);
884 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
885 alloc_hint = em->block_start;
886 if (em)
887 free_extent_map(em);
888 } else {
889 alloc_hint = em->block_start;
890 free_extent_map(em);
891 }
892 }
893 read_unlock(&em_tree->lock);
894
895 return alloc_hint;
896}
897
Chris Mason771ed682008-11-06 22:02:51 -0500898/*
899 * when extent_io.c finds a delayed allocation range in the file,
900 * the call backs end up in this code. The basic idea is to
901 * allocate extents on disk for the range, and create ordered data structs
902 * in ram to track those extents.
903 *
904 * locked_page is the page that writepage had locked already. We use
905 * it to make sure we don't do extra locks or unlocks.
906 *
907 * *page_started is set to one if we unlock locked_page and do everything
908 * required to start IO on it. It may be clean and already done with
909 * IO when we return.
910 */
Josef Bacik00361582013-08-14 14:02:47 -0400911static noinline int cow_file_range(struct inode *inode,
912 struct page *locked_page,
913 u64 start, u64 end, int *page_started,
914 unsigned long *nr_written,
915 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500916{
Josef Bacik00361582013-08-14 14:02:47 -0400917 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500918 u64 alloc_hint = 0;
919 u64 num_bytes;
920 unsigned long ram_size;
921 u64 disk_num_bytes;
922 u64 cur_alloc_size;
923 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500924 struct btrfs_key ins;
925 struct extent_map *em;
926 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
927 int ret = 0;
928
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400929 if (btrfs_is_free_space_inode(inode)) {
930 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500931 ret = -EINVAL;
932 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400933 }
Chris Mason771ed682008-11-06 22:02:51 -0500934
Qu Wenruofda28322013-02-26 08:10:22 +0000935 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500936 num_bytes = max(blocksize, num_bytes);
937 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500938
Chris Mason4cb53002011-05-24 15:35:30 -0400939 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400940 if (num_bytes < 64 * 1024 &&
941 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400942 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400943
Chris Mason771ed682008-11-06 22:02:51 -0500944 if (start == 0) {
945 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400946 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
947 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500948 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400949 extent_clear_unlock_delalloc(inode, start, end, NULL,
950 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400951 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400952 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
953 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000954
Chris Mason771ed682008-11-06 22:02:51 -0500955 *nr_written = *nr_written +
956 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
957 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500958 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100959 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100960 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500961 }
962 }
Chris Masonc8b97812008-10-29 14:49:59 -0400963
964 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200965 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400966
Josef Bacik4b46fce2010-05-23 11:00:55 -0400967 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400968 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400969
Chris Masond3977122009-01-05 21:25:51 -0500970 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400971 unsigned long op;
972
Josef Bacik287a0ab2010-03-19 18:07:23 +0000973 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400974 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500975 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800976 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400977 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100978 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500979
David Sterba172ddd62011-04-21 00:48:27 +0200980 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000981 if (!em) {
982 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000983 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000984 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400985 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500986 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500987 ram_size = ins.offset;
988 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400989 em->mod_start = em->start;
990 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400991
Chris Masone6dcd2d2008-07-17 12:53:50 -0400992 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400993 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500994 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400995 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400996 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400997 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400998 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400999
Chris Masond3977122009-01-05 21:25:51 -05001000 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001001 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001002 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001003 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001004 if (ret != -EEXIST) {
1005 free_extent_map(em);
1006 break;
1007 }
1008 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001009 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001010 }
Liu Boace68ba2013-04-22 10:53:47 +00001011 if (ret)
1012 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001013
Chris Mason98d20f62008-04-14 09:46:10 -04001014 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001015 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001016 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001017 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001018 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001019
Yan Zheng17d217f2008-12-12 10:03:38 -05001020 if (root->root_key.objectid ==
1021 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1022 ret = btrfs_reloc_clone_csums(inode, start,
1023 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001024 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001025 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001026 }
1027
Chris Masond3977122009-01-05 21:25:51 -05001028 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001029 break;
Chris Masond3977122009-01-05 21:25:51 -05001030
Chris Masonc8b97812008-10-29 14:49:59 -04001031 /* we're not doing compressed IO, don't unlock the first
1032 * page (which the caller expects to stay locked), don't
1033 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001034 *
1035 * Do set the Private2 bit so we know this page was properly
1036 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001037 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001038 op = unlock ? PAGE_UNLOCK : 0;
1039 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001040
Josef Bacikc2790a22013-07-29 11:20:47 -04001041 extent_clear_unlock_delalloc(inode, start,
1042 start + ram_size - 1, locked_page,
1043 EXTENT_LOCKED | EXTENT_DELALLOC,
1044 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001045 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001046 num_bytes -= cur_alloc_size;
1047 alloc_hint = ins.objectid + ins.offset;
1048 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001049 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001050out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001051 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001052
Filipe Mananad9f85962014-08-25 10:43:00 +01001053out_drop_extent_cache:
1054 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001055out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001056 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001057out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001058 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001059 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1060 EXTENT_DELALLOC | EXTENT_DEFRAG,
1061 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1062 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001063 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001064}
Chris Masonc8b97812008-10-29 14:49:59 -04001065
Chris Mason771ed682008-11-06 22:02:51 -05001066/*
1067 * work queue call back to started compression on a file and pages
1068 */
1069static noinline void async_cow_start(struct btrfs_work *work)
1070{
1071 struct async_cow *async_cow;
1072 int num_added = 0;
1073 async_cow = container_of(work, struct async_cow, work);
1074
1075 compress_file_range(async_cow->inode, async_cow->locked_page,
1076 async_cow->start, async_cow->end, async_cow,
1077 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001078 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001079 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001080 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001081 }
Chris Mason771ed682008-11-06 22:02:51 -05001082}
1083
1084/*
1085 * work queue call back to submit previously compressed pages
1086 */
1087static noinline void async_cow_submit(struct btrfs_work *work)
1088{
1089 struct async_cow *async_cow;
1090 struct btrfs_root *root;
1091 unsigned long nr_pages;
1092
1093 async_cow = container_of(work, struct async_cow, work);
1094
1095 root = async_cow->root;
1096 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1097 PAGE_CACHE_SHIFT;
1098
David Sterbaee863952015-02-16 19:41:40 +01001099 /*
1100 * atomic_sub_return implies a barrier for waitqueue_active
1101 */
Josef Bacik66657b32012-08-01 15:36:24 -04001102 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001103 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001104 waitqueue_active(&root->fs_info->async_submit_wait))
1105 wake_up(&root->fs_info->async_submit_wait);
1106
Chris Masond3977122009-01-05 21:25:51 -05001107 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001108 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001109}
Chris Masonc8b97812008-10-29 14:49:59 -04001110
Chris Mason771ed682008-11-06 22:02:51 -05001111static noinline void async_cow_free(struct btrfs_work *work)
1112{
1113 struct async_cow *async_cow;
1114 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001115 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001116 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001117 kfree(async_cow);
1118}
1119
1120static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1121 u64 start, u64 end, int *page_started,
1122 unsigned long *nr_written)
1123{
1124 struct async_cow *async_cow;
1125 struct btrfs_root *root = BTRFS_I(inode)->root;
1126 unsigned long nr_pages;
1127 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001128 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001129
Chris Masona3429ab2009-10-08 12:30:20 -04001130 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1131 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001132 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001133 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001134 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001135 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001136 async_cow->root = root;
1137 async_cow->locked_page = locked_page;
1138 async_cow->start = start;
1139
Wang Shilongf79707b2014-07-17 11:44:09 +08001140 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1141 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001142 cur_end = end;
1143 else
1144 cur_end = min(end, start + 512 * 1024 - 1);
1145
1146 async_cow->end = cur_end;
1147 INIT_LIST_HEAD(&async_cow->extents);
1148
Liu Bo9e0af232014-08-15 23:36:53 +08001149 btrfs_init_work(&async_cow->work,
1150 btrfs_delalloc_helper,
1151 async_cow_start, async_cow_submit,
1152 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001153
Chris Mason771ed682008-11-06 22:02:51 -05001154 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1155 PAGE_CACHE_SHIFT;
1156 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1157
Qu Wenruoafe3d242014-02-28 10:46:07 +08001158 btrfs_queue_work(root->fs_info->delalloc_workers,
1159 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001160
1161 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1162 wait_event(root->fs_info->async_submit_wait,
1163 (atomic_read(&root->fs_info->async_delalloc_pages) <
1164 limit));
1165 }
1166
Chris Masond3977122009-01-05 21:25:51 -05001167 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001168 atomic_read(&root->fs_info->async_delalloc_pages)) {
1169 wait_event(root->fs_info->async_submit_wait,
1170 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1171 0));
1172 }
1173
1174 *nr_written += nr_pages;
1175 start = cur_end + 1;
1176 }
1177 *page_started = 1;
1178 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001179}
1180
Chris Masond3977122009-01-05 21:25:51 -05001181static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001182 u64 bytenr, u64 num_bytes)
1183{
1184 int ret;
1185 struct btrfs_ordered_sum *sums;
1186 LIST_HEAD(list);
1187
Yan Zheng07d400a2009-01-06 11:42:00 -05001188 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001189 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001190 if (ret == 0 && list_empty(&list))
1191 return 0;
1192
1193 while (!list_empty(&list)) {
1194 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1195 list_del(&sums->list);
1196 kfree(sums);
1197 }
1198 return 1;
1199}
1200
Chris Masond352ac62008-09-29 15:18:18 -04001201/*
1202 * when nowcow writeback call back. This checks for snapshots or COW copies
1203 * of the extents that exist in the file, and COWs the file as required.
1204 *
1205 * If no cow copies or snapshots exist, we write directly to the existing
1206 * blocks on disk
1207 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001208static noinline int run_delalloc_nocow(struct inode *inode,
1209 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001210 u64 start, u64 end, int *page_started, int force,
1211 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001212{
Chris Masonbe20aa92007-12-17 20:14:01 -05001213 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001214 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001215 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001216 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001217 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001218 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001219 u64 cow_start;
1220 u64 cur_offset;
1221 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001222 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001223 u64 disk_bytenr;
1224 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001225 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001226 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001227 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001228 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001229 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001230 int nocow;
1231 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001232 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001233 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001234
1235 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001236 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001237 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1238 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001239 EXTENT_DO_ACCOUNTING |
1240 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001241 PAGE_CLEAR_DIRTY |
1242 PAGE_SET_WRITEBACK |
1243 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001244 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001245 }
Li Zefan82d59022011-04-20 10:33:24 +08001246
Liu Bo83eea1f2012-07-10 05:28:39 -06001247 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001248
1249 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001250 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001251 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001252 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001253
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001254 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001255 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1256 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001257 EXTENT_DO_ACCOUNTING |
1258 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001259 PAGE_CLEAR_DIRTY |
1260 PAGE_SET_WRITEBACK |
1261 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001262 btrfs_free_path(path);
1263 return PTR_ERR(trans);
1264 }
1265
Josef Bacik74b21072011-04-13 12:02:53 -04001266 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001267
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 cow_start = (u64)-1;
1269 cur_offset = start;
1270 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001271 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001273 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001274 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1276 leaf = path->nodes[0];
1277 btrfs_item_key_to_cpu(leaf, &found_key,
1278 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001279 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 found_key.type == BTRFS_EXTENT_DATA_KEY)
1281 path->slots[0]--;
1282 }
1283 check_prev = 0;
1284next_slot:
1285 leaf = path->nodes[0];
1286 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1287 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001288 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001289 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 if (ret > 0)
1291 break;
1292 leaf = path->nodes[0];
1293 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001294
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 nocow = 0;
1296 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001297 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001298 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001299
Li Zefan33345d012011-04-20 10:31:50 +08001300 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1302 found_key.offset > end)
1303 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001304
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 if (found_key.offset > cur_offset) {
1306 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001307 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 goto out_check;
1309 }
Chris Masonc31f8832008-01-08 15:46:31 -05001310
Yan Zheng80ff3852008-10-30 14:20:02 -04001311 fi = btrfs_item_ptr(leaf, path->slots[0],
1312 struct btrfs_file_extent_item);
1313 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001314
Josef Bacikcc95bef2013-04-04 14:31:27 -04001315 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001316 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1317 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001319 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001320 extent_end = found_key.offset +
1321 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001322 disk_num_bytes =
1323 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001324 if (extent_end <= start) {
1325 path->slots[0]++;
1326 goto next_slot;
1327 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001328 if (disk_bytenr == 0)
1329 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001330 if (btrfs_file_extent_compression(leaf, fi) ||
1331 btrfs_file_extent_encryption(leaf, fi) ||
1332 btrfs_file_extent_other_encoding(leaf, fi))
1333 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001334 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1335 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001336 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001337 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001338 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001339 found_key.offset -
1340 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001341 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001342 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001343 disk_bytenr += cur_offset - found_key.offset;
1344 num_bytes = min(end + 1, extent_end) - cur_offset;
1345 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001346 * if there are pending snapshots for this root,
1347 * we fall into common COW way.
1348 */
1349 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001350 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001351 if (!err)
1352 goto out_check;
1353 }
1354 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001355 * force cow if csum exists in the range.
1356 * this ensure that csum for a given extent are
1357 * either valid or do not exist.
1358 */
1359 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1360 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 nocow = 1;
1362 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1363 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001364 btrfs_file_extent_inline_len(leaf,
1365 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 extent_end = ALIGN(extent_end, root->sectorsize);
1367 } else {
1368 BUG_ON(1);
1369 }
1370out_check:
1371 if (extent_end <= start) {
1372 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001373 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001374 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001375 goto next_slot;
1376 }
1377 if (!nocow) {
1378 if (cow_start == (u64)-1)
1379 cow_start = cur_offset;
1380 cur_offset = extent_end;
1381 if (cur_offset > end)
1382 break;
1383 path->slots[0]++;
1384 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001385 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001386
David Sterbab3b4aa72011-04-21 01:20:15 +02001387 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001388 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001389 ret = cow_file_range(inode, locked_page,
1390 cow_start, found_key.offset - 1,
1391 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001392 if (ret) {
1393 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001394 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001395 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001396 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001397 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001398 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001399
Yan Zhengd899e052008-10-30 14:25:28 -04001400 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1401 struct extent_map *em;
1402 struct extent_map_tree *em_tree;
1403 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001404 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001405 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001406 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001407 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001408 em->len = num_bytes;
1409 em->block_len = num_bytes;
1410 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001411 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001412 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001413 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001414 em->mod_start = em->start;
1415 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001416 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001417 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001418 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001419 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001420 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001421 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001422 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001423 if (ret != -EEXIST) {
1424 free_extent_map(em);
1425 break;
1426 }
1427 btrfs_drop_extent_cache(inode, em->start,
1428 em->start + em->len - 1, 0);
1429 }
1430 type = BTRFS_ORDERED_PREALLOC;
1431 } else {
1432 type = BTRFS_ORDERED_NOCOW;
1433 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001434
1435 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001436 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001437 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001438
Yan, Zhengefa56462010-05-16 10:49:59 -04001439 if (root->root_key.objectid ==
1440 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1441 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1442 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001443 if (ret) {
1444 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001445 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001446 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001447 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001448 }
1449
Josef Bacikc2790a22013-07-29 11:20:47 -04001450 extent_clear_unlock_delalloc(inode, cur_offset,
1451 cur_offset + num_bytes - 1,
1452 locked_page, EXTENT_LOCKED |
1453 EXTENT_DELALLOC, PAGE_UNLOCK |
1454 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001455 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001456 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001457 cur_offset = extent_end;
1458 if (cur_offset > end)
1459 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001460 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001461 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001462
Josef Bacik17ca04a2012-05-31 15:58:55 -04001463 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001464 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001465 cur_offset = end;
1466 }
1467
Yan Zheng80ff3852008-10-30 14:20:02 -04001468 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001469 ret = cow_file_range(inode, locked_page, cow_start, end,
1470 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001471 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001472 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001473 }
1474
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001475error:
Miao Xiea698d0752012-09-20 01:51:59 -06001476 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001477 if (!ret)
1478 ret = err;
1479
Josef Bacik17ca04a2012-05-31 15:58:55 -04001480 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001481 extent_clear_unlock_delalloc(inode, cur_offset, end,
1482 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001483 EXTENT_DELALLOC | EXTENT_DEFRAG |
1484 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1485 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001486 PAGE_SET_WRITEBACK |
1487 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001488 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001489 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001490}
1491
Wang Shilong47059d92014-07-03 18:22:07 +08001492static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1493{
1494
1495 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1496 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1497 return 0;
1498
1499 /*
1500 * @defrag_bytes is a hint value, no spinlock held here,
1501 * if is not zero, it means the file is defragging.
1502 * Force cow if given extent needs to be defragged.
1503 */
1504 if (BTRFS_I(inode)->defrag_bytes &&
1505 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1506 EXTENT_DEFRAG, 0, NULL))
1507 return 1;
1508
1509 return 0;
1510}
1511
Chris Masond352ac62008-09-29 15:18:18 -04001512/*
1513 * extent_io.c call back to do delayed allocation processing
1514 */
Chris Masonc8b97812008-10-29 14:49:59 -04001515static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001516 u64 start, u64 end, int *page_started,
1517 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001518{
Chris Masonbe20aa92007-12-17 20:14:01 -05001519 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001520 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001521
Wang Shilong47059d92014-07-03 18:22:07 +08001522 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001523 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001524 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001525 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001526 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001527 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001528 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001529 ret = cow_file_range(inode, locked_page, start, end,
1530 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001531 } else {
1532 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1533 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001534 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001535 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001536 }
Chris Masonb888db22007-08-27 16:49:44 -04001537 return ret;
1538}
1539
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001540static void btrfs_split_extent_hook(struct inode *inode,
1541 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001542{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001543 u64 size;
1544
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001545 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001546 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001547 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001548
Josef Bacikdcab6a32015-02-11 15:08:59 -05001549 size = orig->end - orig->start + 1;
1550 if (size > BTRFS_MAX_EXTENT_SIZE) {
1551 u64 num_extents;
1552 u64 new_size;
1553
1554 /*
Josef Bacikba117212015-03-13 15:01:24 -04001555 * See the explanation in btrfs_merge_extent_hook, the same
1556 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001557 */
1558 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001559 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001560 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001561 new_size = split - orig->start;
1562 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1563 BTRFS_MAX_EXTENT_SIZE);
1564 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1565 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001566 return;
1567 }
1568
Josef Bacik9e0baf62011-07-15 15:16:44 +00001569 spin_lock(&BTRFS_I(inode)->lock);
1570 BTRFS_I(inode)->outstanding_extents++;
1571 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001572}
1573
1574/*
1575 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1576 * extents so we can keep track of new extents that are just merged onto old
1577 * extents, such as when we are doing sequential writes, so we can properly
1578 * account for the metadata space we'll need.
1579 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001580static void btrfs_merge_extent_hook(struct inode *inode,
1581 struct extent_state *new,
1582 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001583{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001584 u64 new_size, old_size;
1585 u64 num_extents;
1586
Josef Bacik9ed74f22009-09-11 16:12:44 -04001587 /* not delalloc, ignore it */
1588 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001589 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001590
Josef Bacik8461a3d2015-03-13 15:12:08 -04001591 if (new->start > other->start)
1592 new_size = new->end - other->start + 1;
1593 else
1594 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001595
1596 /* we're not bigger than the max, unreserve the space and go */
1597 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1598 spin_lock(&BTRFS_I(inode)->lock);
1599 BTRFS_I(inode)->outstanding_extents--;
1600 spin_unlock(&BTRFS_I(inode)->lock);
1601 return;
1602 }
1603
1604 /*
Josef Bacikba117212015-03-13 15:01:24 -04001605 * We have to add up either side to figure out how many extents were
1606 * accounted for before we merged into one big extent. If the number of
1607 * extents we accounted for is <= the amount we need for the new range
1608 * then we can return, otherwise drop. Think of it like this
1609 *
1610 * [ 4k][MAX_SIZE]
1611 *
1612 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1613 * need 2 outstanding extents, on one side we have 1 and the other side
1614 * we have 1 so they are == and we can return. But in this case
1615 *
1616 * [MAX_SIZE+4k][MAX_SIZE+4k]
1617 *
1618 * Each range on their own accounts for 2 extents, but merged together
1619 * they are only 3 extents worth of accounting, so we need to drop in
1620 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 */
Josef Bacikba117212015-03-13 15:01:24 -04001622 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001623 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1624 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001625 old_size = new->end - new->start + 1;
1626 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1627 BTRFS_MAX_EXTENT_SIZE);
1628
Josef Bacikdcab6a32015-02-11 15:08:59 -05001629 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001630 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001631 return;
1632
Josef Bacik9e0baf62011-07-15 15:16:44 +00001633 spin_lock(&BTRFS_I(inode)->lock);
1634 BTRFS_I(inode)->outstanding_extents--;
1635 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001636}
1637
Miao Xieeb73c1b2013-05-15 07:48:22 +00001638static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1639 struct inode *inode)
1640{
1641 spin_lock(&root->delalloc_lock);
1642 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1643 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1644 &root->delalloc_inodes);
1645 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1646 &BTRFS_I(inode)->runtime_flags);
1647 root->nr_delalloc_inodes++;
1648 if (root->nr_delalloc_inodes == 1) {
1649 spin_lock(&root->fs_info->delalloc_root_lock);
1650 BUG_ON(!list_empty(&root->delalloc_root));
1651 list_add_tail(&root->delalloc_root,
1652 &root->fs_info->delalloc_roots);
1653 spin_unlock(&root->fs_info->delalloc_root_lock);
1654 }
1655 }
1656 spin_unlock(&root->delalloc_lock);
1657}
1658
1659static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1660 struct inode *inode)
1661{
1662 spin_lock(&root->delalloc_lock);
1663 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1664 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1665 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1666 &BTRFS_I(inode)->runtime_flags);
1667 root->nr_delalloc_inodes--;
1668 if (!root->nr_delalloc_inodes) {
1669 spin_lock(&root->fs_info->delalloc_root_lock);
1670 BUG_ON(list_empty(&root->delalloc_root));
1671 list_del_init(&root->delalloc_root);
1672 spin_unlock(&root->fs_info->delalloc_root_lock);
1673 }
1674 }
1675 spin_unlock(&root->delalloc_lock);
1676}
1677
Chris Masond352ac62008-09-29 15:18:18 -04001678/*
1679 * extent_io.c set_bit_hook, used to track delayed allocation
1680 * bytes in this file, and to maintain the list of inodes that
1681 * have pending delalloc work to be done.
1682 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001683static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001684 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001685{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001686
Wang Shilong47059d92014-07-03 18:22:07 +08001687 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1688 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001689 /*
1690 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001691 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001692 * bit, which is only set or cleared with irqs on
1693 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001694 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001695 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001696 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001697 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001698
Josef Bacik9e0baf62011-07-15 15:16:44 +00001699 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001700 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001701 } else {
1702 spin_lock(&BTRFS_I(inode)->lock);
1703 BTRFS_I(inode)->outstanding_extents++;
1704 spin_unlock(&BTRFS_I(inode)->lock);
1705 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001706
Josef Bacik6a3891c2015-03-16 17:38:52 -04001707 /* For sanity tests */
1708 if (btrfs_test_is_dummy_root(root))
1709 return;
1710
Miao Xie963d6782013-01-29 10:10:51 +00001711 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1712 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001713 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001714 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001715 if (*bits & EXTENT_DEFRAG)
1716 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001717 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001718 &BTRFS_I(inode)->runtime_flags))
1719 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001720 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001721 }
Chris Mason291d6732008-01-29 15:55:23 -05001722}
1723
Chris Masond352ac62008-09-29 15:18:18 -04001724/*
1725 * extent_io.c clear_bit_hook, see set_bit_hook for why
1726 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001727static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001728 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001729 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001730{
Wang Shilong47059d92014-07-03 18:22:07 +08001731 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001732 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1733 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001734
1735 spin_lock(&BTRFS_I(inode)->lock);
1736 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1737 BTRFS_I(inode)->defrag_bytes -= len;
1738 spin_unlock(&BTRFS_I(inode)->lock);
1739
Chris Mason75eff682008-12-15 15:54:40 -05001740 /*
1741 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001742 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001743 * bit, which is only set or cleared with irqs on
1744 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001745 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001746 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001747 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001748
Josef Bacik9e0baf62011-07-15 15:16:44 +00001749 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001750 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001751 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1752 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001753 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001754 spin_unlock(&BTRFS_I(inode)->lock);
1755 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001756
Josef Bacikb6d08f02013-09-27 14:57:43 -04001757 /*
1758 * We don't reserve metadata space for space cache inodes so we
1759 * don't need to call dellalloc_release_metadata if there is an
1760 * error.
1761 */
1762 if (*bits & EXTENT_DO_ACCOUNTING &&
1763 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001764 btrfs_delalloc_release_metadata(inode, len);
1765
Josef Bacik6a3891c2015-03-16 17:38:52 -04001766 /* For sanity tests. */
1767 if (btrfs_test_is_dummy_root(root))
1768 return;
1769
Josef Bacik0cb59c92010-07-02 12:14:14 -04001770 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001771 && do_list && !(state->state & EXTENT_NORESERVE))
Qu Wenruodf480632015-09-08 17:25:54 +08001772 __btrfs_free_reserved_data_space(inode, state->start,
1773 len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001774
Miao Xie963d6782013-01-29 10:10:51 +00001775 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1776 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001777 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001778 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001779 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001780 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001781 &BTRFS_I(inode)->runtime_flags))
1782 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001783 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001784 }
Chris Mason291d6732008-01-29 15:55:23 -05001785}
1786
Chris Masond352ac62008-09-29 15:18:18 -04001787/*
1788 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1789 * we don't create bios that span stripes or chunks
1790 */
David Woodhouse64a16702009-07-15 23:29:37 +01001791int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001792 size_t size, struct bio *bio,
1793 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001794{
1795 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001796 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001797 u64 length = 0;
1798 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001799 int ret;
1800
Chris Mason771ed682008-11-06 22:02:51 -05001801 if (bio_flags & EXTENT_BIO_COMPRESSED)
1802 return 0;
1803
Kent Overstreet4f024f32013-10-11 15:44:27 -07001804 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001805 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001806 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001807 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001808 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001809 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001810 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001811 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001812 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001813}
1814
Chris Masond352ac62008-09-29 15:18:18 -04001815/*
1816 * in order to insert checksums into the metadata in large chunks,
1817 * we wait until bio submission time. All the pages in the bio are
1818 * checksummed and sums are attached onto the ordered extent record.
1819 *
1820 * At IO completion time the cums attached on the ordered extent record
1821 * are inserted into the btree
1822 */
Chris Masond3977122009-01-05 21:25:51 -05001823static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1824 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001825 unsigned long bio_flags,
1826 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001827{
Chris Mason065631f2008-02-20 12:07:25 -05001828 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001829 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001830
Chris Masond20f7042008-12-08 16:58:54 -05001831 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001832 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001833 return 0;
1834}
Chris Masone0156402008-04-16 11:15:20 -04001835
Chris Mason4a69a412008-11-06 22:03:00 -05001836/*
1837 * in order to insert checksums into the metadata in large chunks,
1838 * we wait until bio submission time. All the pages in the bio are
1839 * checksummed and sums are attached onto the ordered extent record.
1840 *
1841 * At IO completion time the cums attached on the ordered extent record
1842 * are inserted into the btree
1843 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001844static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001845 int mirror_num, unsigned long bio_flags,
1846 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001847{
1848 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001849 int ret;
1850
1851 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001852 if (ret) {
1853 bio->bi_error = ret;
1854 bio_endio(bio);
1855 }
Stefan Behrens61891922012-11-05 18:51:52 +01001856 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001857}
1858
Chris Masond352ac62008-09-29 15:18:18 -04001859/*
Chris Masoncad321a2008-12-17 14:51:42 -05001860 * extent_io.c submission hook. This does the right thing for csum calculation
1861 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001862 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001863static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001864 int mirror_num, unsigned long bio_flags,
1865 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001866{
1867 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301868 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001869 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001870 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001871 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001872
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001873 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001874
Liu Bo83eea1f2012-07-10 05:28:39 -06001875 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301876 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001877
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001878 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001879 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1880 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001881 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001882
Chris Masond20f7042008-12-08 16:58:54 -05001883 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001884 ret = btrfs_submit_compressed_read(inode, bio,
1885 mirror_num,
1886 bio_flags);
1887 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001888 } else if (!skip_sum) {
1889 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1890 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001891 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001892 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001893 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001894 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001895 /* csum items have already been cloned */
1896 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1897 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001898 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001899 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001900 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001901 bio_flags, bio_offset,
1902 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001903 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001904 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001905 } else if (!skip_sum) {
1906 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1907 if (ret)
1908 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001909 }
1910
Chris Mason0b86a832008-03-24 15:01:56 -04001911mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001912 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1913
1914out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001915 if (ret < 0) {
1916 bio->bi_error = ret;
1917 bio_endio(bio);
1918 }
Stefan Behrens61891922012-11-05 18:51:52 +01001919 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001920}
Chris Mason6885f302008-02-20 16:11:05 -05001921
Chris Masond352ac62008-09-29 15:18:18 -04001922/*
1923 * given a list of ordered sums record them in the inode. This happens
1924 * at IO completion time based on sums calculated at bio submission time.
1925 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001926static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001927 struct inode *inode, u64 file_offset,
1928 struct list_head *list)
1929{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001930 struct btrfs_ordered_sum *sum;
1931
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001932 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001933 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001934 btrfs_csum_file_blocks(trans,
1935 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001936 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001937 }
1938 return 0;
1939}
1940
Josef Bacik2ac55d42010-02-03 19:33:23 +00001941int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1942 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001943{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001944 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001945 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001946 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001947}
1948
Chris Masond352ac62008-09-29 15:18:18 -04001949/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001950struct btrfs_writepage_fixup {
1951 struct page *page;
1952 struct btrfs_work work;
1953};
1954
Christoph Hellwigb2950862008-12-02 09:54:17 -05001955static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001956{
1957 struct btrfs_writepage_fixup *fixup;
1958 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001959 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001960 struct page *page;
1961 struct inode *inode;
1962 u64 page_start;
1963 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001964 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001965
1966 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1967 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001968again:
Chris Mason247e7432008-07-17 12:53:51 -04001969 lock_page(page);
1970 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1971 ClearPageChecked(page);
1972 goto out_page;
1973 }
1974
1975 inode = page->mapping->host;
1976 page_start = page_offset(page);
1977 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1978
Josef Bacik2ac55d42010-02-03 19:33:23 +00001979 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001980 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001981
1982 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001983 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001984 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001985
1986 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1987 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001988 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1989 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001990 unlock_page(page);
1991 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001992 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001993 goto again;
1994 }
Chris Mason247e7432008-07-17 12:53:51 -04001995
Qu Wenruodf480632015-09-08 17:25:54 +08001996 ret = __btrfs_delalloc_reserve_space(inode, page_start,
1997 PAGE_CACHE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001998 if (ret) {
1999 mapping_set_error(page->mapping, ret);
2000 end_extent_writepage(page, ret, page_start, page_end);
2001 ClearPageChecked(page);
2002 goto out;
2003 }
2004
Josef Bacik2ac55d42010-02-03 19:33:23 +00002005 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002006 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002007 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002008out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002009 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2010 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002011out_page:
2012 unlock_page(page);
2013 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002014 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002015}
2016
2017/*
2018 * There are a few paths in the higher layers of the kernel that directly
2019 * set the page dirty bit without asking the filesystem if it is a
2020 * good idea. This causes problems because we want to make sure COW
2021 * properly happens and the data=ordered rules are followed.
2022 *
Chris Masonc8b97812008-10-29 14:49:59 -04002023 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002024 * hasn't been properly setup for IO. We kick off an async process
2025 * to fix it up. The async helper will wait for ordered extents, set
2026 * the delalloc bit and make it safe to write the page.
2027 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002028static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002029{
2030 struct inode *inode = page->mapping->host;
2031 struct btrfs_writepage_fixup *fixup;
2032 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002033
Chris Mason8b62b722009-09-02 16:53:46 -04002034 /* this page is properly in the ordered list */
2035 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002036 return 0;
2037
2038 if (PageChecked(page))
2039 return -EAGAIN;
2040
2041 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2042 if (!fixup)
2043 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002044
Chris Mason247e7432008-07-17 12:53:51 -04002045 SetPageChecked(page);
2046 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002047 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2048 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002049 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002050 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002051 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002052}
2053
Yan Zhengd899e052008-10-30 14:25:28 -04002054static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2055 struct inode *inode, u64 file_pos,
2056 u64 disk_bytenr, u64 disk_num_bytes,
2057 u64 num_bytes, u64 ram_bytes,
2058 u8 compression, u8 encryption,
2059 u16 other_encoding, int extent_type)
2060{
2061 struct btrfs_root *root = BTRFS_I(inode)->root;
2062 struct btrfs_file_extent_item *fi;
2063 struct btrfs_path *path;
2064 struct extent_buffer *leaf;
2065 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002066 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002067 int ret;
2068
2069 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002070 if (!path)
2071 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002072
Chris Masona1ed8352009-09-11 12:27:37 -04002073 /*
2074 * we may be replacing one extent in the tree with another.
2075 * The new extent is pinned in the extent map, and we don't want
2076 * to drop it from the cache until it is completely in the btree.
2077 *
2078 * So, tell btrfs_drop_extents to leave this extent in the cache.
2079 * the caller is expected to unpin it and allow it to be merged
2080 * with the others.
2081 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002082 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2083 file_pos + num_bytes, NULL, 0,
2084 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002085 if (ret)
2086 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002087
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002088 if (!extent_inserted) {
2089 ins.objectid = btrfs_ino(inode);
2090 ins.offset = file_pos;
2091 ins.type = BTRFS_EXTENT_DATA_KEY;
2092
2093 path->leave_spinning = 1;
2094 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2095 sizeof(*fi));
2096 if (ret)
2097 goto out;
2098 }
Yan Zhengd899e052008-10-30 14:25:28 -04002099 leaf = path->nodes[0];
2100 fi = btrfs_item_ptr(leaf, path->slots[0],
2101 struct btrfs_file_extent_item);
2102 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2103 btrfs_set_file_extent_type(leaf, fi, extent_type);
2104 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2105 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2106 btrfs_set_file_extent_offset(leaf, fi, 0);
2107 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2108 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2109 btrfs_set_file_extent_compression(leaf, fi, compression);
2110 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2111 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002112
Yan Zhengd899e052008-10-30 14:25:28 -04002113 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002114 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002115
2116 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002117
2118 ins.objectid = disk_bytenr;
2119 ins.offset = disk_num_bytes;
2120 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002121 ret = btrfs_alloc_reserved_file_extent(trans, root,
2122 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002123 btrfs_ino(inode), file_pos, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002124 if (ret < 0)
2125 goto out;
2126 /*
2127 * Release the reserved range from inode dirty range map, and
2128 * move it to delayed ref codes, as now accounting only happens at
2129 * commit_transaction() time.
2130 */
2131 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2132 ret = btrfs_add_delayed_qgroup_reserve(root->fs_info, trans,
2133 root->objectid, disk_bytenr, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002134out:
Yan Zhengd899e052008-10-30 14:25:28 -04002135 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002136
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002137 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002138}
2139
Liu Bo38c227d2013-01-29 03:18:40 +00002140/* snapshot-aware defrag */
2141struct sa_defrag_extent_backref {
2142 struct rb_node node;
2143 struct old_sa_defrag_extent *old;
2144 u64 root_id;
2145 u64 inum;
2146 u64 file_pos;
2147 u64 extent_offset;
2148 u64 num_bytes;
2149 u64 generation;
2150};
2151
2152struct old_sa_defrag_extent {
2153 struct list_head list;
2154 struct new_sa_defrag_extent *new;
2155
2156 u64 extent_offset;
2157 u64 bytenr;
2158 u64 offset;
2159 u64 len;
2160 int count;
2161};
2162
2163struct new_sa_defrag_extent {
2164 struct rb_root root;
2165 struct list_head head;
2166 struct btrfs_path *path;
2167 struct inode *inode;
2168 u64 file_pos;
2169 u64 len;
2170 u64 bytenr;
2171 u64 disk_len;
2172 u8 compress_type;
2173};
2174
2175static int backref_comp(struct sa_defrag_extent_backref *b1,
2176 struct sa_defrag_extent_backref *b2)
2177{
2178 if (b1->root_id < b2->root_id)
2179 return -1;
2180 else if (b1->root_id > b2->root_id)
2181 return 1;
2182
2183 if (b1->inum < b2->inum)
2184 return -1;
2185 else if (b1->inum > b2->inum)
2186 return 1;
2187
2188 if (b1->file_pos < b2->file_pos)
2189 return -1;
2190 else if (b1->file_pos > b2->file_pos)
2191 return 1;
2192
2193 /*
2194 * [------------------------------] ===> (a range of space)
2195 * |<--->| |<---->| =============> (fs/file tree A)
2196 * |<---------------------------->| ===> (fs/file tree B)
2197 *
2198 * A range of space can refer to two file extents in one tree while
2199 * refer to only one file extent in another tree.
2200 *
2201 * So we may process a disk offset more than one time(two extents in A)
2202 * and locate at the same extent(one extent in B), then insert two same
2203 * backrefs(both refer to the extent in B).
2204 */
2205 return 0;
2206}
2207
2208static void backref_insert(struct rb_root *root,
2209 struct sa_defrag_extent_backref *backref)
2210{
2211 struct rb_node **p = &root->rb_node;
2212 struct rb_node *parent = NULL;
2213 struct sa_defrag_extent_backref *entry;
2214 int ret;
2215
2216 while (*p) {
2217 parent = *p;
2218 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2219
2220 ret = backref_comp(backref, entry);
2221 if (ret < 0)
2222 p = &(*p)->rb_left;
2223 else
2224 p = &(*p)->rb_right;
2225 }
2226
2227 rb_link_node(&backref->node, parent, p);
2228 rb_insert_color(&backref->node, root);
2229}
2230
2231/*
2232 * Note the backref might has changed, and in this case we just return 0.
2233 */
2234static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2235 void *ctx)
2236{
2237 struct btrfs_file_extent_item *extent;
2238 struct btrfs_fs_info *fs_info;
2239 struct old_sa_defrag_extent *old = ctx;
2240 struct new_sa_defrag_extent *new = old->new;
2241 struct btrfs_path *path = new->path;
2242 struct btrfs_key key;
2243 struct btrfs_root *root;
2244 struct sa_defrag_extent_backref *backref;
2245 struct extent_buffer *leaf;
2246 struct inode *inode = new->inode;
2247 int slot;
2248 int ret;
2249 u64 extent_offset;
2250 u64 num_bytes;
2251
2252 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2253 inum == btrfs_ino(inode))
2254 return 0;
2255
2256 key.objectid = root_id;
2257 key.type = BTRFS_ROOT_ITEM_KEY;
2258 key.offset = (u64)-1;
2259
2260 fs_info = BTRFS_I(inode)->root->fs_info;
2261 root = btrfs_read_fs_root_no_name(fs_info, &key);
2262 if (IS_ERR(root)) {
2263 if (PTR_ERR(root) == -ENOENT)
2264 return 0;
2265 WARN_ON(1);
2266 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2267 inum, offset, root_id);
2268 return PTR_ERR(root);
2269 }
2270
2271 key.objectid = inum;
2272 key.type = BTRFS_EXTENT_DATA_KEY;
2273 if (offset > (u64)-1 << 32)
2274 key.offset = 0;
2275 else
2276 key.offset = offset;
2277
2278 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302279 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002280 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002281 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002282
2283 while (1) {
2284 cond_resched();
2285
2286 leaf = path->nodes[0];
2287 slot = path->slots[0];
2288
2289 if (slot >= btrfs_header_nritems(leaf)) {
2290 ret = btrfs_next_leaf(root, path);
2291 if (ret < 0) {
2292 goto out;
2293 } else if (ret > 0) {
2294 ret = 0;
2295 goto out;
2296 }
2297 continue;
2298 }
2299
2300 path->slots[0]++;
2301
2302 btrfs_item_key_to_cpu(leaf, &key, slot);
2303
2304 if (key.objectid > inum)
2305 goto out;
2306
2307 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2308 continue;
2309
2310 extent = btrfs_item_ptr(leaf, slot,
2311 struct btrfs_file_extent_item);
2312
2313 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2314 continue;
2315
Liu Boe68afa42013-07-01 22:13:26 +08002316 /*
2317 * 'offset' refers to the exact key.offset,
2318 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2319 * (key.offset - extent_offset).
2320 */
2321 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002322 continue;
2323
Liu Boe68afa42013-07-01 22:13:26 +08002324 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002325 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002326
Liu Bo38c227d2013-01-29 03:18:40 +00002327 if (extent_offset >= old->extent_offset + old->offset +
2328 old->len || extent_offset + num_bytes <=
2329 old->extent_offset + old->offset)
2330 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002331 break;
2332 }
2333
2334 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2335 if (!backref) {
2336 ret = -ENOENT;
2337 goto out;
2338 }
2339
2340 backref->root_id = root_id;
2341 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002342 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002343 backref->num_bytes = num_bytes;
2344 backref->extent_offset = extent_offset;
2345 backref->generation = btrfs_file_extent_generation(leaf, extent);
2346 backref->old = old;
2347 backref_insert(&new->root, backref);
2348 old->count++;
2349out:
2350 btrfs_release_path(path);
2351 WARN_ON(ret);
2352 return ret;
2353}
2354
2355static noinline bool record_extent_backrefs(struct btrfs_path *path,
2356 struct new_sa_defrag_extent *new)
2357{
2358 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2359 struct old_sa_defrag_extent *old, *tmp;
2360 int ret;
2361
2362 new->path = path;
2363
2364 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002365 ret = iterate_inodes_from_logical(old->bytenr +
2366 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002367 path, record_one_backref,
2368 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002369 if (ret < 0 && ret != -ENOENT)
2370 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002371
2372 /* no backref to be processed for this extent */
2373 if (!old->count) {
2374 list_del(&old->list);
2375 kfree(old);
2376 }
2377 }
2378
2379 if (list_empty(&new->head))
2380 return false;
2381
2382 return true;
2383}
2384
2385static int relink_is_mergable(struct extent_buffer *leaf,
2386 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002387 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002388{
Liu Bo116e0022013-08-02 16:30:40 +08002389 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002390 return 0;
2391
2392 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2393 return 0;
2394
Liu Bo116e0022013-08-02 16:30:40 +08002395 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2396 return 0;
2397
2398 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002399 btrfs_file_extent_other_encoding(leaf, fi))
2400 return 0;
2401
2402 return 1;
2403}
2404
2405/*
2406 * Note the backref might has changed, and in this case we just return 0.
2407 */
2408static noinline int relink_extent_backref(struct btrfs_path *path,
2409 struct sa_defrag_extent_backref *prev,
2410 struct sa_defrag_extent_backref *backref)
2411{
2412 struct btrfs_file_extent_item *extent;
2413 struct btrfs_file_extent_item *item;
2414 struct btrfs_ordered_extent *ordered;
2415 struct btrfs_trans_handle *trans;
2416 struct btrfs_fs_info *fs_info;
2417 struct btrfs_root *root;
2418 struct btrfs_key key;
2419 struct extent_buffer *leaf;
2420 struct old_sa_defrag_extent *old = backref->old;
2421 struct new_sa_defrag_extent *new = old->new;
2422 struct inode *src_inode = new->inode;
2423 struct inode *inode;
2424 struct extent_state *cached = NULL;
2425 int ret = 0;
2426 u64 start;
2427 u64 len;
2428 u64 lock_start;
2429 u64 lock_end;
2430 bool merge = false;
2431 int index;
2432
2433 if (prev && prev->root_id == backref->root_id &&
2434 prev->inum == backref->inum &&
2435 prev->file_pos + prev->num_bytes == backref->file_pos)
2436 merge = true;
2437
2438 /* step 1: get root */
2439 key.objectid = backref->root_id;
2440 key.type = BTRFS_ROOT_ITEM_KEY;
2441 key.offset = (u64)-1;
2442
2443 fs_info = BTRFS_I(src_inode)->root->fs_info;
2444 index = srcu_read_lock(&fs_info->subvol_srcu);
2445
2446 root = btrfs_read_fs_root_no_name(fs_info, &key);
2447 if (IS_ERR(root)) {
2448 srcu_read_unlock(&fs_info->subvol_srcu, index);
2449 if (PTR_ERR(root) == -ENOENT)
2450 return 0;
2451 return PTR_ERR(root);
2452 }
Liu Bo38c227d2013-01-29 03:18:40 +00002453
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002454 if (btrfs_root_readonly(root)) {
2455 srcu_read_unlock(&fs_info->subvol_srcu, index);
2456 return 0;
2457 }
2458
Liu Bo38c227d2013-01-29 03:18:40 +00002459 /* step 2: get inode */
2460 key.objectid = backref->inum;
2461 key.type = BTRFS_INODE_ITEM_KEY;
2462 key.offset = 0;
2463
2464 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2465 if (IS_ERR(inode)) {
2466 srcu_read_unlock(&fs_info->subvol_srcu, index);
2467 return 0;
2468 }
2469
2470 srcu_read_unlock(&fs_info->subvol_srcu, index);
2471
2472 /* step 3: relink backref */
2473 lock_start = backref->file_pos;
2474 lock_end = backref->file_pos + backref->num_bytes - 1;
2475 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2476 0, &cached);
2477
2478 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2479 if (ordered) {
2480 btrfs_put_ordered_extent(ordered);
2481 goto out_unlock;
2482 }
2483
2484 trans = btrfs_join_transaction(root);
2485 if (IS_ERR(trans)) {
2486 ret = PTR_ERR(trans);
2487 goto out_unlock;
2488 }
2489
2490 key.objectid = backref->inum;
2491 key.type = BTRFS_EXTENT_DATA_KEY;
2492 key.offset = backref->file_pos;
2493
2494 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2495 if (ret < 0) {
2496 goto out_free_path;
2497 } else if (ret > 0) {
2498 ret = 0;
2499 goto out_free_path;
2500 }
2501
2502 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2503 struct btrfs_file_extent_item);
2504
2505 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2506 backref->generation)
2507 goto out_free_path;
2508
2509 btrfs_release_path(path);
2510
2511 start = backref->file_pos;
2512 if (backref->extent_offset < old->extent_offset + old->offset)
2513 start += old->extent_offset + old->offset -
2514 backref->extent_offset;
2515
2516 len = min(backref->extent_offset + backref->num_bytes,
2517 old->extent_offset + old->offset + old->len);
2518 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2519
2520 ret = btrfs_drop_extents(trans, root, inode, start,
2521 start + len, 1);
2522 if (ret)
2523 goto out_free_path;
2524again:
2525 key.objectid = btrfs_ino(inode);
2526 key.type = BTRFS_EXTENT_DATA_KEY;
2527 key.offset = start;
2528
Liu Boa09a0a72013-03-11 09:20:58 +00002529 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002530 if (merge) {
2531 struct btrfs_file_extent_item *fi;
2532 u64 extent_len;
2533 struct btrfs_key found_key;
2534
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002535 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002536 if (ret < 0)
2537 goto out_free_path;
2538
2539 path->slots[0]--;
2540 leaf = path->nodes[0];
2541 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2542
2543 fi = btrfs_item_ptr(leaf, path->slots[0],
2544 struct btrfs_file_extent_item);
2545 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2546
Liu Bo116e0022013-08-02 16:30:40 +08002547 if (extent_len + found_key.offset == start &&
2548 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002549 btrfs_set_file_extent_num_bytes(leaf, fi,
2550 extent_len + len);
2551 btrfs_mark_buffer_dirty(leaf);
2552 inode_add_bytes(inode, len);
2553
2554 ret = 1;
2555 goto out_free_path;
2556 } else {
2557 merge = false;
2558 btrfs_release_path(path);
2559 goto again;
2560 }
2561 }
2562
2563 ret = btrfs_insert_empty_item(trans, root, path, &key,
2564 sizeof(*extent));
2565 if (ret) {
2566 btrfs_abort_transaction(trans, root, ret);
2567 goto out_free_path;
2568 }
2569
2570 leaf = path->nodes[0];
2571 item = btrfs_item_ptr(leaf, path->slots[0],
2572 struct btrfs_file_extent_item);
2573 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2574 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2575 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2576 btrfs_set_file_extent_num_bytes(leaf, item, len);
2577 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2578 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2579 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2580 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2581 btrfs_set_file_extent_encryption(leaf, item, 0);
2582 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2583
2584 btrfs_mark_buffer_dirty(leaf);
2585 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002586 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002587
2588 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2589 new->disk_len, 0,
2590 backref->root_id, backref->inum,
2591 new->file_pos, 0); /* start - extent_offset */
2592 if (ret) {
2593 btrfs_abort_transaction(trans, root, ret);
2594 goto out_free_path;
2595 }
2596
2597 ret = 1;
2598out_free_path:
2599 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002600 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002601 btrfs_end_transaction(trans, root);
2602out_unlock:
2603 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2604 &cached, GFP_NOFS);
2605 iput(inode);
2606 return ret;
2607}
2608
Liu Bo6f519562013-10-29 10:45:05 +08002609static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2610{
2611 struct old_sa_defrag_extent *old, *tmp;
2612
2613 if (!new)
2614 return;
2615
2616 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002617 kfree(old);
2618 }
2619 kfree(new);
2620}
2621
Liu Bo38c227d2013-01-29 03:18:40 +00002622static void relink_file_extents(struct new_sa_defrag_extent *new)
2623{
2624 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002625 struct sa_defrag_extent_backref *backref;
2626 struct sa_defrag_extent_backref *prev = NULL;
2627 struct inode *inode;
2628 struct btrfs_root *root;
2629 struct rb_node *node;
2630 int ret;
2631
2632 inode = new->inode;
2633 root = BTRFS_I(inode)->root;
2634
2635 path = btrfs_alloc_path();
2636 if (!path)
2637 return;
2638
2639 if (!record_extent_backrefs(path, new)) {
2640 btrfs_free_path(path);
2641 goto out;
2642 }
2643 btrfs_release_path(path);
2644
2645 while (1) {
2646 node = rb_first(&new->root);
2647 if (!node)
2648 break;
2649 rb_erase(node, &new->root);
2650
2651 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2652
2653 ret = relink_extent_backref(path, prev, backref);
2654 WARN_ON(ret < 0);
2655
2656 kfree(prev);
2657
2658 if (ret == 1)
2659 prev = backref;
2660 else
2661 prev = NULL;
2662 cond_resched();
2663 }
2664 kfree(prev);
2665
2666 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002667out:
Liu Bo6f519562013-10-29 10:45:05 +08002668 free_sa_defrag_extent(new);
2669
Liu Bo38c227d2013-01-29 03:18:40 +00002670 atomic_dec(&root->fs_info->defrag_running);
2671 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002672}
2673
2674static struct new_sa_defrag_extent *
2675record_old_file_extents(struct inode *inode,
2676 struct btrfs_ordered_extent *ordered)
2677{
2678 struct btrfs_root *root = BTRFS_I(inode)->root;
2679 struct btrfs_path *path;
2680 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002681 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002682 struct new_sa_defrag_extent *new;
2683 int ret;
2684
2685 new = kmalloc(sizeof(*new), GFP_NOFS);
2686 if (!new)
2687 return NULL;
2688
2689 new->inode = inode;
2690 new->file_pos = ordered->file_offset;
2691 new->len = ordered->len;
2692 new->bytenr = ordered->start;
2693 new->disk_len = ordered->disk_len;
2694 new->compress_type = ordered->compress_type;
2695 new->root = RB_ROOT;
2696 INIT_LIST_HEAD(&new->head);
2697
2698 path = btrfs_alloc_path();
2699 if (!path)
2700 goto out_kfree;
2701
2702 key.objectid = btrfs_ino(inode);
2703 key.type = BTRFS_EXTENT_DATA_KEY;
2704 key.offset = new->file_pos;
2705
2706 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2707 if (ret < 0)
2708 goto out_free_path;
2709 if (ret > 0 && path->slots[0] > 0)
2710 path->slots[0]--;
2711
2712 /* find out all the old extents for the file range */
2713 while (1) {
2714 struct btrfs_file_extent_item *extent;
2715 struct extent_buffer *l;
2716 int slot;
2717 u64 num_bytes;
2718 u64 offset;
2719 u64 end;
2720 u64 disk_bytenr;
2721 u64 extent_offset;
2722
2723 l = path->nodes[0];
2724 slot = path->slots[0];
2725
2726 if (slot >= btrfs_header_nritems(l)) {
2727 ret = btrfs_next_leaf(root, path);
2728 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002729 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002730 else if (ret > 0)
2731 break;
2732 continue;
2733 }
2734
2735 btrfs_item_key_to_cpu(l, &key, slot);
2736
2737 if (key.objectid != btrfs_ino(inode))
2738 break;
2739 if (key.type != BTRFS_EXTENT_DATA_KEY)
2740 break;
2741 if (key.offset >= new->file_pos + new->len)
2742 break;
2743
2744 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2745
2746 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2747 if (key.offset + num_bytes < new->file_pos)
2748 goto next;
2749
2750 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2751 if (!disk_bytenr)
2752 goto next;
2753
2754 extent_offset = btrfs_file_extent_offset(l, extent);
2755
2756 old = kmalloc(sizeof(*old), GFP_NOFS);
2757 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002758 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002759
2760 offset = max(new->file_pos, key.offset);
2761 end = min(new->file_pos + new->len, key.offset + num_bytes);
2762
2763 old->bytenr = disk_bytenr;
2764 old->extent_offset = extent_offset;
2765 old->offset = offset - key.offset;
2766 old->len = end - offset;
2767 old->new = new;
2768 old->count = 0;
2769 list_add_tail(&old->list, &new->head);
2770next:
2771 path->slots[0]++;
2772 cond_resched();
2773 }
2774
2775 btrfs_free_path(path);
2776 atomic_inc(&root->fs_info->defrag_running);
2777
2778 return new;
2779
Liu Bo38c227d2013-01-29 03:18:40 +00002780out_free_path:
2781 btrfs_free_path(path);
2782out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002783 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002784 return NULL;
2785}
2786
Miao Xiee570fd22014-06-19 10:42:50 +08002787static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2788 u64 start, u64 len)
2789{
2790 struct btrfs_block_group_cache *cache;
2791
2792 cache = btrfs_lookup_block_group(root->fs_info, start);
2793 ASSERT(cache);
2794
2795 spin_lock(&cache->lock);
2796 cache->delalloc_bytes -= len;
2797 spin_unlock(&cache->lock);
2798
2799 btrfs_put_block_group(cache);
2800}
2801
Chris Masond352ac62008-09-29 15:18:18 -04002802/* as ordered data IO finishes, this gets called so we can finish
2803 * an ordered extent if the range of bytes in the file it covers are
2804 * fully written.
2805 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002806static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002807{
Josef Bacik5fd02042012-05-02 14:00:54 -04002808 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002809 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002810 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002811 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002812 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002813 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002814 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002815 int ret = 0;
2816 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002817 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002818 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002819
Liu Bo83eea1f2012-07-10 05:28:39 -06002820 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002821
Josef Bacik5fd02042012-05-02 14:00:54 -04002822 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2823 ret = -EIO;
2824 goto out;
2825 }
2826
Miao Xief6124962014-09-12 18:44:04 +08002827 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2828 ordered_extent->file_offset +
2829 ordered_extent->len - 1);
2830
Josef Bacik77cef2e2013-08-29 13:57:21 -04002831 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2832 truncated = true;
2833 logical_len = ordered_extent->truncated_len;
2834 /* Truncated the entire extent, don't bother adding */
2835 if (!logical_len)
2836 goto out;
2837 }
2838
Yan, Zhengc2167752009-11-12 09:34:21 +00002839 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002840 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002841 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2842 if (nolock)
2843 trans = btrfs_join_transaction_nolock(root);
2844 else
2845 trans = btrfs_join_transaction(root);
2846 if (IS_ERR(trans)) {
2847 ret = PTR_ERR(trans);
2848 trans = NULL;
2849 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002850 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002851 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2852 ret = btrfs_update_inode_fallback(trans, root, inode);
2853 if (ret) /* -ENOMEM or corruption */
2854 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002855 goto out;
2856 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002857
Josef Bacik2ac55d42010-02-03 19:33:23 +00002858 lock_extent_bits(io_tree, ordered_extent->file_offset,
2859 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002860 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002861
Liu Bo38c227d2013-01-29 03:18:40 +00002862 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2863 ordered_extent->file_offset + ordered_extent->len - 1,
2864 EXTENT_DEFRAG, 1, cached_state);
2865 if (ret) {
2866 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002867 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002868 /* the inode is shared */
2869 new = record_old_file_extents(inode, ordered_extent);
2870
2871 clear_extent_bit(io_tree, ordered_extent->file_offset,
2872 ordered_extent->file_offset + ordered_extent->len - 1,
2873 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2874 }
2875
Josef Bacik0cb59c92010-07-02 12:14:14 -04002876 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002877 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002878 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002879 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002880 if (IS_ERR(trans)) {
2881 ret = PTR_ERR(trans);
2882 trans = NULL;
2883 goto out_unlock;
2884 }
Chris Masona79b7d42014-05-22 16:18:52 -07002885
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002886 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002887
Chris Masonc8b97812008-10-29 14:49:59 -04002888 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002889 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002890 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002891 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002892 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002893 ordered_extent->file_offset,
2894 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002895 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002896 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002897 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002898 ret = insert_reserved_file_extent(trans, inode,
2899 ordered_extent->file_offset,
2900 ordered_extent->start,
2901 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002902 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002903 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002904 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002905 if (!ret)
2906 btrfs_release_delalloc_bytes(root,
2907 ordered_extent->start,
2908 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002909 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002910 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2911 ordered_extent->file_offset, ordered_extent->len,
2912 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002913 if (ret < 0) {
2914 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002915 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002916 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002917
Chris Masone6dcd2d2008-07-17 12:53:50 -04002918 add_pending_csums(trans, inode, ordered_extent->file_offset,
2919 &ordered_extent->list);
2920
Josef Bacik6c760c02012-11-09 10:53:21 -05002921 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2922 ret = btrfs_update_inode_fallback(trans, root, inode);
2923 if (ret) { /* -ENOMEM or corruption */
2924 btrfs_abort_transaction(trans, root, ret);
2925 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002926 }
2927 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002928out_unlock:
2929 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2930 ordered_extent->file_offset +
2931 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002932out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002933 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002934 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002935 if (trans)
2936 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002937
Josef Bacik77cef2e2013-08-29 13:57:21 -04002938 if (ret || truncated) {
2939 u64 start, end;
2940
2941 if (truncated)
2942 start = ordered_extent->file_offset + logical_len;
2943 else
2944 start = ordered_extent->file_offset;
2945 end = ordered_extent->file_offset + ordered_extent->len - 1;
2946 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2947
2948 /* Drop the cache for the part of the extent we didn't write. */
2949 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002950
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002951 /*
2952 * If the ordered extent had an IOERR or something else went
2953 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002954 * back to the allocator. We only free the extent in the
2955 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002956 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002957 if ((ret || !logical_len) &&
2958 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002959 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2960 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002961 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002962 }
2963
2964
Josef Bacik5fd02042012-05-02 14:00:54 -04002965 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002966 * This needs to be done to make sure anybody waiting knows we are done
2967 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002968 */
2969 btrfs_remove_ordered_extent(inode, ordered_extent);
2970
Liu Bo38c227d2013-01-29 03:18:40 +00002971 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002972 if (new) {
2973 if (ret) {
2974 free_sa_defrag_extent(new);
2975 atomic_dec(&root->fs_info->defrag_running);
2976 } else {
2977 relink_file_extents(new);
2978 }
2979 }
Liu Bo38c227d2013-01-29 03:18:40 +00002980
Chris Masone6dcd2d2008-07-17 12:53:50 -04002981 /* once for us */
2982 btrfs_put_ordered_extent(ordered_extent);
2983 /* once for the tree */
2984 btrfs_put_ordered_extent(ordered_extent);
2985
Josef Bacik5fd02042012-05-02 14:00:54 -04002986 return ret;
2987}
2988
2989static void finish_ordered_fn(struct btrfs_work *work)
2990{
2991 struct btrfs_ordered_extent *ordered_extent;
2992 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2993 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002994}
2995
Christoph Hellwigb2950862008-12-02 09:54:17 -05002996static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002997 struct extent_state *state, int uptodate)
2998{
Josef Bacik5fd02042012-05-02 14:00:54 -04002999 struct inode *inode = page->mapping->host;
3000 struct btrfs_root *root = BTRFS_I(inode)->root;
3001 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003002 struct btrfs_workqueue *wq;
3003 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003004
liubo1abe9b82011-03-24 11:18:59 +00003005 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3006
Chris Mason8b62b722009-09-02 16:53:46 -04003007 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003008 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3009 end - start + 1, uptodate))
3010 return 0;
3011
Liu Bo9e0af232014-08-15 23:36:53 +08003012 if (btrfs_is_free_space_inode(inode)) {
3013 wq = root->fs_info->endio_freespace_worker;
3014 func = btrfs_freespace_write_helper;
3015 } else {
3016 wq = root->fs_info->endio_write_workers;
3017 func = btrfs_endio_write_helper;
3018 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003019
Liu Bo9e0af232014-08-15 23:36:53 +08003020 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3021 NULL);
3022 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003023
3024 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003025}
3026
Miao Xiedc380ae2014-09-12 18:43:55 +08003027static int __readpage_endio_check(struct inode *inode,
3028 struct btrfs_io_bio *io_bio,
3029 int icsum, struct page *page,
3030 int pgoff, u64 start, size_t len)
3031{
3032 char *kaddr;
3033 u32 csum_expected;
3034 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003035
3036 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3037
3038 kaddr = kmap_atomic(page);
3039 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3040 btrfs_csum_final(csum, (char *)&csum);
3041 if (csum != csum_expected)
3042 goto zeroit;
3043
3044 kunmap_atomic(kaddr);
3045 return 0;
3046zeroit:
David Sterba94647322015-10-08 11:01:36 +02003047 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3048 "csum failed ino %llu off %llu csum %u expected csum %u",
Miao Xiedc380ae2014-09-12 18:43:55 +08003049 btrfs_ino(inode), start, csum, csum_expected);
3050 memset(kaddr + pgoff, 1, len);
3051 flush_dcache_page(page);
3052 kunmap_atomic(kaddr);
3053 if (csum_expected == 0)
3054 return 0;
3055 return -EIO;
3056}
3057
Chris Masond352ac62008-09-29 15:18:18 -04003058/*
Chris Masond352ac62008-09-29 15:18:18 -04003059 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003060 * if there's a match, we allow the bio to finish. If not, the code in
3061 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003062 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003063static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3064 u64 phy_offset, struct page *page,
3065 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003066{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003067 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003068 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003069 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003070 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003071
Chris Masond20f7042008-12-08 16:58:54 -05003072 if (PageChecked(page)) {
3073 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003074 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003075 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003076
3077 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003078 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003079
Yan Zheng17d217f2008-12-12 10:03:38 -05003080 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003081 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05003082 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
3083 GFP_NOFS);
3084 return 0;
3085 }
3086
Miao Xiefacc8a222013-07-25 19:22:34 +08003087 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003088 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3089 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003090}
Chris Masonb888db22007-08-27 16:49:44 -04003091
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003092struct delayed_iput {
3093 struct list_head list;
3094 struct inode *inode;
3095};
3096
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003097/* JDM: If this is fs-wide, why can't we add a pointer to
3098 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003099void btrfs_add_delayed_iput(struct inode *inode)
3100{
3101 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3102 struct delayed_iput *delayed;
3103
3104 if (atomic_add_unless(&inode->i_count, -1, 1))
3105 return;
3106
3107 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3108 delayed->inode = inode;
3109
3110 spin_lock(&fs_info->delayed_iput_lock);
3111 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3112 spin_unlock(&fs_info->delayed_iput_lock);
3113}
3114
3115void btrfs_run_delayed_iputs(struct btrfs_root *root)
3116{
3117 LIST_HEAD(list);
3118 struct btrfs_fs_info *fs_info = root->fs_info;
3119 struct delayed_iput *delayed;
3120 int empty;
3121
3122 spin_lock(&fs_info->delayed_iput_lock);
3123 empty = list_empty(&fs_info->delayed_iputs);
3124 spin_unlock(&fs_info->delayed_iput_lock);
3125 if (empty)
3126 return;
3127
Zhao Leid7c15172015-02-26 10:49:20 +08003128 down_read(&fs_info->delayed_iput_sem);
3129
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003130 spin_lock(&fs_info->delayed_iput_lock);
3131 list_splice_init(&fs_info->delayed_iputs, &list);
3132 spin_unlock(&fs_info->delayed_iput_lock);
3133
3134 while (!list_empty(&list)) {
3135 delayed = list_entry(list.next, struct delayed_iput, list);
3136 list_del(&delayed->list);
3137 iput(delayed->inode);
3138 kfree(delayed);
3139 }
Zhao Leid7c15172015-02-26 10:49:20 +08003140
3141 up_read(&root->fs_info->delayed_iput_sem);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003142}
3143
Yan, Zhengd68fc572010-05-16 10:49:58 -04003144/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003145 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003146 * files in the subvolume, it removes orphan item and frees block_rsv
3147 * structure.
3148 */
3149void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3150 struct btrfs_root *root)
3151{
Josef Bacik90290e12011-12-02 15:44:12 -05003152 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003153 int ret;
3154
Josef Bacik8a35d952012-05-23 14:26:42 -04003155 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003156 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3157 return;
3158
Josef Bacik90290e12011-12-02 15:44:12 -05003159 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003160 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003161 spin_unlock(&root->orphan_lock);
3162 return;
3163 }
3164
3165 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3166 spin_unlock(&root->orphan_lock);
3167 return;
3168 }
3169
3170 block_rsv = root->orphan_block_rsv;
3171 root->orphan_block_rsv = NULL;
3172 spin_unlock(&root->orphan_lock);
3173
Miao Xie27cdeb72014-04-02 19:51:05 +08003174 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003175 btrfs_root_refs(&root->root_item) > 0) {
3176 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3177 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003178 if (ret)
3179 btrfs_abort_transaction(trans, root, ret);
3180 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003181 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3182 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003183 }
3184
Josef Bacik90290e12011-12-02 15:44:12 -05003185 if (block_rsv) {
3186 WARN_ON(block_rsv->size > 0);
3187 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003188 }
3189}
3190
3191/*
Josef Bacik7b128762008-07-24 12:17:14 -04003192 * This creates an orphan entry for the given inode in case something goes
3193 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003194 *
3195 * NOTE: caller of this function should reserve 5 units of metadata for
3196 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003197 */
3198int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3199{
3200 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003201 struct btrfs_block_rsv *block_rsv = NULL;
3202 int reserve = 0;
3203 int insert = 0;
3204 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003205
Yan, Zhengd68fc572010-05-16 10:49:58 -04003206 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003207 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003208 if (!block_rsv)
3209 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003210 }
3211
Yan, Zhengd68fc572010-05-16 10:49:58 -04003212 spin_lock(&root->orphan_lock);
3213 if (!root->orphan_block_rsv) {
3214 root->orphan_block_rsv = block_rsv;
3215 } else if (block_rsv) {
3216 btrfs_free_block_rsv(root, block_rsv);
3217 block_rsv = NULL;
3218 }
Josef Bacik7b128762008-07-24 12:17:14 -04003219
Josef Bacik8a35d952012-05-23 14:26:42 -04003220 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3221 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003222#if 0
3223 /*
3224 * For proper ENOSPC handling, we should do orphan
3225 * cleanup when mounting. But this introduces backward
3226 * compatibility issue.
3227 */
3228 if (!xchg(&root->orphan_item_inserted, 1))
3229 insert = 2;
3230 else
3231 insert = 1;
3232#endif
3233 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003234 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003235 }
Josef Bacik7b128762008-07-24 12:17:14 -04003236
Josef Bacik72ac3c02012-05-23 14:13:11 -04003237 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3238 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003239 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003240 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003241
Yan, Zhengd68fc572010-05-16 10:49:58 -04003242 /* grab metadata reservation from transaction handle */
3243 if (reserve) {
3244 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003245 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003246 }
3247
3248 /* insert an orphan item to track this unlinked/truncated file */
3249 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003250 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003251 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003252 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003253 if (reserve) {
3254 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3255 &BTRFS_I(inode)->runtime_flags);
3256 btrfs_orphan_release_metadata(inode);
3257 }
3258 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003259 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3260 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003261 btrfs_abort_transaction(trans, root, ret);
3262 return ret;
3263 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003264 }
3265 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003266 }
3267
3268 /* insert an orphan item to track subvolume contains orphan files */
3269 if (insert >= 2) {
3270 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3271 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003272 if (ret && ret != -EEXIST) {
3273 btrfs_abort_transaction(trans, root, ret);
3274 return ret;
3275 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003276 }
3277 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003278}
3279
3280/*
3281 * We have done the truncate/delete so we can go ahead and remove the orphan
3282 * item for this particular inode.
3283 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003284static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3285 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003286{
3287 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003288 int delete_item = 0;
3289 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003290 int ret = 0;
3291
Yan, Zhengd68fc572010-05-16 10:49:58 -04003292 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003293 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3294 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003295 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003296
Josef Bacik72ac3c02012-05-23 14:13:11 -04003297 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3298 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003299 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003300 spin_unlock(&root->orphan_lock);
3301
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003302 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003303 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003304 if (trans)
3305 ret = btrfs_del_orphan_item(trans, root,
3306 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003307 }
Josef Bacik7b128762008-07-24 12:17:14 -04003308
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003309 if (release_rsv)
3310 btrfs_orphan_release_metadata(inode);
3311
Josef Bacik4ef31a42013-08-13 14:10:08 -04003312 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003313}
3314
3315/*
3316 * this cleans up any orphans that may be left on the list from the last use
3317 * of this root.
3318 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003319int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003320{
3321 struct btrfs_path *path;
3322 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003323 struct btrfs_key key, found_key;
3324 struct btrfs_trans_handle *trans;
3325 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003326 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003327 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3328
Yan, Zhengd68fc572010-05-16 10:49:58 -04003329 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003330 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003331
3332 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003333 if (!path) {
3334 ret = -ENOMEM;
3335 goto out;
3336 }
Josef Bacik7b128762008-07-24 12:17:14 -04003337 path->reada = -1;
3338
3339 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003340 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003341 key.offset = (u64)-1;
3342
Josef Bacik7b128762008-07-24 12:17:14 -04003343 while (1) {
3344 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003345 if (ret < 0)
3346 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003347
3348 /*
3349 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003350 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003351 * find the key and see if we have stuff that matches
3352 */
3353 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003354 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003355 if (path->slots[0] == 0)
3356 break;
3357 path->slots[0]--;
3358 }
3359
3360 /* pull out the item */
3361 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003362 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3363
3364 /* make sure the item matches what we want */
3365 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3366 break;
David Sterba962a2982014-06-04 18:41:45 +02003367 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003368 break;
3369
3370 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003371 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003372
3373 /*
3374 * this is where we are basically btrfs_lookup, without the
3375 * crossing root thing. we store the inode number in the
3376 * offset of the orphan item.
3377 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003378
3379 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003380 btrfs_err(root->fs_info,
3381 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003382 ret = -EINVAL;
3383 goto out;
3384 }
3385
3386 last_objectid = found_key.offset;
3387
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003388 found_key.objectid = found_key.offset;
3389 found_key.type = BTRFS_INODE_ITEM_KEY;
3390 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003391 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303392 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003393 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003394 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003395
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003396 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3397 struct btrfs_root *dead_root;
3398 struct btrfs_fs_info *fs_info = root->fs_info;
3399 int is_dead_root = 0;
3400
3401 /*
3402 * this is an orphan in the tree root. Currently these
3403 * could come from 2 sources:
3404 * a) a snapshot deletion in progress
3405 * b) a free space cache inode
3406 * We need to distinguish those two, as the snapshot
3407 * orphan must not get deleted.
3408 * find_dead_roots already ran before us, so if this
3409 * is a snapshot deletion, we should find the root
3410 * in the dead_roots list
3411 */
3412 spin_lock(&fs_info->trans_lock);
3413 list_for_each_entry(dead_root, &fs_info->dead_roots,
3414 root_list) {
3415 if (dead_root->root_key.objectid ==
3416 found_key.objectid) {
3417 is_dead_root = 1;
3418 break;
3419 }
3420 }
3421 spin_unlock(&fs_info->trans_lock);
3422 if (is_dead_root) {
3423 /* prevent this orphan from being found again */
3424 key.offset = found_key.objectid - 1;
3425 continue;
3426 }
3427 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003428 /*
3429 * Inode is already gone but the orphan item is still there,
3430 * kill the orphan item.
3431 */
3432 if (ret == -ESTALE) {
3433 trans = btrfs_start_transaction(root, 1);
3434 if (IS_ERR(trans)) {
3435 ret = PTR_ERR(trans);
3436 goto out;
3437 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003438 btrfs_debug(root->fs_info, "auto deleting %Lu",
3439 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003440 ret = btrfs_del_orphan_item(trans, root,
3441 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003442 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003443 if (ret)
3444 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003445 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003446 }
Josef Bacik7b128762008-07-24 12:17:14 -04003447
Josef Bacik7b128762008-07-24 12:17:14 -04003448 /*
3449 * add this inode to the orphan list so btrfs_orphan_del does
3450 * the proper thing when we hit it
3451 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003452 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3453 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003454 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003455
Josef Bacik7b128762008-07-24 12:17:14 -04003456 /* if we have links, this was a truncate, lets do that */
3457 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303458 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003459 iput(inode);
3460 continue;
3461 }
Josef Bacik7b128762008-07-24 12:17:14 -04003462 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003463
3464 /* 1 for the orphan item deletion. */
3465 trans = btrfs_start_transaction(root, 1);
3466 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003467 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003468 ret = PTR_ERR(trans);
3469 goto out;
3470 }
3471 ret = btrfs_orphan_add(trans, inode);
3472 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003473 if (ret) {
3474 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003475 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003476 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003477
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003478 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003479 if (ret)
3480 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003481 } else {
3482 nr_unlink++;
3483 }
3484
3485 /* this will do delete_inode and everything for us */
3486 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003487 if (ret)
3488 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003489 }
Miao Xie3254c872011-11-10 20:45:05 -05003490 /* release the path since we're done with it */
3491 btrfs_release_path(path);
3492
Yan, Zhengd68fc572010-05-16 10:49:58 -04003493 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3494
3495 if (root->orphan_block_rsv)
3496 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3497 (u64)-1);
3498
Miao Xie27cdeb72014-04-02 19:51:05 +08003499 if (root->orphan_block_rsv ||
3500 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003501 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003502 if (!IS_ERR(trans))
3503 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003504 }
Josef Bacik7b128762008-07-24 12:17:14 -04003505
3506 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003507 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003508 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003509 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003510
3511out:
3512 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003513 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003514 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003515 btrfs_free_path(path);
3516 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003517}
3518
Chris Masond352ac62008-09-29 15:18:18 -04003519/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003520 * very simple check to peek ahead in the leaf looking for xattrs. If we
3521 * don't find any xattrs, we know there can't be any acls.
3522 *
3523 * slot is the slot the inode is in, objectid is the objectid of the inode
3524 */
3525static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003526 int slot, u64 objectid,
3527 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003528{
3529 u32 nritems = btrfs_header_nritems(leaf);
3530 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003531 static u64 xattr_access = 0;
3532 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003533 int scanned = 0;
3534
Josef Bacikf23b5a52013-06-19 10:16:26 -04003535 if (!xattr_access) {
3536 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3537 strlen(POSIX_ACL_XATTR_ACCESS));
3538 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3539 strlen(POSIX_ACL_XATTR_DEFAULT));
3540 }
3541
Chris Mason46a53cc2009-04-27 11:47:50 -04003542 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003543 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003544 while (slot < nritems) {
3545 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3546
3547 /* we found a different objectid, there must not be acls */
3548 if (found_key.objectid != objectid)
3549 return 0;
3550
3551 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003552 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003553 if (*first_xattr_slot == -1)
3554 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003555 if (found_key.offset == xattr_access ||
3556 found_key.offset == xattr_default)
3557 return 1;
3558 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003559
3560 /*
3561 * we found a key greater than an xattr key, there can't
3562 * be any acls later on
3563 */
3564 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3565 return 0;
3566
3567 slot++;
3568 scanned++;
3569
3570 /*
3571 * it goes inode, inode backrefs, xattrs, extents,
3572 * so if there are a ton of hard links to an inode there can
3573 * be a lot of backrefs. Don't waste time searching too hard,
3574 * this is just an optimization
3575 */
3576 if (scanned >= 8)
3577 break;
3578 }
3579 /* we hit the end of the leaf before we found an xattr or
3580 * something larger than an xattr. We have to assume the inode
3581 * has acls
3582 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003583 if (*first_xattr_slot == -1)
3584 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003585 return 1;
3586}
3587
3588/*
Chris Masond352ac62008-09-29 15:18:18 -04003589 * read an inode from the btree into the in-memory inode
3590 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003591static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003592{
3593 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003594 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003595 struct btrfs_inode_item *inode_item;
3596 struct btrfs_root *root = BTRFS_I(inode)->root;
3597 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003598 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003599 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003600 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003601 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003602 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003603 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003604
3605 ret = btrfs_fill_inode(inode, &rdev);
3606 if (!ret)
3607 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003608
3609 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003610 if (!path)
3611 goto make_bad;
3612
Chris Mason39279cc2007-06-12 06:35:45 -04003613 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003614
Chris Mason39279cc2007-06-12 06:35:45 -04003615 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003616 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003617 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003618
Chris Mason5f39d392007-10-15 16:14:19 -04003619 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003620
3621 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003622 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003623
Chris Mason5f39d392007-10-15 16:14:19 -04003624 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3625 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003626 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003627 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003628 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3629 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003630 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003631
David Sterbaa937b972014-12-12 17:39:12 +01003632 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3633 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003634
David Sterbaa937b972014-12-12 17:39:12 +01003635 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3636 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003637
David Sterbaa937b972014-12-12 17:39:12 +01003638 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3639 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003640
chandan r9cc97d62012-07-04 12:48:07 +05303641 BTRFS_I(inode)->i_otime.tv_sec =
3642 btrfs_timespec_sec(leaf, &inode_item->otime);
3643 BTRFS_I(inode)->i_otime.tv_nsec =
3644 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003645
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003646 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003647 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003648 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3649
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003650 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003651 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003652 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003653 rdev = btrfs_inode_rdev(leaf, inode_item);
3654
Josef Bacikaec74772008-07-24 12:12:38 -04003655 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003656 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003657
3658cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003659 /*
3660 * If we were modified in the current generation and evicted from memory
3661 * and then re-read we need to do a full sync since we don't have any
3662 * idea about which extents were modified before we were evicted from
3663 * cache.
3664 *
3665 * This is required for both inode re-read from disk and delayed inode
3666 * in delayed_nodes_tree.
3667 */
3668 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3669 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3670 &BTRFS_I(inode)->runtime_flags);
3671
Filipe Mananabde6c242015-07-24 00:00:19 +01003672 /*
3673 * We don't persist the id of the transaction where an unlink operation
3674 * against the inode was last made. So here we assume the inode might
3675 * have been evicted, and therefore the exact value of last_unlink_trans
3676 * lost, and set it to last_trans to avoid metadata inconsistencies
3677 * between the inode and its parent if the inode is fsync'ed and the log
3678 * replayed. For example, in the scenario:
3679 *
3680 * touch mydir/foo
3681 * ln mydir/foo mydir/bar
3682 * sync
3683 * unlink mydir/bar
3684 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3685 * xfs_io -c fsync mydir/foo
3686 * <power failure>
3687 * mount fs, triggers fsync log replay
3688 *
3689 * We must make sure that when we fsync our inode foo we also log its
3690 * parent inode, otherwise after log replay the parent still has the
3691 * dentry with the "bar" name but our inode foo has a link count of 1
3692 * and doesn't have an inode ref with the name "bar" anymore.
3693 *
3694 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3695 * but it guarantees correctness at the expense of ocassional full
3696 * transaction commits on fsync if our inode is a directory, or if our
3697 * inode is not a directory, logging its parent unnecessarily.
3698 */
3699 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3700
Miao Xie67de1172013-12-26 13:07:06 +08003701 path->slots[0]++;
3702 if (inode->i_nlink != 1 ||
3703 path->slots[0] >= btrfs_header_nritems(leaf))
3704 goto cache_acl;
3705
3706 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3707 if (location.objectid != btrfs_ino(inode))
3708 goto cache_acl;
3709
3710 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3711 if (location.type == BTRFS_INODE_REF_KEY) {
3712 struct btrfs_inode_ref *ref;
3713
3714 ref = (struct btrfs_inode_ref *)ptr;
3715 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3716 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3717 struct btrfs_inode_extref *extref;
3718
3719 extref = (struct btrfs_inode_extref *)ptr;
3720 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3721 extref);
3722 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003723cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003724 /*
3725 * try to precache a NULL acl entry for files that don't have
3726 * any xattrs or acls
3727 */
Li Zefan33345d012011-04-20 10:31:50 +08003728 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003729 btrfs_ino(inode), &first_xattr_slot);
3730 if (first_xattr_slot != -1) {
3731 path->slots[0] = first_xattr_slot;
3732 ret = btrfs_load_inode_props(inode, path);
3733 if (ret)
3734 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003735 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003736 btrfs_ino(inode),
3737 root->root_key.objectid, ret);
3738 }
3739 btrfs_free_path(path);
3740
Al Viro72c04902009-06-24 16:58:48 -04003741 if (!maybe_acls)
3742 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003743
Chris Mason39279cc2007-06-12 06:35:45 -04003744 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003745 case S_IFREG:
3746 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003747 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003748 inode->i_fop = &btrfs_file_operations;
3749 inode->i_op = &btrfs_file_inode_operations;
3750 break;
3751 case S_IFDIR:
3752 inode->i_fop = &btrfs_dir_file_operations;
3753 if (root == root->fs_info->tree_root)
3754 inode->i_op = &btrfs_dir_ro_inode_operations;
3755 else
3756 inode->i_op = &btrfs_dir_inode_operations;
3757 break;
3758 case S_IFLNK:
3759 inode->i_op = &btrfs_symlink_inode_operations;
3760 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3761 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003762 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003763 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003764 init_special_inode(inode, inode->i_mode, rdev);
3765 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003766 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003767
3768 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003769 return;
3770
3771make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003772 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003773 make_bad_inode(inode);
3774}
3775
Chris Masond352ac62008-09-29 15:18:18 -04003776/*
3777 * given a leaf and an inode, copy the inode fields into the leaf
3778 */
Chris Masone02119d2008-09-05 16:13:11 -04003779static void fill_inode_item(struct btrfs_trans_handle *trans,
3780 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003781 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003782 struct inode *inode)
3783{
Liu Bo51fab692012-12-27 09:01:21 +00003784 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003785
Liu Bo51fab692012-12-27 09:01:21 +00003786 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003787
Liu Bo51fab692012-12-27 09:01:21 +00003788 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3789 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3790 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3791 &token);
3792 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3793 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003794
David Sterbaa937b972014-12-12 17:39:12 +01003795 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003796 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003797 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003798 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003799
David Sterbaa937b972014-12-12 17:39:12 +01003800 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003801 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003802 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003803 inode->i_mtime.tv_nsec, &token);
3804
David Sterbaa937b972014-12-12 17:39:12 +01003805 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003806 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003807 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003808 inode->i_ctime.tv_nsec, &token);
3809
chandan r9cc97d62012-07-04 12:48:07 +05303810 btrfs_set_token_timespec_sec(leaf, &item->otime,
3811 BTRFS_I(inode)->i_otime.tv_sec, &token);
3812 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3813 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3814
Liu Bo51fab692012-12-27 09:01:21 +00003815 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3816 &token);
3817 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3818 &token);
3819 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3820 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3821 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3822 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3823 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003824}
3825
Chris Masond352ac62008-09-29 15:18:18 -04003826/*
3827 * copy everything in the in-memory inode into the btree.
3828 */
Chris Mason21151332011-11-10 20:39:08 -05003829static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003830 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003831{
3832 struct btrfs_inode_item *inode_item;
3833 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003834 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003835 int ret;
3836
3837 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003838 if (!path)
3839 return -ENOMEM;
3840
Chris Masonb9473432009-03-13 11:00:37 -04003841 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003842 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3843 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003844 if (ret) {
3845 if (ret > 0)
3846 ret = -ENOENT;
3847 goto failed;
3848 }
3849
Chris Mason5f39d392007-10-15 16:14:19 -04003850 leaf = path->nodes[0];
3851 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003852 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003853
Chris Masone02119d2008-09-05 16:13:11 -04003854 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003855 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003856 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003857 ret = 0;
3858failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003859 btrfs_free_path(path);
3860 return ret;
3861}
3862
Chris Masond352ac62008-09-29 15:18:18 -04003863/*
Chris Mason21151332011-11-10 20:39:08 -05003864 * copy everything in the in-memory inode into the btree.
3865 */
3866noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3867 struct btrfs_root *root, struct inode *inode)
3868{
3869 int ret;
3870
3871 /*
3872 * If the inode is a free space inode, we can deadlock during commit
3873 * if we put it into the delayed code.
3874 *
3875 * The data relocation inode should also be directly updated
3876 * without delay
3877 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003878 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003879 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3880 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003881 btrfs_update_root_times(trans, root);
3882
Chris Mason21151332011-11-10 20:39:08 -05003883 ret = btrfs_delayed_update_inode(trans, root, inode);
3884 if (!ret)
3885 btrfs_set_inode_last_trans(trans, inode);
3886 return ret;
3887 }
3888
3889 return btrfs_update_inode_item(trans, root, inode);
3890}
3891
Josef Bacikbe6aef62012-10-22 15:43:12 -04003892noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3893 struct btrfs_root *root,
3894 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003895{
3896 int ret;
3897
3898 ret = btrfs_update_inode(trans, root, inode);
3899 if (ret == -ENOSPC)
3900 return btrfs_update_inode_item(trans, root, inode);
3901 return ret;
3902}
3903
3904/*
Chris Masond352ac62008-09-29 15:18:18 -04003905 * unlink helper that gets used here in inode.c and in the tree logging
3906 * recovery code. It remove a link in a directory with a given name, and
3907 * also drops the back refs in the inode to the directory
3908 */
Al Viro92986792011-03-04 17:14:37 +00003909static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3910 struct btrfs_root *root,
3911 struct inode *dir, struct inode *inode,
3912 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003913{
3914 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003915 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003916 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003917 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003918 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003919 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003920 u64 ino = btrfs_ino(inode);
3921 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003922
3923 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003924 if (!path) {
3925 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003926 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003927 }
3928
Chris Masonb9473432009-03-13 11:00:37 -04003929 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003930 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003931 name, name_len, -1);
3932 if (IS_ERR(di)) {
3933 ret = PTR_ERR(di);
3934 goto err;
3935 }
3936 if (!di) {
3937 ret = -ENOENT;
3938 goto err;
3939 }
Chris Mason5f39d392007-10-15 16:14:19 -04003940 leaf = path->nodes[0];
3941 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003942 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003943 if (ret)
3944 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003945 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003946
Miao Xie67de1172013-12-26 13:07:06 +08003947 /*
3948 * If we don't have dir index, we have to get it by looking up
3949 * the inode ref, since we get the inode ref, remove it directly,
3950 * it is unnecessary to do delayed deletion.
3951 *
3952 * But if we have dir index, needn't search inode ref to get it.
3953 * Since the inode ref is close to the inode item, it is better
3954 * that we delay to delete it, and just do this deletion when
3955 * we update the inode item.
3956 */
3957 if (BTRFS_I(inode)->dir_index) {
3958 ret = btrfs_delayed_delete_inode_ref(inode);
3959 if (!ret) {
3960 index = BTRFS_I(inode)->dir_index;
3961 goto skip_backref;
3962 }
3963 }
3964
Li Zefan33345d012011-04-20 10:31:50 +08003965 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3966 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003967 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003968 btrfs_info(root->fs_info,
3969 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003970 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003971 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003972 goto err;
3973 }
Miao Xie67de1172013-12-26 13:07:06 +08003974skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003975 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003976 if (ret) {
3977 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003978 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003979 }
Chris Mason39279cc2007-06-12 06:35:45 -04003980
Chris Masone02119d2008-09-05 16:13:11 -04003981 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003982 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003983 if (ret != 0 && ret != -ENOENT) {
3984 btrfs_abort_transaction(trans, root, ret);
3985 goto err;
3986 }
Chris Masone02119d2008-09-05 16:13:11 -04003987
3988 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3989 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003990 if (ret == -ENOENT)
3991 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003992 else if (ret)
3993 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003994err:
3995 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003996 if (ret)
3997 goto out;
3998
3999 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004000 inode_inc_iversion(inode);
4001 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04004002 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06004003 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004004out:
Chris Mason39279cc2007-06-12 06:35:45 -04004005 return ret;
4006}
4007
Al Viro92986792011-03-04 17:14:37 +00004008int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4009 struct btrfs_root *root,
4010 struct inode *dir, struct inode *inode,
4011 const char *name, int name_len)
4012{
4013 int ret;
4014 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4015 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004016 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004017 ret = btrfs_update_inode(trans, root, inode);
4018 }
4019 return ret;
4020}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004021
4022/*
4023 * helper to start transaction for unlink and rmdir.
4024 *
Josef Bacikd52be812013-05-29 14:54:47 -04004025 * unlink and rmdir are special in btrfs, they do not always free space, so
4026 * if we cannot make our reservations the normal way try and see if there is
4027 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4028 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004029 */
Josef Bacikd52be812013-05-29 14:54:47 -04004030static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004031{
4032 struct btrfs_trans_handle *trans;
4033 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004034 int ret;
4035
Josef Bacike70bea52011-10-11 14:18:24 -04004036 /*
4037 * 1 for the possible orphan item
4038 * 1 for the dir item
4039 * 1 for the dir index
4040 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004041 * 1 for the inode
4042 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04004043 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004044 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
4045 return trans;
4046
Josef Bacikd52be812013-05-29 14:54:47 -04004047 if (PTR_ERR(trans) == -ENOSPC) {
4048 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004049
Josef Bacikd52be812013-05-29 14:54:47 -04004050 trans = btrfs_start_transaction(root, 0);
4051 if (IS_ERR(trans))
4052 return trans;
4053 ret = btrfs_cond_migrate_bytes(root->fs_info,
4054 &root->fs_info->trans_block_rsv,
4055 num_bytes, 5);
4056 if (ret) {
4057 btrfs_end_transaction(trans, root);
4058 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004059 }
Josef Bacik5a77d762011-11-01 14:32:23 -04004060 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04004061 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004062 }
Josef Bacikd52be812013-05-29 14:54:47 -04004063 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004064}
4065
Chris Mason39279cc2007-06-12 06:35:45 -04004066static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4067{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004068 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004069 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004070 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004071 int ret;
4072
Josef Bacikd52be812013-05-29 14:54:47 -04004073 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004074 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004075 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004076
David Howells2b0143b2015-03-17 22:25:59 +00004077 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004078
David Howells2b0143b2015-03-17 22:25:59 +00004079 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004080 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004081 if (ret)
4082 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004083
Yan, Zhenga22285a2010-05-16 10:48:46 -04004084 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004085 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004086 if (ret)
4087 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004088 }
Josef Bacik7b128762008-07-24 12:17:14 -04004089
Tsutomu Itohb5324022011-07-19 07:27:20 +00004090out:
Josef Bacikd52be812013-05-29 14:54:47 -04004091 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004092 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004093 return ret;
4094}
4095
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004096int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4097 struct btrfs_root *root,
4098 struct inode *dir, u64 objectid,
4099 const char *name, int name_len)
4100{
4101 struct btrfs_path *path;
4102 struct extent_buffer *leaf;
4103 struct btrfs_dir_item *di;
4104 struct btrfs_key key;
4105 u64 index;
4106 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004107 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004108
4109 path = btrfs_alloc_path();
4110 if (!path)
4111 return -ENOMEM;
4112
Li Zefan33345d012011-04-20 10:31:50 +08004113 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004114 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004115 if (IS_ERR_OR_NULL(di)) {
4116 if (!di)
4117 ret = -ENOENT;
4118 else
4119 ret = PTR_ERR(di);
4120 goto out;
4121 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004122
4123 leaf = path->nodes[0];
4124 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4125 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4126 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004127 if (ret) {
4128 btrfs_abort_transaction(trans, root, ret);
4129 goto out;
4130 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004131 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004132
4133 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4134 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004135 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004136 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004137 if (ret != -ENOENT) {
4138 btrfs_abort_transaction(trans, root, ret);
4139 goto out;
4140 }
Li Zefan33345d012011-04-20 10:31:50 +08004141 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004142 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004143 if (IS_ERR_OR_NULL(di)) {
4144 if (!di)
4145 ret = -ENOENT;
4146 else
4147 ret = PTR_ERR(di);
4148 btrfs_abort_transaction(trans, root, ret);
4149 goto out;
4150 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004151
4152 leaf = path->nodes[0];
4153 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004154 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004155 index = key.offset;
4156 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004157 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004158
Miao Xie16cdcec2011-04-22 18:12:22 +08004159 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004160 if (ret) {
4161 btrfs_abort_transaction(trans, root, ret);
4162 goto out;
4163 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004164
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004165 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004166 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004167 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004168 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004169 if (ret)
4170 btrfs_abort_transaction(trans, root, ret);
4171out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004172 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004173 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004174}
4175
Chris Mason39279cc2007-06-12 06:35:45 -04004176static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4177{
David Howells2b0143b2015-03-17 22:25:59 +00004178 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004179 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004180 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004181 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004182
David Sterbab3ae2442012-09-13 16:04:34 -06004183 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004184 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004185 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4186 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004187
Josef Bacikd52be812013-05-29 14:54:47 -04004188 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004189 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004190 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004191
Li Zefan33345d012011-04-20 10:31:50 +08004192 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004193 err = btrfs_unlink_subvol(trans, root, dir,
4194 BTRFS_I(inode)->location.objectid,
4195 dentry->d_name.name,
4196 dentry->d_name.len);
4197 goto out;
4198 }
4199
Josef Bacik7b128762008-07-24 12:17:14 -04004200 err = btrfs_orphan_add(trans, inode);
4201 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004202 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004203
Chris Mason39279cc2007-06-12 06:35:45 -04004204 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004205 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004206 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004207 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004208 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004209out:
Josef Bacikd52be812013-05-29 14:54:47 -04004210 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004211 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004212
Chris Mason39279cc2007-06-12 06:35:45 -04004213 return err;
4214}
4215
Chris Mason28f75a02015-02-04 06:59:29 -08004216static int truncate_space_check(struct btrfs_trans_handle *trans,
4217 struct btrfs_root *root,
4218 u64 bytes_deleted)
4219{
4220 int ret;
4221
4222 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
4223 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4224 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4225 if (!ret)
4226 trans->bytes_reserved += bytes_deleted;
4227 return ret;
4228
4229}
4230
Filipe Manana0305cd52015-10-16 12:34:25 +01004231static int truncate_inline_extent(struct inode *inode,
4232 struct btrfs_path *path,
4233 struct btrfs_key *found_key,
4234 const u64 item_end,
4235 const u64 new_size)
4236{
4237 struct extent_buffer *leaf = path->nodes[0];
4238 int slot = path->slots[0];
4239 struct btrfs_file_extent_item *fi;
4240 u32 size = (u32)(new_size - found_key->offset);
4241 struct btrfs_root *root = BTRFS_I(inode)->root;
4242
4243 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4244
4245 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4246 loff_t offset = new_size;
4247 loff_t page_end = ALIGN(offset, PAGE_CACHE_SIZE);
4248
4249 /*
4250 * Zero out the remaining of the last page of our inline extent,
4251 * instead of directly truncating our inline extent here - that
4252 * would be much more complex (decompressing all the data, then
4253 * compressing the truncated data, which might be bigger than
4254 * the size of the inline extent, resize the extent, etc).
4255 * We release the path because to get the page we might need to
4256 * read the extent item from disk (data not in the page cache).
4257 */
4258 btrfs_release_path(path);
4259 return btrfs_truncate_page(inode, offset, page_end - offset, 0);
4260 }
4261
4262 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4263 size = btrfs_file_extent_calc_inline_size(size);
4264 btrfs_truncate_item(root, path, size, 1);
4265
4266 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4267 inode_sub_bytes(inode, item_end + 1 - new_size);
4268
4269 return 0;
4270}
4271
Chris Mason323ac952008-10-01 19:05:46 -04004272/*
Chris Mason39279cc2007-06-12 06:35:45 -04004273 * this can truncate away extent items, csum items and directory items.
4274 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004275 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004276 *
4277 * csum items that cross the new i_size are truncated to the new size
4278 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004279 *
4280 * min_type is the minimum key type to truncate down to. If set to 0, this
4281 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004282 */
Yan, Zheng80825102009-11-12 09:35:36 +00004283int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4284 struct btrfs_root *root,
4285 struct inode *inode,
4286 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004287{
Chris Mason39279cc2007-06-12 06:35:45 -04004288 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004289 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004290 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004291 struct btrfs_key key;
4292 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004293 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004294 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004295 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004296 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004297 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004298 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004299 int found_extent;
4300 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004301 int pending_del_nr = 0;
4302 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004303 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004304 int ret;
4305 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004306 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004307 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004308 bool be_nice = 0;
4309 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004310 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004311
4312 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004313
Chris Mason28ed1342014-12-17 09:41:04 -08004314 /*
4315 * for non-free space inodes and ref cows, we want to back off from
4316 * time to time
4317 */
4318 if (!btrfs_is_free_space_inode(inode) &&
4319 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4320 be_nice = 1;
4321
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004322 path = btrfs_alloc_path();
4323 if (!path)
4324 return -ENOMEM;
4325 path->reada = -1;
4326
Josef Bacik5dc562c2012-08-17 13:14:17 -04004327 /*
4328 * We want to drop from the next block forward in case this new size is
4329 * not block aligned since we will be keeping the last block of the
4330 * extent just the way it is.
4331 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004332 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4333 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004334 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4335 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004336
Miao Xie16cdcec2011-04-22 18:12:22 +08004337 /*
4338 * This function is also used to drop the items in the log tree before
4339 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4340 * it is used to drop the loged items. So we shouldn't kill the delayed
4341 * items.
4342 */
4343 if (min_type == 0 && root == BTRFS_I(inode)->root)
4344 btrfs_kill_delayed_inode_items(inode);
4345
Li Zefan33345d012011-04-20 10:31:50 +08004346 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004347 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004348 key.type = (u8)-1;
4349
Chris Mason85e21ba2008-01-29 15:11:36 -05004350search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004351 /*
4352 * with a 16K leaf size and 128MB extents, you can actually queue
4353 * up a huge file in a single leaf. Most of the time that
4354 * bytes_deleted is > 0, it will be huge by the time we get here
4355 */
4356 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
4357 if (btrfs_should_end_transaction(trans, root)) {
4358 err = -EAGAIN;
4359 goto error;
4360 }
4361 }
4362
4363
Chris Masonb9473432009-03-13 11:00:37 -04004364 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004365 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004366 if (ret < 0) {
4367 err = ret;
4368 goto out;
4369 }
Chris Masond3977122009-01-05 21:25:51 -05004370
Chris Mason85e21ba2008-01-29 15:11:36 -05004371 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004372 /* there are no items in the tree for us to truncate, we're
4373 * done
4374 */
Yan, Zheng80825102009-11-12 09:35:36 +00004375 if (path->slots[0] == 0)
4376 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004377 path->slots[0]--;
4378 }
4379
Chris Masond3977122009-01-05 21:25:51 -05004380 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004381 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004382 leaf = path->nodes[0];
4383 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004384 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004385
Li Zefan33345d012011-04-20 10:31:50 +08004386 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004387 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004388
Chris Mason85e21ba2008-01-29 15:11:36 -05004389 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004390 break;
4391
Chris Mason5f39d392007-10-15 16:14:19 -04004392 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004393 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004394 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004395 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004396 extent_type = btrfs_file_extent_type(leaf, fi);
4397 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004398 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004399 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004400 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004401 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004402 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004403 }
Yan008630c2007-11-07 13:31:09 -05004404 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004405 }
Yan, Zheng80825102009-11-12 09:35:36 +00004406 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004407 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004408 } else {
4409 if (item_end < new_size)
4410 break;
4411 if (found_key.offset >= new_size)
4412 del_item = 1;
4413 else
4414 del_item = 0;
4415 }
Chris Mason39279cc2007-06-12 06:35:45 -04004416 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004417 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004418 if (found_type != BTRFS_EXTENT_DATA_KEY)
4419 goto delete;
4420
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004421 if (del_item)
4422 last_size = found_key.offset;
4423 else
4424 last_size = new_size;
4425
Chris Mason179e29e2007-11-01 11:28:41 -04004426 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004427 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004428 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004429 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004430 u64 orig_num_bytes =
4431 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004432 extent_num_bytes = ALIGN(new_size -
4433 found_key.offset,
4434 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004435 btrfs_set_file_extent_num_bytes(leaf, fi,
4436 extent_num_bytes);
4437 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004438 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004439 if (test_bit(BTRFS_ROOT_REF_COWS,
4440 &root->state) &&
4441 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004442 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004443 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004444 } else {
Chris Masondb945352007-10-15 16:15:53 -04004445 extent_num_bytes =
4446 btrfs_file_extent_disk_num_bytes(leaf,
4447 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004448 extent_offset = found_key.offset -
4449 btrfs_file_extent_offset(leaf, fi);
4450
Chris Mason39279cc2007-06-12 06:35:45 -04004451 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004452 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004453 if (extent_start != 0) {
4454 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004455 if (test_bit(BTRFS_ROOT_REF_COWS,
4456 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004457 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004458 }
4459 }
Chris Mason90692182008-02-08 13:49:28 -05004460 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004461 /*
4462 * we can't truncate inline items that have had
4463 * special encodings
4464 */
4465 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004466 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4467 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004468
4469 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004470 * Need to release path in order to truncate a
4471 * compressed extent. So delete any accumulated
4472 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004473 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004474 if (btrfs_file_extent_compression(leaf, fi) !=
4475 BTRFS_COMPRESS_NONE && pending_del_nr) {
4476 err = btrfs_del_items(trans, root, path,
4477 pending_del_slot,
4478 pending_del_nr);
4479 if (err) {
4480 btrfs_abort_transaction(trans,
4481 root,
4482 err);
4483 goto error;
4484 }
4485 pending_del_nr = 0;
4486 }
4487
4488 err = truncate_inline_extent(inode, path,
4489 &found_key,
4490 item_end,
4491 new_size);
4492 if (err) {
4493 btrfs_abort_transaction(trans,
4494 root, err);
4495 goto error;
4496 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004497 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4498 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004499 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004500 }
Chris Mason39279cc2007-06-12 06:35:45 -04004501 }
Chris Mason179e29e2007-11-01 11:28:41 -04004502delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004503 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004504 if (!pending_del_nr) {
4505 /* no pending yet, add ourselves */
4506 pending_del_slot = path->slots[0];
4507 pending_del_nr = 1;
4508 } else if (pending_del_nr &&
4509 path->slots[0] + 1 == pending_del_slot) {
4510 /* hop on the pending chunk */
4511 pending_del_nr++;
4512 pending_del_slot = path->slots[0];
4513 } else {
Chris Masond3977122009-01-05 21:25:51 -05004514 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004515 }
Chris Mason39279cc2007-06-12 06:35:45 -04004516 } else {
4517 break;
4518 }
Chris Mason28f75a02015-02-04 06:59:29 -08004519 should_throttle = 0;
4520
Miao Xie27cdeb72014-04-02 19:51:05 +08004521 if (found_extent &&
4522 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4523 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004524 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004525 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004526 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004527 extent_num_bytes, 0,
4528 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004529 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004530 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004531 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004532 btrfs_async_run_delayed_refs(root,
4533 trans->delayed_ref_updates * 2, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004534 if (be_nice) {
4535 if (truncate_space_check(trans, root,
4536 extent_num_bytes)) {
4537 should_end = 1;
4538 }
4539 if (btrfs_should_throttle_delayed_refs(trans,
4540 root)) {
4541 should_throttle = 1;
4542 }
4543 }
Chris Mason39279cc2007-06-12 06:35:45 -04004544 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004545
Yan, Zheng80825102009-11-12 09:35:36 +00004546 if (found_type == BTRFS_INODE_ITEM_KEY)
4547 break;
4548
4549 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004550 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004551 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004552 if (pending_del_nr) {
4553 ret = btrfs_del_items(trans, root, path,
4554 pending_del_slot,
4555 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004556 if (ret) {
4557 btrfs_abort_transaction(trans,
4558 root, ret);
4559 goto error;
4560 }
Yan, Zheng80825102009-11-12 09:35:36 +00004561 pending_del_nr = 0;
4562 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004563 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004564 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004565 unsigned long updates = trans->delayed_ref_updates;
4566 if (updates) {
4567 trans->delayed_ref_updates = 0;
4568 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4569 if (ret && !err)
4570 err = ret;
4571 }
4572 }
Chris Mason28f75a02015-02-04 06:59:29 -08004573 /*
4574 * if we failed to refill our space rsv, bail out
4575 * and let the transaction restart
4576 */
4577 if (should_end) {
4578 err = -EAGAIN;
4579 goto error;
4580 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004581 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004582 } else {
4583 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004584 }
Chris Mason39279cc2007-06-12 06:35:45 -04004585 }
Yan, Zheng80825102009-11-12 09:35:36 +00004586out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004587 if (pending_del_nr) {
4588 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4589 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004590 if (ret)
4591 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004592 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004593error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004594 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004595 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004596
Chris Mason39279cc2007-06-12 06:35:45 -04004597 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004598
Chris Mason28f75a02015-02-04 06:59:29 -08004599 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
Chris Mason28ed1342014-12-17 09:41:04 -08004600 unsigned long updates = trans->delayed_ref_updates;
4601 if (updates) {
4602 trans->delayed_ref_updates = 0;
4603 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4604 if (ret && !err)
4605 err = ret;
4606 }
4607 }
Yan, Zheng80825102009-11-12 09:35:36 +00004608 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004609}
4610
Chris Masona52d9a82007-08-27 16:49:44 -04004611/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004612 * btrfs_truncate_page - read, zero a chunk and write a page
4613 * @inode - inode that we're zeroing
4614 * @from - the offset to start zeroing
4615 * @len - the length to zero, 0 to zero the entire range respective to the
4616 * offset
4617 * @front - zero up to the offset instead of from the offset on
4618 *
4619 * This will find the page for the "from" offset and cow the page and zero the
4620 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004621 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004622int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4623 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004624{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004625 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004626 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004627 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4628 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004629 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004630 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004631 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004632 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4633 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4634 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004635 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004636 int ret = 0;
4637 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004638 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004639
Josef Bacik2aaa6652012-08-29 14:27:18 -04004640 if ((offset & (blocksize - 1)) == 0 &&
4641 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004642 goto out;
Qu Wenruodf480632015-09-08 17:25:54 +08004643 ret = __btrfs_delalloc_reserve_space(inode,
4644 round_down(from, PAGE_CACHE_SIZE), PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004645 if (ret)
4646 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004647
Chris Mason211c17f2008-05-15 09:13:45 -04004648again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004649 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004650 if (!page) {
Qu Wenruodf480632015-09-08 17:25:54 +08004651 __btrfs_delalloc_release_space(inode,
4652 round_down(from, PAGE_CACHE_SIZE),
4653 PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004654 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004655 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004656 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004657
4658 page_start = page_offset(page);
4659 page_end = page_start + PAGE_CACHE_SIZE - 1;
4660
Chris Masona52d9a82007-08-27 16:49:44 -04004661 if (!PageUptodate(page)) {
4662 ret = btrfs_readpage(NULL, page);
4663 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004664 if (page->mapping != mapping) {
4665 unlock_page(page);
4666 page_cache_release(page);
4667 goto again;
4668 }
Chris Masona52d9a82007-08-27 16:49:44 -04004669 if (!PageUptodate(page)) {
4670 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004671 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004672 }
4673 }
Chris Mason211c17f2008-05-15 09:13:45 -04004674 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004675
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004676 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004677 set_page_extent_mapped(page);
4678
4679 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4680 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004681 unlock_extent_cached(io_tree, page_start, page_end,
4682 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004683 unlock_page(page);
4684 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004685 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004686 btrfs_put_ordered_extent(ordered);
4687 goto again;
4688 }
4689
Josef Bacik2ac55d42010-02-03 19:33:23 +00004690 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004691 EXTENT_DIRTY | EXTENT_DELALLOC |
4692 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004693 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004694
Josef Bacik2ac55d42010-02-03 19:33:23 +00004695 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4696 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004697 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004698 unlock_extent_cached(io_tree, page_start, page_end,
4699 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004700 goto out_unlock;
4701 }
4702
Chris Masone6dcd2d2008-07-17 12:53:50 -04004703 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004704 if (!len)
4705 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004706 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004707 if (front)
4708 memset(kaddr, 0, offset);
4709 else
4710 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004711 flush_dcache_page(page);
4712 kunmap(page);
4713 }
Chris Mason247e7432008-07-17 12:53:51 -04004714 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004715 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004716 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4717 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004718
Chris Mason89642222008-07-24 09:41:53 -04004719out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004720 if (ret)
Qu Wenruodf480632015-09-08 17:25:54 +08004721 __btrfs_delalloc_release_space(inode, page_start,
4722 PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004723 unlock_page(page);
4724 page_cache_release(page);
4725out:
4726 return ret;
4727}
4728
Josef Bacik16e75492013-10-22 12:18:51 -04004729static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4730 u64 offset, u64 len)
4731{
4732 struct btrfs_trans_handle *trans;
4733 int ret;
4734
4735 /*
4736 * Still need to make sure the inode looks like it's been updated so
4737 * that any holes get logged if we fsync.
4738 */
4739 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4740 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4741 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4742 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4743 return 0;
4744 }
4745
4746 /*
4747 * 1 - for the one we're dropping
4748 * 1 - for the one we're adding
4749 * 1 - for updating the inode.
4750 */
4751 trans = btrfs_start_transaction(root, 3);
4752 if (IS_ERR(trans))
4753 return PTR_ERR(trans);
4754
4755 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4756 if (ret) {
4757 btrfs_abort_transaction(trans, root, ret);
4758 btrfs_end_transaction(trans, root);
4759 return ret;
4760 }
4761
4762 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4763 0, 0, len, 0, len, 0, 0, 0);
4764 if (ret)
4765 btrfs_abort_transaction(trans, root, ret);
4766 else
4767 btrfs_update_inode(trans, root, inode);
4768 btrfs_end_transaction(trans, root);
4769 return ret;
4770}
4771
Josef Bacik695a0d02011-03-04 15:46:53 -05004772/*
4773 * This function puts in dummy file extents for the area we're creating a hole
4774 * for. So if we are truncating this file to a larger size we need to insert
4775 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4776 * the range between oldsize and size
4777 */
Josef Bacika41ad392011-01-31 15:30:16 -05004778int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004779{
Yan Zheng9036c102008-10-30 14:19:41 -04004780 struct btrfs_root *root = BTRFS_I(inode)->root;
4781 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004782 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004783 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004784 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004785 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4786 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004787 u64 last_byte;
4788 u64 cur_offset;
4789 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004790 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004791
Josef Bacika71754f2013-06-17 17:14:39 -04004792 /*
4793 * If our size started in the middle of a page we need to zero out the
4794 * rest of the page before we expand the i_size, otherwise we could
4795 * expose stale data.
4796 */
4797 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4798 if (err)
4799 return err;
4800
Yan Zheng9036c102008-10-30 14:19:41 -04004801 if (size <= hole_start)
4802 return 0;
4803
Yan Zheng9036c102008-10-30 14:19:41 -04004804 while (1) {
4805 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004806
Josef Bacik2ac55d42010-02-03 19:33:23 +00004807 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004808 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004809 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4810 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004811 if (!ordered)
4812 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004813 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4814 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004815 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004816 btrfs_put_ordered_extent(ordered);
4817 }
4818
Yan Zheng9036c102008-10-30 14:19:41 -04004819 cur_offset = hole_start;
4820 while (1) {
4821 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4822 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004823 if (IS_ERR(em)) {
4824 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004825 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004826 break;
4827 }
Yan Zheng9036c102008-10-30 14:19:41 -04004828 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004829 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004830 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004831 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004832 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004833
Josef Bacik16e75492013-10-22 12:18:51 -04004834 err = maybe_insert_hole(root, inode, cur_offset,
4835 hole_size);
4836 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004837 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004838 btrfs_drop_extent_cache(inode, cur_offset,
4839 cur_offset + hole_size - 1, 0);
4840 hole_em = alloc_extent_map();
4841 if (!hole_em) {
4842 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4843 &BTRFS_I(inode)->runtime_flags);
4844 goto next;
4845 }
4846 hole_em->start = cur_offset;
4847 hole_em->len = hole_size;
4848 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004849
Josef Bacik5dc562c2012-08-17 13:14:17 -04004850 hole_em->block_start = EXTENT_MAP_HOLE;
4851 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004852 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004853 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004854 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4855 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004856 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004857
4858 while (1) {
4859 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004860 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004861 write_unlock(&em_tree->lock);
4862 if (err != -EEXIST)
4863 break;
4864 btrfs_drop_extent_cache(inode, cur_offset,
4865 cur_offset +
4866 hole_size - 1, 0);
4867 }
4868 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004869 }
Josef Bacik16e75492013-10-22 12:18:51 -04004870next:
Yan Zheng9036c102008-10-30 14:19:41 -04004871 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004872 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004873 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004874 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004875 break;
4876 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004877 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004878 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4879 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004880 return err;
4881}
4882
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004883static int wait_snapshoting_atomic_t(atomic_t *a)
4884{
4885 schedule();
4886 return 0;
4887}
4888
4889static void wait_for_snapshot_creation(struct btrfs_root *root)
4890{
4891 while (true) {
4892 int ret;
4893
4894 ret = btrfs_start_write_no_snapshoting(root);
4895 if (ret)
4896 break;
4897 wait_on_atomic_t(&root->will_be_snapshoted,
4898 wait_snapshoting_atomic_t,
4899 TASK_UNINTERRUPTIBLE);
4900 }
4901}
4902
Eric Sandeen3972f262013-01-12 02:57:22 +00004903static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004904{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004905 struct btrfs_root *root = BTRFS_I(inode)->root;
4906 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004907 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004908 loff_t newsize = attr->ia_size;
4909 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004910 int ret;
4911
Eric Sandeen3972f262013-01-12 02:57:22 +00004912 /*
4913 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4914 * special case where we need to update the times despite not having
4915 * these flags set. For all other operations the VFS set these flags
4916 * explicitly if it wants a timestamp update.
4917 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004918 if (newsize != oldsize) {
4919 inode_inc_iversion(inode);
4920 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4921 inode->i_ctime = inode->i_mtime =
4922 current_fs_time(inode->i_sb);
4923 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004924
Josef Bacika41ad392011-01-31 15:30:16 -05004925 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004926 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004927 /*
4928 * Don't do an expanding truncate while snapshoting is ongoing.
4929 * This is to ensure the snapshot captures a fully consistent
4930 * state of this file - if the snapshot captures this expanding
4931 * truncation, it must capture all writes that happened before
4932 * this truncation.
4933 */
4934 wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004935 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004936 if (ret) {
4937 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004938 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004939 }
Yan, Zheng80825102009-11-12 09:35:36 +00004940
Miao Xief4a2f4c2011-12-14 20:12:01 -05004941 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004942 if (IS_ERR(trans)) {
4943 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004944 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004945 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004946
4947 i_size_write(inode, newsize);
4948 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4949 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004950 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004951 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004952 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004953
Josef Bacika41ad392011-01-31 15:30:16 -05004954 /*
4955 * We're truncating a file that used to have good data down to
4956 * zero. Make sure it gets into the ordered flush list so that
4957 * any new writes get down to disk quickly.
4958 */
4959 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004960 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4961 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004962
Josef Bacikf3fe8202013-01-07 17:03:21 -05004963 /*
4964 * 1 for the orphan item we're going to add
4965 * 1 for the orphan item deletion.
4966 */
4967 trans = btrfs_start_transaction(root, 2);
4968 if (IS_ERR(trans))
4969 return PTR_ERR(trans);
4970
4971 /*
4972 * We need to do this in case we fail at _any_ point during the
4973 * actual truncate. Once we do the truncate_setsize we could
4974 * invalidate pages which forces any outstanding ordered io to
4975 * be instantly completed which will give us extents that need
4976 * to be truncated. If we fail to get an orphan inode down we
4977 * could have left over extents that were never meant to live,
4978 * so we need to garuntee from this point on that everything
4979 * will be consistent.
4980 */
4981 ret = btrfs_orphan_add(trans, inode);
4982 btrfs_end_transaction(trans, root);
4983 if (ret)
4984 return ret;
4985
Josef Bacika41ad392011-01-31 15:30:16 -05004986 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4987 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004988
4989 /* Disable nonlocked read DIO to avoid the end less truncate */
4990 btrfs_inode_block_unlocked_dio(inode);
4991 inode_dio_wait(inode);
4992 btrfs_inode_resume_unlocked_dio(inode);
4993
Josef Bacika41ad392011-01-31 15:30:16 -05004994 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004995 if (ret && inode->i_nlink) {
4996 int err;
4997
4998 /*
4999 * failed to truncate, disk_i_size is only adjusted down
5000 * as we remove extents, so it should represent the true
5001 * size of the inode, so reset the in memory size and
5002 * delete our orphan entry.
5003 */
5004 trans = btrfs_join_transaction(root);
5005 if (IS_ERR(trans)) {
5006 btrfs_orphan_del(NULL, inode);
5007 return ret;
5008 }
5009 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5010 err = btrfs_orphan_del(trans, inode);
5011 if (err)
5012 btrfs_abort_transaction(trans, root, err);
5013 btrfs_end_transaction(trans, root);
5014 }
Yan, Zheng80825102009-11-12 09:35:36 +00005015 }
5016
Josef Bacika41ad392011-01-31 15:30:16 -05005017 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005018}
5019
Chris Mason39279cc2007-06-12 06:35:45 -04005020static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5021{
David Howells2b0143b2015-03-17 22:25:59 +00005022 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005023 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005024 int err;
5025
Li Zefanb83cc962010-12-20 16:04:08 +08005026 if (btrfs_root_readonly(root))
5027 return -EROFS;
5028
Chris Mason39279cc2007-06-12 06:35:45 -04005029 err = inode_change_ok(inode, attr);
5030 if (err)
5031 return err;
5032
Chris Mason5a3f23d2009-03-31 13:27:11 -04005033 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005034 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005035 if (err)
5036 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005037 }
Yan Zheng9036c102008-10-30 14:19:41 -04005038
Christoph Hellwig10257742010-06-04 11:30:02 +02005039 if (attr->ia_valid) {
5040 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005041 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005042 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005043
Josef Bacik22c44fe2011-11-30 10:45:38 -05005044 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005045 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005046 }
5047
Chris Mason39279cc2007-06-12 06:35:45 -04005048 return err;
5049}
Chris Mason61295eb2008-01-14 16:24:38 -05005050
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005051/*
5052 * While truncating the inode pages during eviction, we get the VFS calling
5053 * btrfs_invalidatepage() against each page of the inode. This is slow because
5054 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5055 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5056 * extent_state structures over and over, wasting lots of time.
5057 *
5058 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5059 * those expensive operations on a per page basis and do only the ordered io
5060 * finishing, while we release here the extent_map and extent_state structures,
5061 * without the excessive merging and splitting.
5062 */
5063static void evict_inode_truncate_pages(struct inode *inode)
5064{
5065 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5066 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5067 struct rb_node *node;
5068
5069 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005070 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005071
5072 write_lock(&map_tree->lock);
5073 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5074 struct extent_map *em;
5075
5076 node = rb_first(&map_tree->map);
5077 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005078 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5079 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005080 remove_extent_mapping(map_tree, em);
5081 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005082 if (need_resched()) {
5083 write_unlock(&map_tree->lock);
5084 cond_resched();
5085 write_lock(&map_tree->lock);
5086 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005087 }
5088 write_unlock(&map_tree->lock);
5089
Filipe Manana6ca07092015-05-26 00:55:42 +01005090 /*
5091 * Keep looping until we have no more ranges in the io tree.
5092 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005093 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5094 * still in progress (unlocked the pages in the bio but did not yet
5095 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005096 * ranges can still be locked and eviction started because before
5097 * submitting those bios, which are executed by a separate task (work
5098 * queue kthread), inode references (inode->i_count) were not taken
5099 * (which would be dropped in the end io callback of each bio).
5100 * Therefore here we effectively end up waiting for those bios and
5101 * anyone else holding locked ranges without having bumped the inode's
5102 * reference count - if we don't do it, when they access the inode's
5103 * io_tree to unlock a range it may be too late, leading to an
5104 * use-after-free issue.
5105 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005106 spin_lock(&io_tree->lock);
5107 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5108 struct extent_state *state;
5109 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005110 u64 start;
5111 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005112
5113 node = rb_first(&io_tree->state);
5114 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005115 start = state->start;
5116 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005117 spin_unlock(&io_tree->lock);
5118
Filipe Manana6ca07092015-05-26 00:55:42 +01005119 lock_extent_bits(io_tree, start, end, 0, &cached_state);
5120 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005121 EXTENT_LOCKED | EXTENT_DIRTY |
5122 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5123 EXTENT_DEFRAG, 1, 1,
5124 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005125
Filipe Manana7064dd52014-08-08 02:47:05 +01005126 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005127 spin_lock(&io_tree->lock);
5128 }
5129 spin_unlock(&io_tree->lock);
5130}
5131
Al Virobd555972010-06-07 11:35:40 -04005132void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005133{
5134 struct btrfs_trans_handle *trans;
5135 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005136 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005137 int steal_from_global = 0;
Josef Bacik07127182011-08-19 10:29:59 -04005138 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04005139 int ret;
5140
liubo1abe9b82011-03-24 11:18:59 +00005141 trace_btrfs_inode_evict(inode);
5142
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005143 evict_inode_truncate_pages(inode);
5144
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005145 if (inode->i_nlink &&
5146 ((btrfs_root_refs(&root->root_item) != 0 &&
5147 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5148 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005149 goto no_delete;
5150
Chris Mason39279cc2007-06-12 06:35:45 -04005151 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005152 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005153 goto no_delete;
5154 }
Al Virobd555972010-06-07 11:35:40 -04005155 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005156 if (!special_file(inode->i_mode))
5157 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005158
Miao Xief6124962014-09-12 18:44:04 +08005159 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5160
Yan, Zhengc71bf092009-11-12 09:34:40 +00005161 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06005162 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005163 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005164 goto no_delete;
5165 }
5166
Yan, Zheng76dda932009-09-21 16:00:26 -04005167 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005168 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5169 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005170 goto no_delete;
5171 }
5172
Miao Xie0e8c36a2012-12-19 06:59:51 +00005173 ret = btrfs_commit_inode_delayed_inode(inode);
5174 if (ret) {
5175 btrfs_orphan_del(NULL, inode);
5176 goto no_delete;
5177 }
5178
Miao Xie66d8f3d2012-09-06 04:02:28 -06005179 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005180 if (!rsv) {
5181 btrfs_orphan_del(NULL, inode);
5182 goto no_delete;
5183 }
Josef Bacik4a338542011-08-29 11:01:31 -04005184 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005185 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005186 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005187
Chris Masondbe674a2008-07-17 12:54:05 -04005188 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005189
Josef Bacik4289a662011-08-05 13:22:24 -04005190 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005191 * This is a bit simpler than btrfs_truncate since we've already
5192 * reserved our space for our orphan item in the unlink, so we just
5193 * need to reserve some slack space in case we add bytes and update
5194 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005195 */
Yan, Zheng80825102009-11-12 09:35:36 +00005196 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005197 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5198 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005199
Josef Bacik726c35f2011-09-26 15:46:06 -04005200 /*
5201 * Try and steal from the global reserve since we will
5202 * likely not use this space anyway, we want to try as
5203 * hard as possible to get this to work.
5204 */
5205 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005206 steal_from_global++;
5207 else
5208 steal_from_global = 0;
5209 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005210
Josef Bacik3bce8762015-02-24 12:35:51 -08005211 /*
5212 * steal_from_global == 0: we reserved stuff, hooray!
5213 * steal_from_global == 1: we didn't reserve stuff, boo!
5214 * steal_from_global == 2: we've committed, still not a lot of
5215 * room but maybe we'll have room in the global reserve this
5216 * time.
5217 * steal_from_global == 3: abandon all hope!
5218 */
5219 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005220 btrfs_warn(root->fs_info,
5221 "Could not get space for a delete, will truncate on mount %d",
5222 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005223 btrfs_orphan_del(NULL, inode);
5224 btrfs_free_block_rsv(root, rsv);
5225 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005226 }
5227
Miao Xie0e8c36a2012-12-19 06:59:51 +00005228 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005229 if (IS_ERR(trans)) {
5230 btrfs_orphan_del(NULL, inode);
5231 btrfs_free_block_rsv(root, rsv);
5232 goto no_delete;
5233 }
5234
Josef Bacik3bce8762015-02-24 12:35:51 -08005235 /*
5236 * We can't just steal from the global reserve, we need tomake
5237 * sure there is room to do it, if not we need to commit and try
5238 * again.
5239 */
5240 if (steal_from_global) {
5241 if (!btrfs_check_space_for_delayed_refs(trans, root))
5242 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5243 min_size);
5244 else
5245 ret = -ENOSPC;
5246 }
5247
5248 /*
5249 * Couldn't steal from the global reserve, we have too much
5250 * pending stuff built up, commit the transaction and try it
5251 * again.
5252 */
5253 if (ret) {
5254 ret = btrfs_commit_transaction(trans, root);
5255 if (ret) {
5256 btrfs_orphan_del(NULL, inode);
5257 btrfs_free_block_rsv(root, rsv);
5258 goto no_delete;
5259 }
5260 continue;
5261 } else {
5262 steal_from_global = 0;
5263 }
5264
Josef Bacik4289a662011-08-05 13:22:24 -04005265 trans->block_rsv = rsv;
5266
Yan, Zhengd68fc572010-05-16 10:49:58 -04005267 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005268 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005269 break;
5270
Miao Xie8407aa42012-09-07 01:43:32 -06005271 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005272 btrfs_end_transaction(trans, root);
5273 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005274 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005275 }
5276
Josef Bacik4289a662011-08-05 13:22:24 -04005277 btrfs_free_block_rsv(root, rsv);
5278
Josef Bacik4ef31a42013-08-13 14:10:08 -04005279 /*
5280 * Errors here aren't a big deal, it just means we leave orphan items
5281 * in the tree. They will be cleaned up on the next mount.
5282 */
Yan, Zheng80825102009-11-12 09:35:36 +00005283 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005284 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005285 btrfs_orphan_del(trans, inode);
5286 } else {
5287 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005288 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005289
Josef Bacik4289a662011-08-05 13:22:24 -04005290 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005291 if (!(root == root->fs_info->tree_root ||
5292 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005293 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005294
Chris Mason54aa1f42007-06-22 14:16:25 -04005295 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005296 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005297no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005298 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005299 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005300 return;
Chris Mason39279cc2007-06-12 06:35:45 -04005301}
5302
5303/*
5304 * this returns the key found in the dir entry in the location pointer.
5305 * If no dir entries were found, location->objectid is 0.
5306 */
5307static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5308 struct btrfs_key *location)
5309{
5310 const char *name = dentry->d_name.name;
5311 int namelen = dentry->d_name.len;
5312 struct btrfs_dir_item *di;
5313 struct btrfs_path *path;
5314 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005315 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005316
5317 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005318 if (!path)
5319 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005320
Li Zefan33345d012011-04-20 10:31:50 +08005321 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005322 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005323 if (IS_ERR(di))
5324 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005325
David Sterbac7040052011-04-19 18:00:01 +02005326 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005327 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005328
Chris Mason5f39d392007-10-15 16:14:19 -04005329 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005330out:
Chris Mason39279cc2007-06-12 06:35:45 -04005331 btrfs_free_path(path);
5332 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005333out_err:
5334 location->objectid = 0;
5335 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005336}
5337
5338/*
5339 * when we hit a tree root in a directory, the btrfs part of the inode
5340 * needs to be changed to reflect the root directory of the tree root. This
5341 * is kind of like crossing a mount point.
5342 */
5343static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005344 struct inode *dir,
5345 struct dentry *dentry,
5346 struct btrfs_key *location,
5347 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005348{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005349 struct btrfs_path *path;
5350 struct btrfs_root *new_root;
5351 struct btrfs_root_ref *ref;
5352 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005353 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005354 int ret;
5355 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005356
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005357 path = btrfs_alloc_path();
5358 if (!path) {
5359 err = -ENOMEM;
5360 goto out;
5361 }
Chris Mason39279cc2007-06-12 06:35:45 -04005362
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005363 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005364 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5365 key.type = BTRFS_ROOT_REF_KEY;
5366 key.offset = location->objectid;
5367
5368 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5369 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005370 if (ret) {
5371 if (ret < 0)
5372 err = ret;
5373 goto out;
5374 }
Chris Mason39279cc2007-06-12 06:35:45 -04005375
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005376 leaf = path->nodes[0];
5377 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005378 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005379 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5380 goto out;
5381
5382 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5383 (unsigned long)(ref + 1),
5384 dentry->d_name.len);
5385 if (ret)
5386 goto out;
5387
David Sterbab3b4aa72011-04-21 01:20:15 +02005388 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005389
5390 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5391 if (IS_ERR(new_root)) {
5392 err = PTR_ERR(new_root);
5393 goto out;
5394 }
5395
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005396 *sub_root = new_root;
5397 location->objectid = btrfs_root_dirid(&new_root->root_item);
5398 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005399 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005400 err = 0;
5401out:
5402 btrfs_free_path(path);
5403 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005404}
5405
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005406static void inode_tree_add(struct inode *inode)
5407{
5408 struct btrfs_root *root = BTRFS_I(inode)->root;
5409 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005410 struct rb_node **p;
5411 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005412 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005413 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005414
Al Viro1d3382cb2010-10-23 15:19:20 -04005415 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005416 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005417 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005418 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005419 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005420 while (*p) {
5421 parent = *p;
5422 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5423
Li Zefan33345d012011-04-20 10:31:50 +08005424 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005425 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005426 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005427 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005428 else {
5429 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005430 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005431 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005432 RB_CLEAR_NODE(parent);
5433 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005434 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005435 }
5436 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005437 rb_link_node(new, parent, p);
5438 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005439 spin_unlock(&root->inode_lock);
5440}
5441
5442static void inode_tree_del(struct inode *inode)
5443{
5444 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005445 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005446
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005447 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005448 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005449 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005450 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005451 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005452 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005453 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005454
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005455 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005456 synchronize_srcu(&root->fs_info->subvol_srcu);
5457 spin_lock(&root->inode_lock);
5458 empty = RB_EMPTY_ROOT(&root->inode_tree);
5459 spin_unlock(&root->inode_lock);
5460 if (empty)
5461 btrfs_add_dead_root(root);
5462 }
5463}
5464
Jeff Mahoney143bede2012-03-01 14:56:26 +01005465void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005466{
5467 struct rb_node *node;
5468 struct rb_node *prev;
5469 struct btrfs_inode *entry;
5470 struct inode *inode;
5471 u64 objectid = 0;
5472
Liu Bo7813b3d2014-02-10 17:37:25 +08005473 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5474 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005475
5476 spin_lock(&root->inode_lock);
5477again:
5478 node = root->inode_tree.rb_node;
5479 prev = NULL;
5480 while (node) {
5481 prev = node;
5482 entry = rb_entry(node, struct btrfs_inode, rb_node);
5483
Li Zefan33345d012011-04-20 10:31:50 +08005484 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005485 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005486 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005487 node = node->rb_right;
5488 else
5489 break;
5490 }
5491 if (!node) {
5492 while (prev) {
5493 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005494 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005495 node = prev;
5496 break;
5497 }
5498 prev = rb_next(prev);
5499 }
5500 }
5501 while (node) {
5502 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005503 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005504 inode = igrab(&entry->vfs_inode);
5505 if (inode) {
5506 spin_unlock(&root->inode_lock);
5507 if (atomic_read(&inode->i_count) > 1)
5508 d_prune_aliases(inode);
5509 /*
Al Viro45321ac2010-06-07 13:43:19 -04005510 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005511 * the inode cache when its usage count
5512 * hits zero.
5513 */
5514 iput(inode);
5515 cond_resched();
5516 spin_lock(&root->inode_lock);
5517 goto again;
5518 }
5519
5520 if (cond_resched_lock(&root->inode_lock))
5521 goto again;
5522
5523 node = rb_next(node);
5524 }
5525 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005526}
5527
Chris Masone02119d2008-09-05 16:13:11 -04005528static int btrfs_init_locked_inode(struct inode *inode, void *p)
5529{
5530 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005531 inode->i_ino = args->location->objectid;
5532 memcpy(&BTRFS_I(inode)->location, args->location,
5533 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005534 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005535 return 0;
5536}
5537
5538static int btrfs_find_actor(struct inode *inode, void *opaque)
5539{
5540 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005541 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005542 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005543}
5544
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005545static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005546 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005547 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005548{
5549 struct inode *inode;
5550 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005551 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005552
Chris Mason90d3e592014-01-09 17:28:00 -08005553 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005554 args.root = root;
5555
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005556 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005557 btrfs_init_locked_inode,
5558 (void *)&args);
5559 return inode;
5560}
5561
Balaji Rao1a54ef82008-07-21 02:01:04 +05305562/* Get an inode object given its location and corresponding root.
5563 * Returns in *is_new if the inode was read from disk
5564 */
5565struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005566 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305567{
5568 struct inode *inode;
5569
Chris Mason90d3e592014-01-09 17:28:00 -08005570 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305571 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005572 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305573
5574 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305575 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005576 if (!is_bad_inode(inode)) {
5577 inode_tree_add(inode);
5578 unlock_new_inode(inode);
5579 if (new)
5580 *new = 1;
5581 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005582 unlock_new_inode(inode);
5583 iput(inode);
5584 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005585 }
5586 }
5587
Balaji Rao1a54ef82008-07-21 02:01:04 +05305588 return inode;
5589}
5590
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005591static struct inode *new_simple_dir(struct super_block *s,
5592 struct btrfs_key *key,
5593 struct btrfs_root *root)
5594{
5595 struct inode *inode = new_inode(s);
5596
5597 if (!inode)
5598 return ERR_PTR(-ENOMEM);
5599
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005600 BTRFS_I(inode)->root = root;
5601 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005602 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005603
5604 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005605 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005606 inode->i_fop = &simple_dir_operations;
5607 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305608 inode->i_mtime = CURRENT_TIME;
5609 inode->i_atime = inode->i_mtime;
5610 inode->i_ctime = inode->i_mtime;
5611 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005612
5613 return inode;
5614}
5615
Chris Mason3de45862008-11-17 21:02:50 -05005616struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005617{
Chris Masond3977122009-01-05 21:25:51 -05005618 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005619 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005620 struct btrfs_root *sub_root = root;
5621 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005622 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005623 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005624
5625 if (dentry->d_name.len > BTRFS_NAME_LEN)
5626 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005627
Jeff Layton39e3c952012-11-28 11:30:53 -05005628 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005629 if (ret < 0)
5630 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005631
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005632 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005633 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005634
5635 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005636 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005637 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005638 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005639
5640 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5641
Yan, Zheng76dda932009-09-21 16:00:26 -04005642 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005643 ret = fixup_tree_root_location(root, dir, dentry,
5644 &location, &sub_root);
5645 if (ret < 0) {
5646 if (ret != -ENOENT)
5647 inode = ERR_PTR(ret);
5648 else
5649 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5650 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005651 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005652 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005653 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5654
Julia Lawall34d19ba2011-01-24 19:55:19 +00005655 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005656 down_read(&root->fs_info->cleanup_work_sem);
5657 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005658 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005659 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005660 if (ret) {
5661 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005662 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005663 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005664 }
5665
Chris Mason3de45862008-11-17 21:02:50 -05005666 return inode;
5667}
5668
Nick Pigginfe15ce42011-01-07 17:49:23 +11005669static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005670{
5671 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005672 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005673
Li Zefan848cce02012-02-21 17:04:28 +08005674 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005675 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005676
Li Zefan848cce02012-02-21 17:04:28 +08005677 if (inode) {
5678 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005679 if (btrfs_root_refs(&root->root_item) == 0)
5680 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005681
5682 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5683 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005684 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005685 return 0;
5686}
5687
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005688static void btrfs_dentry_release(struct dentry *dentry)
5689{
Daeseok Youn944a4512014-04-14 15:37:02 +09005690 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005691}
5692
Chris Mason3de45862008-11-17 21:02:50 -05005693static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005694 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005695{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005696 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005697
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005698 inode = btrfs_lookup_dentry(dir, dentry);
5699 if (IS_ERR(inode)) {
5700 if (PTR_ERR(inode) == -ENOENT)
5701 inode = NULL;
5702 else
5703 return ERR_CAST(inode);
5704 }
5705
Al Viro41d28bc2014-10-12 22:24:21 -04005706 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005707}
5708
Miao Xie16cdcec2011-04-22 18:12:22 +08005709unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005710 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5711};
5712
Al Viro9cdda8d2013-05-22 16:48:09 -04005713static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005714{
Al Viro9cdda8d2013-05-22 16:48:09 -04005715 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005716 struct btrfs_root *root = BTRFS_I(inode)->root;
5717 struct btrfs_item *item;
5718 struct btrfs_dir_item *di;
5719 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005720 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005721 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005722 struct list_head ins_list;
5723 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005724 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005725 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005726 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005727 unsigned char d_type;
5728 int over = 0;
5729 u32 di_cur;
5730 u32 di_total;
5731 u32 di_len;
5732 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005733 char tmp_name[32];
5734 char *name_ptr;
5735 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005736 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005737
5738 /* FIXME, use a real flag for deciding about the key type */
5739 if (root->fs_info->tree_root == root)
5740 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005741
Al Viro9cdda8d2013-05-22 16:48:09 -04005742 if (!dir_emit_dots(file, ctx))
5743 return 0;
5744
David Woodhouse49593bf2008-08-17 17:08:36 +01005745 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005746 if (!path)
5747 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005748
Josef Bacik026fd312011-05-13 10:32:11 -04005749 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005750
Miao Xie16cdcec2011-04-22 18:12:22 +08005751 if (key_type == BTRFS_DIR_INDEX_KEY) {
5752 INIT_LIST_HEAD(&ins_list);
5753 INIT_LIST_HEAD(&del_list);
5754 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5755 }
5756
David Sterba962a2982014-06-04 18:41:45 +02005757 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005758 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005759 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005760
Chris Mason39279cc2007-06-12 06:35:45 -04005761 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5762 if (ret < 0)
5763 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005764
5765 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005766 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005767 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005768 if (slot >= btrfs_header_nritems(leaf)) {
5769 ret = btrfs_next_leaf(root, path);
5770 if (ret < 0)
5771 goto err;
5772 else if (ret > 0)
5773 break;
5774 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005775 }
Chris Mason3de45862008-11-17 21:02:50 -05005776
Ross Kirkdd3cc162013-09-16 15:58:09 +01005777 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005778 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5779
5780 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005781 break;
David Sterba962a2982014-06-04 18:41:45 +02005782 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005783 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005784 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005785 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005786 if (key_type == BTRFS_DIR_INDEX_KEY &&
5787 btrfs_should_delete_dir_index(&del_list,
5788 found_key.offset))
5789 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005790
Al Viro9cdda8d2013-05-22 16:48:09 -04005791 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005792 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005793
Chris Mason39279cc2007-06-12 06:35:45 -04005794 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5795 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005796 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005797
5798 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005799 struct btrfs_key location;
5800
Josef Bacik22a94d42011-03-16 16:47:17 -04005801 if (verify_dir_item(root, leaf, di))
5802 break;
5803
Chris Mason5f39d392007-10-15 16:14:19 -04005804 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005805 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005806 name_ptr = tmp_name;
5807 } else {
5808 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005809 if (!name_ptr) {
5810 ret = -ENOMEM;
5811 goto err;
5812 }
Chris Mason5f39d392007-10-15 16:14:19 -04005813 }
5814 read_extent_buffer(leaf, name_ptr,
5815 (unsigned long)(di + 1), name_len);
5816
5817 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5818 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005819
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005820
Chris Mason3de45862008-11-17 21:02:50 -05005821 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005822 * skip it.
5823 *
5824 * In contrast to old kernels, we insert the snapshot's
5825 * dir item and dir index after it has been created, so
5826 * we won't find a reference to our own snapshot. We
5827 * still keep the following code for backward
5828 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005829 */
5830 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5831 location.objectid == root->root_key.objectid) {
5832 over = 0;
5833 goto skip;
5834 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005835 over = !dir_emit(ctx, name_ptr, name_len,
5836 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005837
Chris Mason3de45862008-11-17 21:02:50 -05005838skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005839 if (name_ptr != tmp_name)
5840 kfree(name_ptr);
5841
Chris Mason39279cc2007-06-12 06:35:45 -04005842 if (over)
5843 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005844 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005845 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005846 di_cur += di_len;
5847 di = (struct btrfs_dir_item *)((char *)di + di_len);
5848 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005849next:
5850 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005851 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005852
Miao Xie16cdcec2011-04-22 18:12:22 +08005853 if (key_type == BTRFS_DIR_INDEX_KEY) {
5854 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005855 ctx->pos++;
5856 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005857 if (ret)
5858 goto nopos;
5859 }
5860
David Woodhouse49593bf2008-08-17 17:08:36 +01005861 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005862 ctx->pos++;
5863
5864 /*
5865 * Stop new entries from being returned after we return the last
5866 * entry.
5867 *
5868 * New directory entries are assigned a strictly increasing
5869 * offset. This means that new entries created during readdir
5870 * are *guaranteed* to be seen in the future by that readdir.
5871 * This has broken buggy programs which operate on names as
5872 * they're returned by readdir. Until we re-use freed offsets
5873 * we have this hack to stop new entries from being returned
5874 * under the assumption that they'll never reach this huge
5875 * offset.
5876 *
5877 * This is being careful not to overflow 32bit loff_t unless the
5878 * last entry requires it because doing so has broken 32bit apps
5879 * in the past.
5880 */
5881 if (key_type == BTRFS_DIR_INDEX_KEY) {
5882 if (ctx->pos >= INT_MAX)
5883 ctx->pos = LLONG_MAX;
5884 else
5885 ctx->pos = INT_MAX;
5886 }
Chris Mason39279cc2007-06-12 06:35:45 -04005887nopos:
5888 ret = 0;
5889err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005890 if (key_type == BTRFS_DIR_INDEX_KEY)
5891 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005892 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005893 return ret;
5894}
5895
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005896int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005897{
5898 struct btrfs_root *root = BTRFS_I(inode)->root;
5899 struct btrfs_trans_handle *trans;
5900 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005901 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005902
Josef Bacik72ac3c02012-05-23 14:13:11 -04005903 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005904 return 0;
5905
Liu Bo83eea1f2012-07-10 05:28:39 -06005906 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005907 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005908
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005909 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005910 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005911 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005912 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005913 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005914 if (IS_ERR(trans))
5915 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005916 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005917 }
5918 return ret;
5919}
5920
5921/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005922 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005923 * inode changes. But, it is most likely to find the inode in cache.
5924 * FIXME, needs more benchmarking...there are no reasons other than performance
5925 * to keep or drop this code.
5926 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005927static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005928{
5929 struct btrfs_root *root = BTRFS_I(inode)->root;
5930 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005931 int ret;
5932
Josef Bacik72ac3c02012-05-23 14:13:11 -04005933 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005934 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005935
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005936 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005937 if (IS_ERR(trans))
5938 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005939
5940 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005941 if (ret && ret == -ENOSPC) {
5942 /* whoops, lets try again with the full transaction */
5943 btrfs_end_transaction(trans, root);
5944 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005945 if (IS_ERR(trans))
5946 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005947
Chris Mason94b60442010-05-26 11:02:00 -04005948 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005949 }
Chris Mason39279cc2007-06-12 06:35:45 -04005950 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005951 if (BTRFS_I(inode)->delayed_node)
5952 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005953
5954 return ret;
5955}
5956
5957/*
5958 * This is a copy of file_update_time. We need this so we can return error on
5959 * ENOSPC for updating the inode in the case of file write and mmap writes.
5960 */
Josef Bacike41f9412012-03-26 09:46:47 -04005961static int btrfs_update_time(struct inode *inode, struct timespec *now,
5962 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005963{
Alexander Block2bc5565282012-06-15 09:49:33 +02005964 struct btrfs_root *root = BTRFS_I(inode)->root;
5965
5966 if (btrfs_root_readonly(root))
5967 return -EROFS;
5968
Josef Bacike41f9412012-03-26 09:46:47 -04005969 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005970 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005971 if (flags & S_CTIME)
5972 inode->i_ctime = *now;
5973 if (flags & S_MTIME)
5974 inode->i_mtime = *now;
5975 if (flags & S_ATIME)
5976 inode->i_atime = *now;
5977 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005978}
5979
Chris Masond352ac62008-09-29 15:18:18 -04005980/*
5981 * find the highest existing sequence number in a directory
5982 * and then set the in-memory index_cnt variable to reflect
5983 * free sequence numbers
5984 */
Josef Bacikaec74772008-07-24 12:12:38 -04005985static int btrfs_set_inode_index_count(struct inode *inode)
5986{
5987 struct btrfs_root *root = BTRFS_I(inode)->root;
5988 struct btrfs_key key, found_key;
5989 struct btrfs_path *path;
5990 struct extent_buffer *leaf;
5991 int ret;
5992
Li Zefan33345d012011-04-20 10:31:50 +08005993 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005994 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005995 key.offset = (u64)-1;
5996
5997 path = btrfs_alloc_path();
5998 if (!path)
5999 return -ENOMEM;
6000
6001 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6002 if (ret < 0)
6003 goto out;
6004 /* FIXME: we should be able to handle this */
6005 if (ret == 0)
6006 goto out;
6007 ret = 0;
6008
6009 /*
6010 * MAGIC NUMBER EXPLANATION:
6011 * since we search a directory based on f_pos we have to start at 2
6012 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6013 * else has to start at 2
6014 */
6015 if (path->slots[0] == 0) {
6016 BTRFS_I(inode)->index_cnt = 2;
6017 goto out;
6018 }
6019
6020 path->slots[0]--;
6021
6022 leaf = path->nodes[0];
6023 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6024
Li Zefan33345d012011-04-20 10:31:50 +08006025 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006026 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006027 BTRFS_I(inode)->index_cnt = 2;
6028 goto out;
6029 }
6030
6031 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6032out:
6033 btrfs_free_path(path);
6034 return ret;
6035}
6036
Chris Masond352ac62008-09-29 15:18:18 -04006037/*
6038 * helper to find a free sequence number in a given directory. This current
6039 * code is very simple, later versions will do smarter things in the btree
6040 */
Chris Mason3de45862008-11-17 21:02:50 -05006041int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006042{
6043 int ret = 0;
6044
6045 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006046 ret = btrfs_inode_delayed_dir_index_count(dir);
6047 if (ret) {
6048 ret = btrfs_set_inode_index_count(dir);
6049 if (ret)
6050 return ret;
6051 }
Josef Bacikaec74772008-07-24 12:12:38 -04006052 }
6053
Chris Mason00e4e6b2008-08-05 11:18:09 -04006054 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006055 BTRFS_I(dir)->index_cnt++;
6056
6057 return ret;
6058}
6059
Chris Masonb0d5d102014-09-08 13:08:51 -07006060static int btrfs_insert_inode_locked(struct inode *inode)
6061{
6062 struct btrfs_iget_args args;
6063 args.location = &BTRFS_I(inode)->location;
6064 args.root = BTRFS_I(inode)->root;
6065
6066 return insert_inode_locked4(inode,
6067 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6068 btrfs_find_actor, &args);
6069}
6070
Chris Mason39279cc2007-06-12 06:35:45 -04006071static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6072 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006073 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006074 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006075 u64 ref_objectid, u64 objectid,
6076 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006077{
6078 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006079 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006080 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006081 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006082 struct btrfs_inode_ref *ref;
6083 struct btrfs_key key[2];
6084 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006085 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006086 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006087 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006088
Chris Mason5f39d392007-10-15 16:14:19 -04006089 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006090 if (!path)
6091 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006092
Chris Mason39279cc2007-06-12 06:35:45 -04006093 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006094 if (!inode) {
6095 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006096 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006097 }
Chris Mason39279cc2007-06-12 06:35:45 -04006098
Li Zefan581bb052011-04-20 10:06:11 +08006099 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006100 * O_TMPFILE, set link count to 0, so that after this point,
6101 * we fill in an inode item with the correct link count.
6102 */
6103 if (!name)
6104 set_nlink(inode, 0);
6105
6106 /*
Li Zefan581bb052011-04-20 10:06:11 +08006107 * we have to initialize this early, so we can reclaim the inode
6108 * number if we fail afterwards in this function.
6109 */
6110 inode->i_ino = objectid;
6111
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006112 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006113 trace_btrfs_inode_request(dir);
6114
Chris Mason3de45862008-11-17 21:02:50 -05006115 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006116 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006117 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006118 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006119 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006120 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006121 } else if (dir) {
6122 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006123 }
6124 /*
6125 * index_cnt is ignored for everything but a dir,
6126 * btrfs_get_inode_index_count has an explanation for the magic
6127 * number
6128 */
6129 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006130 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006131 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006132 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006133 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006134
Josef Bacik5dc562c2012-08-17 13:14:17 -04006135 /*
6136 * We could have gotten an inode number from somebody who was fsynced
6137 * and then removed in this same transaction, so let's just set full
6138 * sync since it will be a full sync anyway and this will blow away the
6139 * old info in the log.
6140 */
6141 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6142
Chris Mason9c583092008-01-29 15:15:18 -05006143 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006144 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006145 key[0].offset = 0;
6146
Chris Mason9c583092008-01-29 15:15:18 -05006147 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006148
6149 if (name) {
6150 /*
6151 * Start new inodes with an inode_ref. This is slightly more
6152 * efficient for small numbers of hard links since they will
6153 * be packed into one item. Extended refs will kick in if we
6154 * add more hard links than can fit in the ref item.
6155 */
6156 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006157 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006158 key[1].offset = ref_objectid;
6159
6160 sizes[1] = name_len + sizeof(*ref);
6161 }
Chris Mason9c583092008-01-29 15:15:18 -05006162
Chris Masonb0d5d102014-09-08 13:08:51 -07006163 location = &BTRFS_I(inode)->location;
6164 location->objectid = objectid;
6165 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006166 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006167
6168 ret = btrfs_insert_inode_locked(inode);
6169 if (ret < 0)
6170 goto fail;
6171
Chris Masonb9473432009-03-13 11:00:37 -04006172 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006173 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006174 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006175 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006176
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006177 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006178 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306179
6180 inode->i_mtime = CURRENT_TIME;
6181 inode->i_atime = inode->i_mtime;
6182 inode->i_ctime = inode->i_mtime;
6183 BTRFS_I(inode)->i_otime = inode->i_mtime;
6184
Chris Mason5f39d392007-10-15 16:14:19 -04006185 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6186 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006187 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6188 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006189 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006190
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006191 if (name) {
6192 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6193 struct btrfs_inode_ref);
6194 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6195 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6196 ptr = (unsigned long)(ref + 1);
6197 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6198 }
Chris Mason9c583092008-01-29 15:15:18 -05006199
Chris Mason5f39d392007-10-15 16:14:19 -04006200 btrfs_mark_buffer_dirty(path->nodes[0]);
6201 btrfs_free_path(path);
6202
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006203 btrfs_inherit_iflags(inode, dir);
6204
Al Viro569254b2011-07-24 17:08:40 -04006205 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04006206 if (btrfs_test_opt(root, NODATASUM))
6207 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06006208 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006209 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6210 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006211 }
6212
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006213 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006214
6215 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006216 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006217
Alexander Block8ea05e32012-07-25 17:35:53 +02006218 btrfs_update_root_times(trans, root);
6219
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006220 ret = btrfs_inode_inherit_props(trans, inode, dir);
6221 if (ret)
6222 btrfs_err(root->fs_info,
6223 "error inheriting props for ino %llu (root %llu): %d",
6224 btrfs_ino(inode), root->root_key.objectid, ret);
6225
Chris Mason39279cc2007-06-12 06:35:45 -04006226 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006227
6228fail_unlock:
6229 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006230fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006231 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006232 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006233 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006234 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006235 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006236}
6237
6238static inline u8 btrfs_inode_type(struct inode *inode)
6239{
6240 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6241}
6242
Chris Masond352ac62008-09-29 15:18:18 -04006243/*
6244 * utility function to add 'inode' into 'parent_inode' with
6245 * a give name and a given sequence number.
6246 * if 'add_backref' is true, also insert a backref from the
6247 * inode to the parent directory.
6248 */
Chris Masone02119d2008-09-05 16:13:11 -04006249int btrfs_add_link(struct btrfs_trans_handle *trans,
6250 struct inode *parent_inode, struct inode *inode,
6251 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006252{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006253 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006254 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006255 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006256 u64 ino = btrfs_ino(inode);
6257 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006258
Li Zefan33345d012011-04-20 10:31:50 +08006259 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006260 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6261 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006262 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006263 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006264 key.offset = 0;
6265 }
Chris Mason39279cc2007-06-12 06:35:45 -04006266
Li Zefan33345d012011-04-20 10:31:50 +08006267 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006268 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6269 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006270 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006271 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006272 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6273 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006274 }
6275
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006276 /* Nothing to clean up yet */
6277 if (ret)
6278 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006279
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006280 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6281 parent_inode, &key,
6282 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006283 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006284 goto fail_dir_item;
6285 else if (ret) {
6286 btrfs_abort_transaction(trans, root, ret);
6287 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006288 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006289
6290 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6291 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006292 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006293 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6294 ret = btrfs_update_inode(trans, root, parent_inode);
6295 if (ret)
6296 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006297 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006298
6299fail_dir_item:
6300 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6301 u64 local_index;
6302 int err;
6303 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6304 key.objectid, root->root_key.objectid,
6305 parent_ino, &local_index, name, name_len);
6306
6307 } else if (add_backref) {
6308 u64 local_index;
6309 int err;
6310
6311 err = btrfs_del_inode_ref(trans, root, name, name_len,
6312 ino, parent_ino, &local_index);
6313 }
6314 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006315}
6316
6317static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006318 struct inode *dir, struct dentry *dentry,
6319 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006320{
Josef Bacika1b075d2010-11-19 20:36:11 +00006321 int err = btrfs_add_link(trans, dir, inode,
6322 dentry->d_name.name, dentry->d_name.len,
6323 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006324 if (err > 0)
6325 err = -EEXIST;
6326 return err;
6327}
6328
Josef Bacik618e21d2007-07-11 10:18:17 -04006329static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006330 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006331{
6332 struct btrfs_trans_handle *trans;
6333 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006334 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006335 int err;
6336 int drop_inode = 0;
6337 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006338 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006339
6340 if (!new_valid_dev(rdev))
6341 return -EINVAL;
6342
Josef Bacik9ed74f22009-09-11 16:12:44 -04006343 /*
6344 * 2 for inode item and ref
6345 * 2 for dir items
6346 * 1 for xattr if selinux is on
6347 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006348 trans = btrfs_start_transaction(root, 5);
6349 if (IS_ERR(trans))
6350 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006351
Li Zefan581bb052011-04-20 10:06:11 +08006352 err = btrfs_find_free_ino(root, &objectid);
6353 if (err)
6354 goto out_unlock;
6355
Josef Bacikaec74772008-07-24 12:12:38 -04006356 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006357 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006358 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006359 if (IS_ERR(inode)) {
6360 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006361 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006362 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006363
Casey Schauflerad19db72011-12-15 10:09:07 -05006364 /*
6365 * If the active LSM wants to access the inode during
6366 * d_instantiate it needs these. Smack checks to see
6367 * if the filesystem supports xattrs by looking at the
6368 * ops vector.
6369 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006370 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006371 init_special_inode(inode, inode->i_mode, rdev);
6372
6373 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006374 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006375 goto out_unlock_inode;
6376
6377 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6378 if (err) {
6379 goto out_unlock_inode;
6380 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006381 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006382 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006383 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006384 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006385
Josef Bacik618e21d2007-07-11 10:18:17 -04006386out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006387 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006388 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006389 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006390 if (drop_inode) {
6391 inode_dec_link_count(inode);
6392 iput(inode);
6393 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006394 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006395
6396out_unlock_inode:
6397 drop_inode = 1;
6398 unlock_new_inode(inode);
6399 goto out_unlock;
6400
Josef Bacik618e21d2007-07-11 10:18:17 -04006401}
6402
Chris Mason39279cc2007-06-12 06:35:45 -04006403static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006404 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006405{
6406 struct btrfs_trans_handle *trans;
6407 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006408 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006409 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006410 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006411 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006412 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006413
Josef Bacik9ed74f22009-09-11 16:12:44 -04006414 /*
6415 * 2 for inode item and ref
6416 * 2 for dir items
6417 * 1 for xattr if selinux is on
6418 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006419 trans = btrfs_start_transaction(root, 5);
6420 if (IS_ERR(trans))
6421 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006422
Li Zefan581bb052011-04-20 10:06:11 +08006423 err = btrfs_find_free_ino(root, &objectid);
6424 if (err)
6425 goto out_unlock;
6426
Josef Bacikaec74772008-07-24 12:12:38 -04006427 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006428 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006429 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006430 if (IS_ERR(inode)) {
6431 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006432 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006433 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006434 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006435 /*
6436 * If the active LSM wants to access the inode during
6437 * d_instantiate it needs these. Smack checks to see
6438 * if the filesystem supports xattrs by looking at the
6439 * ops vector.
6440 */
6441 inode->i_fop = &btrfs_file_operations;
6442 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006443 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006444
6445 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6446 if (err)
6447 goto out_unlock_inode;
6448
6449 err = btrfs_update_inode(trans, root, inode);
6450 if (err)
6451 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006452
Josef Bacika1b075d2010-11-19 20:36:11 +00006453 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006454 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006455 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006456
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006457 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006458 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006459 d_instantiate(dentry, inode);
6460
Chris Mason39279cc2007-06-12 06:35:45 -04006461out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006462 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006463 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006464 inode_dec_link_count(inode);
6465 iput(inode);
6466 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006467 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006468 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006469 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006470
6471out_unlock_inode:
6472 unlock_new_inode(inode);
6473 goto out_unlock;
6474
Chris Mason39279cc2007-06-12 06:35:45 -04006475}
6476
6477static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6478 struct dentry *dentry)
6479{
6480 struct btrfs_trans_handle *trans;
6481 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006482 struct inode *inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006483 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006484 int err;
6485 int drop_inode = 0;
6486
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006487 /* do not allow sys_link's with other subvols of the same device */
6488 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006489 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006490
Mark Fashehf1863732012-08-08 11:32:27 -07006491 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006492 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006493
Chris Mason3de45862008-11-17 21:02:50 -05006494 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006495 if (err)
6496 goto fail;
6497
Yan, Zhenga22285a2010-05-16 10:48:46 -04006498 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006499 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006500 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006501 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006502 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006503 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006504 if (IS_ERR(trans)) {
6505 err = PTR_ERR(trans);
6506 goto fail;
6507 }
Chris Mason5f39d392007-10-15 16:14:19 -04006508
Miao Xie67de1172013-12-26 13:07:06 +08006509 /* There are several dir indexes for this inode, clear the cache. */
6510 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006511 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006512 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006513 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006514 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006515 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006516
Josef Bacika1b075d2010-11-19 20:36:11 +00006517 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006518
Yan, Zhenga5719522009-09-24 09:17:31 -04006519 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006520 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006521 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006522 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006523 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006524 if (err)
6525 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006526 if (inode->i_nlink == 1) {
6527 /*
6528 * If new hard link count is 1, it's a file created
6529 * with open(2) O_TMPFILE flag.
6530 */
6531 err = btrfs_orphan_del(trans, inode);
6532 if (err)
6533 goto fail;
6534 }
Al Viro08c422c2011-12-23 07:58:13 -05006535 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006536 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006537 }
Chris Mason39279cc2007-06-12 06:35:45 -04006538
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006539 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006540 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006541fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006542 if (drop_inode) {
6543 inode_dec_link_count(inode);
6544 iput(inode);
6545 }
Liu Bob53d3f52012-11-14 14:34:34 +00006546 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006547 return err;
6548}
6549
Al Viro18bb1db2011-07-26 01:41:39 -04006550static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006551{
Chris Masonb9d86662008-05-02 16:13:49 -04006552 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006553 struct btrfs_trans_handle *trans;
6554 struct btrfs_root *root = BTRFS_I(dir)->root;
6555 int err = 0;
6556 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006557 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006558 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006559
Josef Bacik9ed74f22009-09-11 16:12:44 -04006560 /*
6561 * 2 items for inode and ref
6562 * 2 items for dir items
6563 * 1 for xattr if selinux is on
6564 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006565 trans = btrfs_start_transaction(root, 5);
6566 if (IS_ERR(trans))
6567 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006568
Li Zefan581bb052011-04-20 10:06:11 +08006569 err = btrfs_find_free_ino(root, &objectid);
6570 if (err)
6571 goto out_fail;
6572
Josef Bacikaec74772008-07-24 12:12:38 -04006573 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006574 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006575 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006576 if (IS_ERR(inode)) {
6577 err = PTR_ERR(inode);
6578 goto out_fail;
6579 }
Chris Mason5f39d392007-10-15 16:14:19 -04006580
Chris Mason39279cc2007-06-12 06:35:45 -04006581 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006582 /* these must be set before we unlock the inode */
6583 inode->i_op = &btrfs_dir_inode_operations;
6584 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006585
Eric Paris2a7dba32011-02-01 11:05:39 -05006586 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006587 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006588 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006589
Chris Masondbe674a2008-07-17 12:54:05 -04006590 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006591 err = btrfs_update_inode(trans, root, inode);
6592 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006593 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006594
Josef Bacika1b075d2010-11-19 20:36:11 +00006595 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6596 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006597 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006598 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006599
Chris Mason39279cc2007-06-12 06:35:45 -04006600 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006601 /*
6602 * mkdir is special. We're unlocking after we call d_instantiate
6603 * to avoid a race with nfsd calling d_instantiate.
6604 */
6605 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006606 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006607
6608out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006609 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006610 if (drop_on_err) {
6611 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006612 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006613 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006614 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006615 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006616 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006617
6618out_fail_inode:
6619 unlock_new_inode(inode);
6620 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006621}
6622
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006623/* Find next extent map of a given extent map, caller needs to ensure locks */
6624static struct extent_map *next_extent_map(struct extent_map *em)
6625{
6626 struct rb_node *next;
6627
6628 next = rb_next(&em->rb_node);
6629 if (!next)
6630 return NULL;
6631 return container_of(next, struct extent_map, rb_node);
6632}
6633
6634static struct extent_map *prev_extent_map(struct extent_map *em)
6635{
6636 struct rb_node *prev;
6637
6638 prev = rb_prev(&em->rb_node);
6639 if (!prev)
6640 return NULL;
6641 return container_of(prev, struct extent_map, rb_node);
6642}
6643
Chris Masond352ac62008-09-29 15:18:18 -04006644/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006645 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006646 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006647 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006648 */
Chris Mason3b951512008-04-17 11:29:12 -04006649static int merge_extent_mapping(struct extent_map_tree *em_tree,
6650 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006651 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006652 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006653{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006654 struct extent_map *prev;
6655 struct extent_map *next;
6656 u64 start;
6657 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006658 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006659
Chris Masone6dcd2d2008-07-17 12:53:50 -04006660 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006661
6662 if (existing->start > map_start) {
6663 next = existing;
6664 prev = prev_extent_map(next);
6665 } else {
6666 prev = existing;
6667 next = next_extent_map(prev);
6668 }
6669
6670 start = prev ? extent_map_end(prev) : em->start;
6671 start = max_t(u64, start, em->start);
6672 end = next ? next->start : extent_map_end(em);
6673 end = min_t(u64, end, extent_map_end(em));
6674 start_diff = start - em->start;
6675 em->start = start;
6676 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006677 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6678 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006679 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006680 em->block_len -= start_diff;
6681 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006682 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006683}
6684
Chris Masonc8b97812008-10-29 14:49:59 -04006685static noinline int uncompress_inline(struct btrfs_path *path,
6686 struct inode *inode, struct page *page,
6687 size_t pg_offset, u64 extent_offset,
6688 struct btrfs_file_extent_item *item)
6689{
6690 int ret;
6691 struct extent_buffer *leaf = path->nodes[0];
6692 char *tmp;
6693 size_t max_size;
6694 unsigned long inline_size;
6695 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006696 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006697
6698 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006699 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006700 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6701 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006702 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006703 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006704 if (!tmp)
6705 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006706 ptr = btrfs_file_extent_inline_start(item);
6707
6708 read_extent_buffer(leaf, tmp, ptr, inline_size);
6709
Chris Mason5b050f02008-11-11 09:34:41 -05006710 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006711 ret = btrfs_decompress(compress_type, tmp, page,
6712 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006713 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006714 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006715}
6716
Chris Masond352ac62008-09-29 15:18:18 -04006717/*
6718 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006719 * the ugly parts come from merging extents from the disk with the in-ram
6720 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006721 * where the in-ram extents might be locked pending data=ordered completion.
6722 *
6723 * This also copies inline extents directly into the page.
6724 */
Chris Masond3977122009-01-05 21:25:51 -05006725
Chris Masona52d9a82007-08-27 16:49:44 -04006726struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006727 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006728 int create)
6729{
6730 int ret;
6731 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006732 u64 extent_start = 0;
6733 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006734 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006735 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006736 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006737 struct btrfs_root *root = BTRFS_I(inode)->root;
6738 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006739 struct extent_buffer *leaf;
6740 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006741 struct extent_map *em = NULL;
6742 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006743 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006744 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006745 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006746
Chris Masona52d9a82007-08-27 16:49:44 -04006747again:
Chris Mason890871b2009-09-02 16:24:52 -04006748 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006749 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006750 if (em)
6751 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006752 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006753
Chris Masona52d9a82007-08-27 16:49:44 -04006754 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006755 if (em->start > start || em->start + em->len <= start)
6756 free_extent_map(em);
6757 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006758 free_extent_map(em);
6759 else
6760 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006761 }
David Sterba172ddd62011-04-21 00:48:27 +02006762 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006763 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006764 err = -ENOMEM;
6765 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006766 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006767 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006768 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006769 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006770 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006771 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006772
6773 if (!path) {
6774 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006775 if (!path) {
6776 err = -ENOMEM;
6777 goto out;
6778 }
6779 /*
6780 * Chances are we'll be called again, so go ahead and do
6781 * readahead
6782 */
6783 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006784 }
6785
Chris Mason179e29e2007-11-01 11:28:41 -04006786 ret = btrfs_lookup_file_extent(trans, root, path,
6787 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006788 if (ret < 0) {
6789 err = ret;
6790 goto out;
6791 }
6792
6793 if (ret != 0) {
6794 if (path->slots[0] == 0)
6795 goto not_found;
6796 path->slots[0]--;
6797 }
6798
Chris Mason5f39d392007-10-15 16:14:19 -04006799 leaf = path->nodes[0];
6800 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006801 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006802 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006803 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006804 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006805 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006806 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006807 /*
6808 * If we backup past the first extent we want to move forward
6809 * and see if there is an extent in front of us, otherwise we'll
6810 * say there is a hole for our whole search range which can
6811 * cause problems.
6812 */
6813 extent_end = start;
6814 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006815 }
6816
Chris Mason5f39d392007-10-15 16:14:19 -04006817 found_type = btrfs_file_extent_type(leaf, item);
6818 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006819 if (found_type == BTRFS_FILE_EXTENT_REG ||
6820 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006821 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006822 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006823 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6824 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006825 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006826 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006827 }
Josef Bacik25a50342013-10-14 12:08:38 -04006828next:
Yan Zheng9036c102008-10-30 14:19:41 -04006829 if (start >= extent_end) {
6830 path->slots[0]++;
6831 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6832 ret = btrfs_next_leaf(root, path);
6833 if (ret < 0) {
6834 err = ret;
6835 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006836 }
Yan Zheng9036c102008-10-30 14:19:41 -04006837 if (ret > 0)
6838 goto not_found;
6839 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006840 }
Yan Zheng9036c102008-10-30 14:19:41 -04006841 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6842 if (found_key.objectid != objectid ||
6843 found_key.type != BTRFS_EXTENT_DATA_KEY)
6844 goto not_found;
6845 if (start + len <= found_key.offset)
6846 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006847 if (start > found_key.offset)
6848 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006849 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006850 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006851 em->len = found_key.offset - start;
6852 goto not_found_em;
6853 }
6854
Filipe Manana7ffbb592014-06-09 03:48:05 +01006855 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6856
Yan Zhengd899e052008-10-30 14:25:28 -04006857 if (found_type == BTRFS_FILE_EXTENT_REG ||
6858 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006859 goto insert;
6860 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006861 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006862 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006863 size_t size;
6864 size_t extent_offset;
6865 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006866
Filipe Manana7ffbb592014-06-09 03:48:05 +01006867 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006868 goto out;
Yan689f9342007-10-29 11:41:07 -04006869
Chris Mason514ac8a2014-01-03 21:07:00 -08006870 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006871 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006872 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006873 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006874 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006875 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006876 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006877 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006878 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006879 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006880 if (btrfs_file_extent_compression(leaf, item) !=
6881 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006882 ret = uncompress_inline(path, inode, page,
6883 pg_offset,
6884 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006885 if (ret) {
6886 err = ret;
6887 goto out;
6888 }
Chris Masonc8b97812008-10-29 14:49:59 -04006889 } else {
6890 map = kmap(page);
6891 read_extent_buffer(leaf, map + pg_offset, ptr,
6892 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006893 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6894 memset(map + pg_offset + copy_size, 0,
6895 PAGE_CACHE_SIZE - pg_offset -
6896 copy_size);
6897 }
Chris Masonc8b97812008-10-29 14:49:59 -04006898 kunmap(page);
6899 }
Chris Mason179e29e2007-11-01 11:28:41 -04006900 flush_dcache_page(page);
6901 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006902 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006903 if (!trans) {
6904 kunmap(page);
6905 free_extent_map(em);
6906 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006907
David Sterbab3b4aa72011-04-21 01:20:15 +02006908 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006909 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006910
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006911 if (IS_ERR(trans))
6912 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006913 goto again;
6914 }
Chris Masonc8b97812008-10-29 14:49:59 -04006915 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006916 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006917 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006918 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006919 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006920 }
Chris Masond1310b22008-01-24 16:13:08 -05006921 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006922 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006923 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006924 }
6925not_found:
6926 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006927 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006928 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006929not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006930 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006931 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006932insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006933 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006934 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006935 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006936 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006937 err = -EIO;
6938 goto out;
6939 }
Chris Masond1310b22008-01-24 16:13:08 -05006940
6941 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006942 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006943 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006944 /* it is possible that someone inserted the extent into the tree
6945 * while we had the lock dropped. It is also possible that
6946 * an overlapping map exists in the tree
6947 */
Chris Masona52d9a82007-08-27 16:49:44 -04006948 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006949 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006950
6951 ret = 0;
6952
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006953 existing = search_extent_mapping(em_tree, start, len);
6954 /*
6955 * existing will always be non-NULL, since there must be
6956 * extent causing the -EEXIST.
6957 */
6958 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006959 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006960 /*
6961 * The existing extent map is the one nearest to
6962 * the [start, start + len) range which overlaps
6963 */
6964 err = merge_extent_mapping(em_tree, existing,
6965 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006966 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006967 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006968 free_extent_map(em);
6969 em = NULL;
6970 }
6971 } else {
6972 free_extent_map(em);
6973 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006974 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006975 }
Chris Masona52d9a82007-08-27 16:49:44 -04006976 }
Chris Mason890871b2009-09-02 16:24:52 -04006977 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006978out:
liubo1abe9b82011-03-24 11:18:59 +00006979
Steven Rostedt4cd85872013-11-14 22:57:29 -05006980 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006981
Tsutomu Itoh527afb42015-08-19 14:55:00 +09006982 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006983 if (trans) {
6984 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006985 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006986 err = ret;
6987 }
Chris Masona52d9a82007-08-27 16:49:44 -04006988 if (err) {
6989 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006990 return ERR_PTR(err);
6991 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006992 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006993 return em;
6994}
6995
Chris Masonec29ed52011-02-23 16:23:20 -05006996struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6997 size_t pg_offset, u64 start, u64 len,
6998 int create)
6999{
7000 struct extent_map *em;
7001 struct extent_map *hole_em = NULL;
7002 u64 range_start = start;
7003 u64 end;
7004 u64 found;
7005 u64 found_end;
7006 int err = 0;
7007
7008 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7009 if (IS_ERR(em))
7010 return em;
7011 if (em) {
7012 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007013 * if our em maps to
7014 * - a hole or
7015 * - a pre-alloc extent,
7016 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007017 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007018 if (em->block_start != EXTENT_MAP_HOLE &&
7019 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007020 return em;
7021 else
7022 hole_em = em;
7023 }
7024
7025 /* check to see if we've wrapped (len == -1 or similar) */
7026 end = start + len;
7027 if (end < start)
7028 end = (u64)-1;
7029 else
7030 end -= 1;
7031
7032 em = NULL;
7033
7034 /* ok, we didn't find anything, lets look for delalloc */
7035 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7036 end, len, EXTENT_DELALLOC, 1);
7037 found_end = range_start + found;
7038 if (found_end < range_start)
7039 found_end = (u64)-1;
7040
7041 /*
7042 * we didn't find anything useful, return
7043 * the original results from get_extent()
7044 */
7045 if (range_start > end || found_end <= start) {
7046 em = hole_em;
7047 hole_em = NULL;
7048 goto out;
7049 }
7050
7051 /* adjust the range_start to make sure it doesn't
7052 * go backwards from the start they passed in
7053 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307054 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007055 found = found_end - range_start;
7056
7057 if (found > 0) {
7058 u64 hole_start = start;
7059 u64 hole_len = len;
7060
David Sterba172ddd62011-04-21 00:48:27 +02007061 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007062 if (!em) {
7063 err = -ENOMEM;
7064 goto out;
7065 }
7066 /*
7067 * when btrfs_get_extent can't find anything it
7068 * returns one huge hole
7069 *
7070 * make sure what it found really fits our range, and
7071 * adjust to make sure it is based on the start from
7072 * the caller
7073 */
7074 if (hole_em) {
7075 u64 calc_end = extent_map_end(hole_em);
7076
7077 if (calc_end <= start || (hole_em->start > end)) {
7078 free_extent_map(hole_em);
7079 hole_em = NULL;
7080 } else {
7081 hole_start = max(hole_em->start, start);
7082 hole_len = calc_end - hole_start;
7083 }
7084 }
7085 em->bdev = NULL;
7086 if (hole_em && range_start > hole_start) {
7087 /* our hole starts before our delalloc, so we
7088 * have to return just the parts of the hole
7089 * that go until the delalloc starts
7090 */
7091 em->len = min(hole_len,
7092 range_start - hole_start);
7093 em->start = hole_start;
7094 em->orig_start = hole_start;
7095 /*
7096 * don't adjust block start at all,
7097 * it is fixed at EXTENT_MAP_HOLE
7098 */
7099 em->block_start = hole_em->block_start;
7100 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007101 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7102 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007103 } else {
7104 em->start = range_start;
7105 em->len = found;
7106 em->orig_start = range_start;
7107 em->block_start = EXTENT_MAP_DELALLOC;
7108 em->block_len = found;
7109 }
7110 } else if (hole_em) {
7111 return hole_em;
7112 }
7113out:
7114
7115 free_extent_map(hole_em);
7116 if (err) {
7117 free_extent_map(em);
7118 return ERR_PTR(err);
7119 }
7120 return em;
7121}
7122
Josef Bacik4b46fce2010-05-23 11:00:55 -04007123static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7124 u64 start, u64 len)
7125{
7126 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007127 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007128 struct btrfs_key ins;
7129 u64 alloc_hint;
7130 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007131
Josef Bacik4b46fce2010-05-23 11:00:55 -04007132 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04007133 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007134 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007135 if (ret)
7136 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007137
Josef Bacik70c8a912012-10-11 16:54:30 -04007138 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007139 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04007140 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08007141 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007142 return em;
7143 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007144
7145 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
7146 ins.offset, ins.offset, 0);
7147 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08007148 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007149 free_extent_map(em);
7150 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007151 }
Josef Bacik00361582013-08-14 14:02:47 -04007152
Josef Bacik4b46fce2010-05-23 11:00:55 -04007153 return em;
7154}
7155
Chris Mason46bfbb52010-05-26 11:04:10 -04007156/*
7157 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7158 * block must be cow'd
7159 */
Josef Bacik00361582013-08-14 14:02:47 -04007160noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007161 u64 *orig_start, u64 *orig_block_len,
7162 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007163{
Josef Bacik00361582013-08-14 14:02:47 -04007164 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007165 struct btrfs_path *path;
7166 int ret;
7167 struct extent_buffer *leaf;
7168 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007169 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007170 struct btrfs_file_extent_item *fi;
7171 struct btrfs_key key;
7172 u64 disk_bytenr;
7173 u64 backref_offset;
7174 u64 extent_end;
7175 u64 num_bytes;
7176 int slot;
7177 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007178 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007179
Chris Mason46bfbb52010-05-26 11:04:10 -04007180 path = btrfs_alloc_path();
7181 if (!path)
7182 return -ENOMEM;
7183
Josef Bacik00361582013-08-14 14:02:47 -04007184 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007185 offset, 0);
7186 if (ret < 0)
7187 goto out;
7188
7189 slot = path->slots[0];
7190 if (ret == 1) {
7191 if (slot == 0) {
7192 /* can't find the item, must cow */
7193 ret = 0;
7194 goto out;
7195 }
7196 slot--;
7197 }
7198 ret = 0;
7199 leaf = path->nodes[0];
7200 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007201 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007202 key.type != BTRFS_EXTENT_DATA_KEY) {
7203 /* not our file or wrong item type, must cow */
7204 goto out;
7205 }
7206
7207 if (key.offset > offset) {
7208 /* Wrong offset, must cow */
7209 goto out;
7210 }
7211
7212 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7213 found_type = btrfs_file_extent_type(leaf, fi);
7214 if (found_type != BTRFS_FILE_EXTENT_REG &&
7215 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7216 /* not a regular extent, must cow */
7217 goto out;
7218 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007219
7220 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7221 goto out;
7222
Miao Xiee77751a2013-12-27 21:11:50 +08007223 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7224 if (extent_end <= offset)
7225 goto out;
7226
Chris Mason46bfbb52010-05-26 11:04:10 -04007227 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007228 if (disk_bytenr == 0)
7229 goto out;
7230
7231 if (btrfs_file_extent_compression(leaf, fi) ||
7232 btrfs_file_extent_encryption(leaf, fi) ||
7233 btrfs_file_extent_other_encoding(leaf, fi))
7234 goto out;
7235
Chris Mason46bfbb52010-05-26 11:04:10 -04007236 backref_offset = btrfs_file_extent_offset(leaf, fi);
7237
Josef Bacik7ee9e442013-06-21 16:37:03 -04007238 if (orig_start) {
7239 *orig_start = key.offset - backref_offset;
7240 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7241 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7242 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007243
Chris Mason46bfbb52010-05-26 11:04:10 -04007244 if (btrfs_extent_readonly(root, disk_bytenr))
7245 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007246
7247 num_bytes = min(offset + *len, extent_end) - offset;
7248 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7249 u64 range_end;
7250
7251 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7252 ret = test_range_bit(io_tree, offset, range_end,
7253 EXTENT_DELALLOC, 0, NULL);
7254 if (ret) {
7255 ret = -EAGAIN;
7256 goto out;
7257 }
7258 }
7259
Josef Bacik1bda19e2013-10-18 12:10:36 -04007260 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007261
7262 /*
7263 * look for other files referencing this extent, if we
7264 * find any we must cow
7265 */
Josef Bacik00361582013-08-14 14:02:47 -04007266 trans = btrfs_join_transaction(root);
7267 if (IS_ERR(trans)) {
7268 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007269 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007270 }
7271
7272 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7273 key.offset - backref_offset, disk_bytenr);
7274 btrfs_end_transaction(trans, root);
7275 if (ret) {
7276 ret = 0;
7277 goto out;
7278 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007279
7280 /*
7281 * adjust disk_bytenr and num_bytes to cover just the bytes
7282 * in this extent we are about to write. If there
7283 * are any csums in that range we have to cow in order
7284 * to keep the csums correct
7285 */
7286 disk_bytenr += backref_offset;
7287 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007288 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7289 goto out;
7290 /*
7291 * all of the above have passed, it is safe to overwrite this extent
7292 * without cow
7293 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007294 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007295 ret = 1;
7296out:
7297 btrfs_free_path(path);
7298 return ret;
7299}
7300
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007301bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7302{
7303 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7304 int found = false;
7305 void **pagep = NULL;
7306 struct page *page = NULL;
7307 int start_idx;
7308 int end_idx;
7309
7310 start_idx = start >> PAGE_CACHE_SHIFT;
7311
7312 /*
7313 * end is the last byte in the last page. end == start is legal
7314 */
7315 end_idx = end >> PAGE_CACHE_SHIFT;
7316
7317 rcu_read_lock();
7318
7319 /* Most of the code in this while loop is lifted from
7320 * find_get_page. It's been modified to begin searching from a
7321 * page and return just the first page found in that range. If the
7322 * found idx is less than or equal to the end idx then we know that
7323 * a page exists. If no pages are found or if those pages are
7324 * outside of the range then we're fine (yay!) */
7325 while (page == NULL &&
7326 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7327 page = radix_tree_deref_slot(pagep);
7328 if (unlikely(!page))
7329 break;
7330
7331 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007332 if (radix_tree_deref_retry(page)) {
7333 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007334 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007335 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007336 /*
7337 * Otherwise, shmem/tmpfs must be storing a swap entry
7338 * here as an exceptional entry: so return it without
7339 * attempting to raise page count.
7340 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007341 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007342 break; /* TODO: Is this relevant for this use case? */
7343 }
7344
Filipe Manana91405152014-06-05 13:22:24 +01007345 if (!page_cache_get_speculative(page)) {
7346 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007347 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007348 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007349
7350 /*
7351 * Has the page moved?
7352 * This is part of the lockless pagecache protocol. See
7353 * include/linux/pagemap.h for details.
7354 */
7355 if (unlikely(page != *pagep)) {
7356 page_cache_release(page);
7357 page = NULL;
7358 }
7359 }
7360
7361 if (page) {
7362 if (page->index <= end_idx)
7363 found = true;
7364 page_cache_release(page);
7365 }
7366
7367 rcu_read_unlock();
7368 return found;
7369}
7370
Josef Bacikeb838e72012-07-31 16:28:48 -04007371static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7372 struct extent_state **cached_state, int writing)
7373{
7374 struct btrfs_ordered_extent *ordered;
7375 int ret = 0;
7376
7377 while (1) {
7378 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7379 0, cached_state);
7380 /*
7381 * We're concerned with the entire range that we're going to be
7382 * doing DIO to, so we need to make sure theres no ordered
7383 * extents in this range.
7384 */
7385 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7386 lockend - lockstart + 1);
7387
7388 /*
7389 * We need to make sure there are no buffered pages in this
7390 * range either, we could have raced between the invalidate in
7391 * generic_file_direct_write and locking the extent. The
7392 * invalidate needs to happen so that reads after a write do not
7393 * get stale data.
7394 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007395 if (!ordered &&
7396 (!writing ||
7397 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007398 break;
7399
7400 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7401 cached_state, GFP_NOFS);
7402
7403 if (ordered) {
7404 btrfs_start_ordered_extent(inode, ordered, 1);
7405 btrfs_put_ordered_extent(ordered);
7406 } else {
7407 /* Screw you mmap */
Filipe Manana728404d2014-10-10 09:43:11 +01007408 ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
Filipe Manana075bdbd2014-10-09 21:18:55 +01007409 if (ret)
7410 break;
7411 ret = filemap_fdatawait_range(inode->i_mapping,
7412 lockstart,
7413 lockend);
Josef Bacikeb838e72012-07-31 16:28:48 -04007414 if (ret)
7415 break;
7416
7417 /*
7418 * If we found a page that couldn't be invalidated just
7419 * fall back to buffered.
7420 */
7421 ret = invalidate_inode_pages2_range(inode->i_mapping,
7422 lockstart >> PAGE_CACHE_SHIFT,
7423 lockend >> PAGE_CACHE_SHIFT);
7424 if (ret)
7425 break;
7426 }
7427
7428 cond_resched();
7429 }
7430
7431 return ret;
7432}
7433
Josef Bacik69ffb542012-09-11 15:40:07 -04007434static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7435 u64 len, u64 orig_start,
7436 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007437 u64 orig_block_len, u64 ram_bytes,
7438 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007439{
7440 struct extent_map_tree *em_tree;
7441 struct extent_map *em;
7442 struct btrfs_root *root = BTRFS_I(inode)->root;
7443 int ret;
7444
7445 em_tree = &BTRFS_I(inode)->extent_tree;
7446 em = alloc_extent_map();
7447 if (!em)
7448 return ERR_PTR(-ENOMEM);
7449
7450 em->start = start;
7451 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007452 em->mod_start = start;
7453 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007454 em->len = len;
7455 em->block_len = block_len;
7456 em->block_start = block_start;
7457 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007458 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007459 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007460 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007461 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7462 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007463 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007464
7465 do {
7466 btrfs_drop_extent_cache(inode, em->start,
7467 em->start + em->len - 1, 0);
7468 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007469 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007470 write_unlock(&em_tree->lock);
7471 } while (ret == -EEXIST);
7472
7473 if (ret) {
7474 free_extent_map(em);
7475 return ERR_PTR(ret);
7476 }
7477
7478 return em;
7479}
7480
chandan50745b02015-08-28 21:10:13 +05307481struct btrfs_dio_data {
7482 u64 outstanding_extents;
7483 u64 reserve;
7484};
Josef Bacik69ffb542012-09-11 15:40:07 -04007485
Josef Bacik4b46fce2010-05-23 11:00:55 -04007486static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7487 struct buffer_head *bh_result, int create)
7488{
7489 struct extent_map *em;
7490 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007491 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307492 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007493 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007494 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007495 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007496 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007497 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007498
Miao Xie172a5042013-02-21 02:48:22 -07007499 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007500 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007501 else
Josef Bacikc3298612012-08-03 16:49:19 -04007502 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007503
Josef Bacikc3298612012-08-03 16:49:19 -04007504 lockstart = start;
7505 lockend = start + len - 1;
7506
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007507 if (current->journal_info) {
7508 /*
7509 * Need to pull our outstanding extents and set journal_info to NULL so
7510 * that anything that needs to check if there's a transction doesn't get
7511 * confused.
7512 */
chandan50745b02015-08-28 21:10:13 +05307513 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007514 current->journal_info = NULL;
7515 }
7516
Josef Bacikeb838e72012-07-31 16:28:48 -04007517 /*
7518 * If this errors out it's because we couldn't invalidate pagecache for
7519 * this range and we need to fallback to buffered.
7520 */
7521 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7522 return -ENOTBLK;
7523
Josef Bacik4b46fce2010-05-23 11:00:55 -04007524 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007525 if (IS_ERR(em)) {
7526 ret = PTR_ERR(em);
7527 goto unlock_err;
7528 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007529
7530 /*
7531 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7532 * io. INLINE is special, and we could probably kludge it in here, but
7533 * it's still buffered so for safety lets just fall back to the generic
7534 * buffered path.
7535 *
7536 * For COMPRESSED we _have_ to read the entire extent in so we can
7537 * decompress it, so there will be buffering required no matter what we
7538 * do, so go ahead and fallback to buffered.
7539 *
7540 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7541 * to buffered IO. Don't blame me, this is the price we pay for using
7542 * the generic code.
7543 */
7544 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7545 em->block_start == EXTENT_MAP_INLINE) {
7546 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007547 ret = -ENOTBLK;
7548 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007549 }
7550
7551 /* Just a good old fashioned hole, return */
7552 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7553 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7554 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007555 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007556 }
7557
7558 /*
7559 * We don't allocate a new extent in the following cases
7560 *
7561 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7562 * existing extent.
7563 * 2) The extent is marked as PREALLOC. We're good to go here and can
7564 * just use the extent.
7565 *
7566 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007567 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007568 len = min(len, em->len - (start - em->start));
7569 lockstart = start + len;
7570 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007571 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007572
7573 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7574 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7575 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007576 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007577 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007578
7579 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7580 type = BTRFS_ORDERED_PREALLOC;
7581 else
7582 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007583 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007584 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007585
Josef Bacik00361582013-08-14 14:02:47 -04007586 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007587 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007588 if (type == BTRFS_ORDERED_PREALLOC) {
7589 free_extent_map(em);
7590 em = create_pinned_em(inode, start, len,
7591 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007592 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007593 orig_block_len,
7594 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007595 if (IS_ERR(em)) {
7596 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007597 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007598 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007599 }
7600
Chris Mason46bfbb52010-05-26 11:04:10 -04007601 ret = btrfs_add_ordered_extent_dio(inode, start,
7602 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007603 if (ret) {
7604 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007605 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007606 }
7607 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007608 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007609 }
Josef Bacik00361582013-08-14 14:02:47 -04007610
Chris Mason46bfbb52010-05-26 11:04:10 -04007611 /*
7612 * this will cow the extent, reset the len in case we changed
7613 * it above
7614 */
7615 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007616 free_extent_map(em);
7617 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007618 if (IS_ERR(em)) {
7619 ret = PTR_ERR(em);
7620 goto unlock_err;
7621 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007622 len = min(len, em->len - (start - em->start));
7623unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007624 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7625 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007626 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007627 bh_result->b_bdev = em->bdev;
7628 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007629 if (create) {
7630 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7631 set_buffer_new(bh_result);
7632
7633 /*
7634 * Need to update the i_size under the extent lock so buffered
7635 * readers will get the updated i_size when we unlock.
7636 */
7637 if (start + len > i_size_read(inode))
7638 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007639
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007640 /*
7641 * If we have an outstanding_extents count still set then we're
7642 * within our reservation, otherwise we need to adjust our inode
7643 * counter appropriately.
7644 */
chandan50745b02015-08-28 21:10:13 +05307645 if (dio_data->outstanding_extents) {
7646 (dio_data->outstanding_extents)--;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007647 } else {
Josef Bacik3e05bde2015-02-11 15:08:57 -05007648 spin_lock(&BTRFS_I(inode)->lock);
7649 BTRFS_I(inode)->outstanding_extents++;
7650 spin_unlock(&BTRFS_I(inode)->lock);
7651 }
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007652
Qu Wenruodf480632015-09-08 17:25:54 +08007653 __btrfs_free_reserved_data_space(inode, start, len);
chandan50745b02015-08-28 21:10:13 +05307654 WARN_ON(dio_data->reserve < len);
7655 dio_data->reserve -= len;
7656 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007657 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007658
Josef Bacikeb838e72012-07-31 16:28:48 -04007659 /*
7660 * In the case of write we need to clear and unlock the entire range,
7661 * in the case of read we need to unlock only the end area that we
7662 * aren't using if there is any left over space.
7663 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007664 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007665 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7666 lockend, unlock_bits, 1, 0,
7667 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007668 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007669 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007670 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007671
Josef Bacik4b46fce2010-05-23 11:00:55 -04007672 free_extent_map(em);
7673
7674 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007675
7676unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007677 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7678 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
chandan50745b02015-08-28 21:10:13 +05307679 if (dio_data)
7680 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007681 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007682}
7683
Miao Xie8b110e32014-09-12 18:44:03 +08007684static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7685 int rw, int mirror_num)
7686{
7687 struct btrfs_root *root = BTRFS_I(inode)->root;
7688 int ret;
7689
7690 BUG_ON(rw & REQ_WRITE);
7691
7692 bio_get(bio);
7693
7694 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7695 BTRFS_WQ_ENDIO_DIO_REPAIR);
7696 if (ret)
7697 goto err;
7698
7699 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7700err:
7701 bio_put(bio);
7702 return ret;
7703}
7704
7705static int btrfs_check_dio_repairable(struct inode *inode,
7706 struct bio *failed_bio,
7707 struct io_failure_record *failrec,
7708 int failed_mirror)
7709{
7710 int num_copies;
7711
7712 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7713 failrec->logical, failrec->len);
7714 if (num_copies == 1) {
7715 /*
7716 * we only have a single copy of the data, so don't bother with
7717 * all the retry and error correction code that follows. no
7718 * matter what the error is, it is very likely to persist.
7719 */
7720 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7721 num_copies, failrec->this_mirror, failed_mirror);
7722 return 0;
7723 }
7724
7725 failrec->failed_mirror = failed_mirror;
7726 failrec->this_mirror++;
7727 if (failrec->this_mirror == failed_mirror)
7728 failrec->this_mirror++;
7729
7730 if (failrec->this_mirror > num_copies) {
7731 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7732 num_copies, failrec->this_mirror, failed_mirror);
7733 return 0;
7734 }
7735
7736 return 1;
7737}
7738
7739static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7740 struct page *page, u64 start, u64 end,
7741 int failed_mirror, bio_end_io_t *repair_endio,
7742 void *repair_arg)
7743{
7744 struct io_failure_record *failrec;
7745 struct bio *bio;
7746 int isector;
7747 int read_mode;
7748 int ret;
7749
7750 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7751
7752 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7753 if (ret)
7754 return ret;
7755
7756 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7757 failed_mirror);
7758 if (!ret) {
7759 free_io_failure(inode, failrec);
7760 return -EIO;
7761 }
7762
7763 if (failed_bio->bi_vcnt > 1)
7764 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7765 else
7766 read_mode = READ_SYNC;
7767
7768 isector = start - btrfs_io_bio(failed_bio)->logical;
7769 isector >>= inode->i_sb->s_blocksize_bits;
7770 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7771 0, isector, repair_endio, repair_arg);
7772 if (!bio) {
7773 free_io_failure(inode, failrec);
7774 return -EIO;
7775 }
7776
7777 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7778 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7779 read_mode, failrec->this_mirror, failrec->in_validation);
7780
7781 ret = submit_dio_repair_bio(inode, bio, read_mode,
7782 failrec->this_mirror);
7783 if (ret) {
7784 free_io_failure(inode, failrec);
7785 bio_put(bio);
7786 }
7787
7788 return ret;
7789}
7790
7791struct btrfs_retry_complete {
7792 struct completion done;
7793 struct inode *inode;
7794 u64 start;
7795 int uptodate;
7796};
7797
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007798static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007799{
7800 struct btrfs_retry_complete *done = bio->bi_private;
7801 struct bio_vec *bvec;
7802 int i;
7803
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007804 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007805 goto end;
7806
7807 done->uptodate = 1;
7808 bio_for_each_segment_all(bvec, bio, i)
7809 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7810end:
7811 complete(&done->done);
7812 bio_put(bio);
7813}
7814
7815static int __btrfs_correct_data_nocsum(struct inode *inode,
7816 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007817{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007818 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007819 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007820 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007821 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007822 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007823
Miao Xiec1dc0892014-09-12 18:43:56 +08007824 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007825 done.inode = inode;
7826
7827 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7828try_again:
7829 done.uptodate = 0;
7830 done.start = start;
7831 init_completion(&done.done);
7832
7833 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7834 start + bvec->bv_len - 1,
7835 io_bio->mirror_num,
7836 btrfs_retry_endio_nocsum, &done);
7837 if (ret)
7838 return ret;
7839
7840 wait_for_completion(&done.done);
7841
7842 if (!done.uptodate) {
7843 /* We might have another mirror, so try again */
7844 goto try_again;
7845 }
7846
7847 start += bvec->bv_len;
7848 }
7849
7850 return 0;
7851}
7852
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007853static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007854{
7855 struct btrfs_retry_complete *done = bio->bi_private;
7856 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7857 struct bio_vec *bvec;
7858 int uptodate;
7859 int ret;
7860 int i;
7861
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007862 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007863 goto end;
7864
7865 uptodate = 1;
7866 bio_for_each_segment_all(bvec, bio, i) {
7867 ret = __readpage_endio_check(done->inode, io_bio, i,
7868 bvec->bv_page, 0,
7869 done->start, bvec->bv_len);
7870 if (!ret)
7871 clean_io_failure(done->inode, done->start,
7872 bvec->bv_page, 0);
7873 else
7874 uptodate = 0;
7875 }
7876
7877 done->uptodate = uptodate;
7878end:
7879 complete(&done->done);
7880 bio_put(bio);
7881}
7882
7883static int __btrfs_subio_endio_read(struct inode *inode,
7884 struct btrfs_io_bio *io_bio, int err)
7885{
7886 struct bio_vec *bvec;
7887 struct btrfs_retry_complete done;
7888 u64 start;
7889 u64 offset = 0;
7890 int i;
7891 int ret;
7892
7893 err = 0;
7894 start = io_bio->logical;
7895 done.inode = inode;
7896
Miao Xiec1dc0892014-09-12 18:43:56 +08007897 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007898 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7899 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007900 if (likely(!ret))
7901 goto next;
7902try_again:
7903 done.uptodate = 0;
7904 done.start = start;
7905 init_completion(&done.done);
7906
7907 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7908 start + bvec->bv_len - 1,
7909 io_bio->mirror_num,
7910 btrfs_retry_endio, &done);
7911 if (ret) {
7912 err = ret;
7913 goto next;
7914 }
7915
7916 wait_for_completion(&done.done);
7917
7918 if (!done.uptodate) {
7919 /* We might have another mirror, so try again */
7920 goto try_again;
7921 }
7922next:
7923 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007924 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007925 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007926
7927 return err;
7928}
7929
Miao Xie8b110e32014-09-12 18:44:03 +08007930static int btrfs_subio_endio_read(struct inode *inode,
7931 struct btrfs_io_bio *io_bio, int err)
7932{
7933 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7934
7935 if (skip_csum) {
7936 if (unlikely(err))
7937 return __btrfs_correct_data_nocsum(inode, io_bio);
7938 else
7939 return 0;
7940 } else {
7941 return __btrfs_subio_endio_read(inode, io_bio, err);
7942 }
7943}
7944
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007945static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08007946{
7947 struct btrfs_dio_private *dip = bio->bi_private;
7948 struct inode *inode = dip->inode;
7949 struct bio *dio_bio;
7950 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007951 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08007952
Miao Xie8b110e32014-09-12 18:44:03 +08007953 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7954 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007955
Josef Bacik4b46fce2010-05-23 11:00:55 -04007956 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007957 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007958 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007959
Josef Bacik4b46fce2010-05-23 11:00:55 -04007960 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007961
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007962 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08007963
7964 if (io_bio->end_io)
7965 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007966 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007967}
7968
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007969static void btrfs_endio_direct_write(struct bio *bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007970{
7971 struct btrfs_dio_private *dip = bio->bi_private;
7972 struct inode *inode = dip->inode;
7973 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007974 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007975 u64 ordered_offset = dip->logical_offset;
7976 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007977 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007978 int ret;
7979
Chris Mason163cf092010-11-28 19:56:33 -05007980again:
7981 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7982 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007983 ordered_bytes,
7984 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007985 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007986 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007987
Liu Bo9e0af232014-08-15 23:36:53 +08007988 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7989 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007990 btrfs_queue_work(root->fs_info->endio_write_workers,
7991 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007992out_test:
7993 /*
7994 * our bio might span multiple ordered extents. If we haven't
7995 * completed the accounting for the whole dio, go back and try again
7996 */
7997 if (ordered_offset < dip->logical_offset + dip->bytes) {
7998 ordered_bytes = dip->logical_offset + dip->bytes -
7999 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008000 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008001 goto again;
8002 }
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);
Chris Mason9be33952013-05-17 18:30:14 -04008008 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008009}
8010
Chris Masoneaf25d92010-05-25 09:48:28 -04008011static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
8012 struct bio *bio, int mirror_num,
8013 unsigned long bio_flags, u64 offset)
8014{
8015 int ret;
8016 struct btrfs_root *root = BTRFS_I(inode)->root;
8017 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008018 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008019 return 0;
8020}
8021
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008022static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008023{
8024 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008025 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008026
Miao Xie8b110e32014-09-12 18:44:03 +08008027 if (err)
8028 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
8029 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
8030 btrfs_ino(dip->inode), bio->bi_rw,
8031 (unsigned long long)bio->bi_iter.bi_sector,
8032 bio->bi_iter.bi_size, err);
8033
8034 if (dip->subio_endio)
8035 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008036
8037 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008038 dip->errors = 1;
8039
8040 /*
8041 * before atomic variable goto zero, we must make sure
8042 * dip->errors is perceived to be set.
8043 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008044 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008045 }
8046
8047 /* if there are more bios still pending for this dio, just exit */
8048 if (!atomic_dec_and_test(&dip->pending_bios))
8049 goto out;
8050
Chris Mason9be33952013-05-17 18:30:14 -04008051 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008052 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008053 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008054 dip->dio_bio->bi_error = 0;
8055 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008056 }
8057out:
8058 bio_put(bio);
8059}
8060
8061static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8062 u64 first_sector, gfp_t gfp_flags)
8063{
Chris Masonda2f0f72015-07-02 13:57:22 -07008064 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008065 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008066 if (bio)
8067 bio_associate_current(bio);
8068 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008069}
8070
Miao Xiec1dc0892014-09-12 18:43:56 +08008071static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8072 struct inode *inode,
8073 struct btrfs_dio_private *dip,
8074 struct bio *bio,
8075 u64 file_offset)
8076{
8077 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8078 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8079 int ret;
8080
8081 /*
8082 * We load all the csum data we need when we submit
8083 * the first bio to reduce the csum tree search and
8084 * contention.
8085 */
8086 if (dip->logical_offset == file_offset) {
8087 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8088 file_offset);
8089 if (ret)
8090 return ret;
8091 }
8092
8093 if (bio == dip->orig_bio)
8094 return 0;
8095
8096 file_offset -= dip->logical_offset;
8097 file_offset >>= inode->i_sb->s_blocksize_bits;
8098 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8099
8100 return 0;
8101}
8102
Miao Xiee65e1532010-11-22 03:04:43 +00008103static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8104 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008105 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008106{
Miao Xiefacc8a222013-07-25 19:22:34 +08008107 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00008108 int write = rw & REQ_WRITE;
8109 struct btrfs_root *root = BTRFS_I(inode)->root;
8110 int ret;
8111
Josef Bacikb812ce22012-11-16 13:56:32 -05008112 if (async_submit)
8113 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8114
Miao Xiee65e1532010-11-22 03:04:43 +00008115 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008116
8117 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008118 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8119 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008120 if (ret)
8121 goto err;
8122 }
Miao Xiee65e1532010-11-22 03:04:43 +00008123
Josef Bacik1ae39932011-04-06 14:41:34 -04008124 if (skip_sum)
8125 goto map;
8126
8127 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008128 ret = btrfs_wq_submit_bio(root->fs_info,
8129 inode, rw, bio, 0, 0,
8130 file_offset,
8131 __btrfs_submit_bio_start_direct_io,
8132 __btrfs_submit_bio_done);
8133 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008134 } else if (write) {
8135 /*
8136 * If we aren't doing async submit, calculate the csum of the
8137 * bio now.
8138 */
8139 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8140 if (ret)
8141 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008142 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008143 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8144 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008145 if (ret)
8146 goto err;
8147 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008148map:
8149 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008150err:
8151 bio_put(bio);
8152 return ret;
8153}
8154
8155static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8156 int skip_sum)
8157{
8158 struct inode *inode = dip->inode;
8159 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008160 struct bio *bio;
8161 struct bio *orig_bio = dip->orig_bio;
8162 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008163 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008164 u64 file_offset = dip->logical_offset;
8165 u64 submit_len = 0;
8166 u64 map_length;
8167 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08008168 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04008169 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008170
Kent Overstreet4f024f32013-10-11 15:44:27 -07008171 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008172 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008173 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008174 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008175 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008176
Kent Overstreet4f024f32013-10-11 15:44:27 -07008177 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008178 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008179 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008180 goto submit;
8181 }
8182
David Woodhouse53b381b2013-01-29 18:40:14 -05008183 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008184 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008185 async_submit = 0;
8186 else
8187 async_submit = 1;
8188
Josef Bacik02f57c72011-04-06 14:25:44 -04008189 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8190 if (!bio)
8191 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008192
Josef Bacik02f57c72011-04-06 14:25:44 -04008193 bio->bi_private = dip;
8194 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008195 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008196 atomic_inc(&dip->pending_bios);
8197
Miao Xiee65e1532010-11-22 03:04:43 +00008198 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02008199 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00008200 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02008201 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00008202 /*
8203 * inc the count before we submit the bio so
8204 * we know the end IO handler won't happen before
8205 * we inc the count. Otherwise, the dip might get freed
8206 * before we're done setting it up
8207 */
8208 atomic_inc(&dip->pending_bios);
8209 ret = __btrfs_submit_dio_bio(bio, inode, rw,
8210 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008211 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008212 if (ret) {
8213 bio_put(bio);
8214 atomic_dec(&dip->pending_bios);
8215 goto out_err;
8216 }
8217
Miao Xiee65e1532010-11-22 03:04:43 +00008218 start_sector += submit_len >> 9;
8219 file_offset += submit_len;
8220
8221 submit_len = 0;
8222 nr_pages = 0;
8223
8224 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8225 start_sector, GFP_NOFS);
8226 if (!bio)
8227 goto out_err;
8228 bio->bi_private = dip;
8229 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008230 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008231
Kent Overstreet4f024f32013-10-11 15:44:27 -07008232 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008233 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008234 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008235 &map_length, NULL, 0);
8236 if (ret) {
8237 bio_put(bio);
8238 goto out_err;
8239 }
8240 } else {
8241 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308242 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00008243 bvec++;
8244 }
8245 }
8246
Josef Bacik02f57c72011-04-06 14:25:44 -04008247submit:
Miao Xiee65e1532010-11-22 03:04:43 +00008248 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008249 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008250 if (!ret)
8251 return 0;
8252
8253 bio_put(bio);
8254out_err:
8255 dip->errors = 1;
8256 /*
8257 * before atomic variable goto zero, we must
8258 * make sure dip->errors is perceived to be set.
8259 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008260 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008261 if (atomic_dec_and_test(&dip->pending_bios))
8262 bio_io_error(dip->orig_bio);
8263
8264 /* bio_end_io() will handle error, so we needn't return it */
8265 return 0;
8266}
8267
Chris Mason9be33952013-05-17 18:30:14 -04008268static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8269 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008270{
Filipe Manana61de7182015-07-01 12:13:10 +01008271 struct btrfs_dio_private *dip = NULL;
8272 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008273 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008274 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02008275 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008276 int ret = 0;
8277
8278 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8279
Chris Mason9be33952013-05-17 18:30:14 -04008280 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008281 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008282 ret = -ENOMEM;
8283 goto free_ordered;
8284 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008285
Miao Xiec1dc0892014-09-12 18:43:56 +08008286 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008287 if (!dip) {
8288 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008289 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008290 }
8291
8292 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008293 dip->inode = inode;
8294 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008295 dip->bytes = dio_bio->bi_iter.bi_size;
8296 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008297 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008298 dip->orig_bio = io_bio;
8299 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008300 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008301 btrfs_bio = btrfs_io_bio(io_bio);
8302 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008303
Miao Xiec1dc0892014-09-12 18:43:56 +08008304 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008305 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008306 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008307 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008308 dip->subio_endio = btrfs_subio_endio_read;
8309 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008310
Miao Xiee65e1532010-11-22 03:04:43 +00008311 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8312 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008313 return;
Chris Mason9be33952013-05-17 18:30:14 -04008314
Miao Xie23ea8e52014-09-12 18:43:54 +08008315 if (btrfs_bio->end_io)
8316 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008317
Josef Bacik4b46fce2010-05-23 11:00:55 -04008318free_ordered:
8319 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008320 * If we arrived here it means either we failed to submit the dip
8321 * or we either failed to clone the dio_bio or failed to allocate the
8322 * dip. If we cloned the dio_bio and allocated the dip, we can just
8323 * call bio_endio against our io_bio so that we get proper resource
8324 * cleanup if we fail to submit the dip, otherwise, we must do the
8325 * same as btrfs_endio_direct_[write|read] because we can't call these
8326 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008327 */
Filipe Manana61de7182015-07-01 12:13:10 +01008328 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008329 io_bio->bi_error = -EIO;
8330 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008331 /*
8332 * The end io callbacks free our dip, do the final put on io_bio
8333 * and all the cleanup and final put for dio_bio (through
8334 * dio_end_io()).
8335 */
8336 dip = NULL;
8337 io_bio = NULL;
8338 } else {
8339 if (write) {
8340 struct btrfs_ordered_extent *ordered;
8341
8342 ordered = btrfs_lookup_ordered_extent(inode,
8343 file_offset);
8344 set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
8345 /*
8346 * Decrements our ref on the ordered extent and removes
8347 * the ordered extent from the inode's ordered tree,
8348 * doing all the proper resource cleanup such as for the
8349 * reserved space and waking up any waiters for this
8350 * ordered extent (through btrfs_remove_ordered_extent).
8351 */
8352 btrfs_finish_ordered_io(ordered);
8353 } else {
8354 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8355 file_offset + dio_bio->bi_iter.bi_size - 1);
8356 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008357 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008358 /*
8359 * Releases and cleans up our dio_bio, no need to bio_put()
8360 * nor bio_endio()/bio_io_error() against dio_bio.
8361 */
8362 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008363 }
Filipe Manana61de7182015-07-01 12:13:10 +01008364 if (io_bio)
8365 bio_put(io_bio);
8366 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008367}
8368
Omar Sandoval6f673762015-03-16 04:33:52 -07008369static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008370 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008371{
8372 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008373 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008374 unsigned blocksize_mask = root->sectorsize - 1;
8375 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008376
8377 if (offset & blocksize_mask)
8378 goto out;
8379
Al Viro28060d52014-03-22 05:15:17 -04008380 if (iov_iter_alignment(iter) & blocksize_mask)
8381 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008382
Al Viro28060d52014-03-22 05:15:17 -04008383 /* If this is a write we don't need to check anymore */
Omar Sandoval6f673762015-03-16 04:33:52 -07008384 if (iov_iter_rw(iter) == WRITE)
Al Viro28060d52014-03-22 05:15:17 -04008385 return 0;
8386 /*
8387 * Check to make sure we don't have duplicate iov_base's in this
8388 * iovec, if so return EINVAL, otherwise we'll get csum errors
8389 * when reading back.
8390 */
8391 for (seg = 0; seg < iter->nr_segs; seg++) {
8392 for (i = seg + 1; i < iter->nr_segs; i++) {
8393 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008394 goto out;
8395 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008396 }
8397 retval = 0;
8398out:
8399 return retval;
8400}
Josef Bacikeb838e72012-07-31 16:28:48 -04008401
Omar Sandoval22c61862015-03-16 04:33:53 -07008402static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
8403 loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008404{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008405 struct file *file = iocb->ki_filp;
8406 struct inode *inode = file->f_mapping->host;
chandan50745b02015-08-28 21:10:13 +05308407 struct btrfs_root *root = BTRFS_I(inode)->root;
8408 struct btrfs_dio_data dio_data = { 0 };
Miao Xie09348562013-02-07 10:12:07 +00008409 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008410 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008411 bool wakeup = true;
8412 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008413 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008414
Omar Sandoval6f673762015-03-16 04:33:52 -07008415 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008416 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008417
Jens Axboefe0f07d2015-04-15 17:05:48 -06008418 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008419 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008420
Josef Bacik0e267c42013-07-02 10:38:02 -04008421 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008422 * The generic stuff only does filemap_write_and_wait_range, which
8423 * isn't enough if we've written compressed pages to this area, so
8424 * we need to flush the dirty pages again to make absolutely sure
8425 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008426 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008427 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008428 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8429 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008430 filemap_fdatawrite_range(inode->i_mapping, offset,
8431 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008432
Omar Sandoval6f673762015-03-16 04:33:52 -07008433 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008434 /*
8435 * If the write DIO is beyond the EOF, we need update
8436 * the isize, but it is protected by i_mutex. So we can
8437 * not unlock the i_mutex at this case.
8438 */
8439 if (offset + count <= inode->i_size) {
8440 mutex_unlock(&inode->i_mutex);
8441 relock = true;
8442 }
Qu Wenruodf480632015-09-08 17:25:54 +08008443 ret = __btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008444 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008445 goto out;
chandan50745b02015-08-28 21:10:13 +05308446 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008447 BTRFS_MAX_EXTENT_SIZE - 1,
8448 BTRFS_MAX_EXTENT_SIZE);
8449
8450 /*
8451 * We need to know how many extents we reserved so that we can
8452 * do the accounting properly if we go over the number we
8453 * originally calculated. Abuse current->journal_info for this.
8454 */
chandan50745b02015-08-28 21:10:13 +05308455 dio_data.reserve = round_up(count, root->sectorsize);
8456 current->journal_info = &dio_data;
David Sterbaee39b432014-09-30 01:33:33 +02008457 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8458 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008459 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008460 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8461 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008462 }
8463
Omar Sandoval17f8c842015-03-16 04:33:50 -07008464 ret = __blockdev_direct_IO(iocb, inode,
8465 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
8466 iter, offset, btrfs_get_blocks_direct, NULL,
8467 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008468 if (iov_iter_rw(iter) == WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008469 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008470 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308471 if (dio_data.reserve)
Qu Wenruodf480632015-09-08 17:25:54 +08008472 __btrfs_delalloc_release_space(inode, offset,
8473 dio_data.reserve);
Liu Boddba1bf2015-06-17 16:59:58 +08008474 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruodf480632015-09-08 17:25:54 +08008475 __btrfs_delalloc_release_space(inode, offset,
8476 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008477 }
Miao Xie38851cc2013-02-08 07:04:11 +00008478out:
Miao Xie2e60a512013-02-08 07:01:08 +00008479 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008480 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008481 if (relock)
8482 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008483
8484 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008485}
8486
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008487#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8488
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008489static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8490 __u64 start, __u64 len)
8491{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008492 int ret;
8493
8494 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8495 if (ret)
8496 return ret;
8497
Chris Masonec29ed52011-02-23 16:23:20 -05008498 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008499}
8500
Chris Mason9ebefb182007-06-15 13:50:00 -04008501int btrfs_readpage(struct file *file, struct page *page)
8502{
Chris Masond1310b22008-01-24 16:13:08 -05008503 struct extent_io_tree *tree;
8504 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008505 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008506}
Chris Mason1832a6d2007-12-21 16:27:21 -05008507
Chris Mason39279cc2007-06-12 06:35:45 -04008508static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8509{
Chris Masond1310b22008-01-24 16:13:08 -05008510 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04008511
8512
8513 if (current->flags & PF_MEMALLOC) {
8514 redirty_page_for_writepage(wbc, page);
8515 unlock_page(page);
8516 return 0;
8517 }
Chris Masond1310b22008-01-24 16:13:08 -05008518 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008519 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8520}
Chris Mason39279cc2007-06-12 06:35:45 -04008521
Eric Sandeen48a3b632013-04-25 20:41:01 +00008522static int btrfs_writepages(struct address_space *mapping,
8523 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008524{
Chris Masond1310b22008-01-24 16:13:08 -05008525 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008526
Chris Masond1310b22008-01-24 16:13:08 -05008527 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008528 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8529}
8530
Chris Mason3ab2fb52007-11-08 10:59:22 -05008531static int
8532btrfs_readpages(struct file *file, struct address_space *mapping,
8533 struct list_head *pages, unsigned nr_pages)
8534{
Chris Masond1310b22008-01-24 16:13:08 -05008535 struct extent_io_tree *tree;
8536 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008537 return extent_readpages(tree, mapping, pages, nr_pages,
8538 btrfs_get_extent);
8539}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008540static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008541{
Chris Masond1310b22008-01-24 16:13:08 -05008542 struct extent_io_tree *tree;
8543 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008544 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008545
Chris Masond1310b22008-01-24 16:13:08 -05008546 tree = &BTRFS_I(page->mapping->host)->io_tree;
8547 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008548 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008549 if (ret == 1) {
8550 ClearPagePrivate(page);
8551 set_page_private(page, 0);
8552 page_cache_release(page);
8553 }
8554 return ret;
8555}
Chris Mason39279cc2007-06-12 06:35:45 -04008556
Chris Masone6dcd2d2008-07-17 12:53:50 -04008557static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8558{
Chris Mason98509cf2008-09-11 15:51:43 -04008559 if (PageWriteback(page) || PageDirty(page))
8560 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008561 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008562}
8563
Lukas Czernerd47992f2013-05-21 23:17:23 -04008564static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8565 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008566{
Josef Bacik5fd02042012-05-02 14:00:54 -04008567 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008568 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008569 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008570 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008571 u64 page_start = page_offset(page);
8572 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008573 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008574
Chris Mason8b62b722009-09-02 16:53:46 -04008575 /*
8576 * we have the page locked, so new writeback can't start,
8577 * and the dirty bit won't be cleared while we are here.
8578 *
8579 * Wait for IO on this page so that we can safely clear
8580 * the PagePrivate2 bit and do ordered accounting
8581 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008582 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008583
Josef Bacik5fd02042012-05-02 14:00:54 -04008584 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008585 if (offset) {
8586 btrfs_releasepage(page, GFP_NOFS);
8587 return;
8588 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008589
8590 if (!inode_evicting)
8591 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8592 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008593 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008594 /*
8595 * IO on this page will never be started, so we need
8596 * to account for any ordered extents now
8597 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008598 if (!inode_evicting)
8599 clear_extent_bit(tree, page_start, page_end,
8600 EXTENT_DIRTY | EXTENT_DELALLOC |
8601 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8602 EXTENT_DEFRAG, 1, 0, &cached_state,
8603 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008604 /*
8605 * whoever cleared the private bit is responsible
8606 * for the finish_ordered_io
8607 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008608 if (TestClearPagePrivate2(page)) {
8609 struct btrfs_ordered_inode_tree *tree;
8610 u64 new_len;
8611
8612 tree = &BTRFS_I(inode)->ordered_tree;
8613
8614 spin_lock_irq(&tree->lock);
8615 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8616 new_len = page_start - ordered->file_offset;
8617 if (new_len < ordered->truncated_len)
8618 ordered->truncated_len = new_len;
8619 spin_unlock_irq(&tree->lock);
8620
8621 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8622 page_start,
8623 PAGE_CACHE_SIZE, 1))
8624 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008625 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008626 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008627 if (!inode_evicting) {
8628 cached_state = NULL;
8629 lock_extent_bits(tree, page_start, page_end, 0,
8630 &cached_state);
8631 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008632 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008633
8634 if (!inode_evicting) {
8635 clear_extent_bit(tree, page_start, page_end,
8636 EXTENT_LOCKED | EXTENT_DIRTY |
8637 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8638 EXTENT_DEFRAG, 1, 1,
8639 &cached_state, GFP_NOFS);
8640
8641 __btrfs_releasepage(page, GFP_NOFS);
8642 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008643
Chris Mason4a096752008-07-21 10:29:44 -04008644 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008645 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008646 ClearPagePrivate(page);
8647 set_page_private(page, 0);
8648 page_cache_release(page);
8649 }
Chris Mason39279cc2007-06-12 06:35:45 -04008650}
8651
Chris Mason9ebefb182007-06-15 13:50:00 -04008652/*
8653 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8654 * called from a page fault handler when a page is first dirtied. Hence we must
8655 * be careful to check for EOF conditions here. We set the page up correctly
8656 * for a written page which means we get ENOSPC checking when writing into
8657 * holes and correct delalloc and unwritten extent mapping on filesystems that
8658 * support these features.
8659 *
8660 * We are not allowed to take the i_mutex here so we have to play games to
8661 * protect against truncate races as the page could now be beyond EOF. Because
8662 * vmtruncate() writes the inode size before removing pages, once we have the
8663 * page lock we can determine safely if the page is beyond EOF. If it is not
8664 * beyond EOF, then the page is guaranteed safe against truncation until we
8665 * unlock the page.
8666 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008667int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008668{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008669 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008670 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008671 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008672 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8673 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008674 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008675 char *kaddr;
8676 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008677 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008678 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008679 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008680 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008681 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008682
Jan Karab2b5ef52012-06-12 16:20:45 +02008683 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008684 page_start = page_offset(page);
8685 page_end = page_start + PAGE_CACHE_SIZE - 1;
8686
8687 ret = __btrfs_delalloc_reserve_space(inode, page_start,
8688 PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008689 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008690 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008691 reserved = 1;
8692 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008693 if (ret) {
8694 if (ret == -ENOMEM)
8695 ret = VM_FAULT_OOM;
8696 else /* -ENOSPC, -EIO, etc */
8697 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008698 if (reserved)
8699 goto out;
8700 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008701 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008702
Nick Piggin56a76f82009-03-31 15:23:23 -07008703 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008704again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008705 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008706 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008707
Chris Mason9ebefb182007-06-15 13:50:00 -04008708 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008709 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008710 /* page got truncated out from underneath us */
8711 goto out_unlock;
8712 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008713 wait_on_page_writeback(page);
8714
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008715 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008716 set_page_extent_mapped(page);
8717
Chris Masoneb84ae02008-07-17 13:53:27 -04008718 /*
8719 * we can't set the delalloc bits if there are pending ordered
8720 * extents. Drop our locks and wait for them to finish
8721 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008722 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8723 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008724 unlock_extent_cached(io_tree, page_start, page_end,
8725 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008726 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008727 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008728 btrfs_put_ordered_extent(ordered);
8729 goto again;
8730 }
8731
Josef Bacikfbf19082009-10-01 17:10:23 -04008732 /*
8733 * XXX - page_mkwrite gets called every time the page is dirtied, even
8734 * if it was already dirty, so for space accounting reasons we need to
8735 * clear any delalloc bits for the range we are fixing to save. There
8736 * is probably a better way to do this, but for now keep consistent with
8737 * prepare_pages in the normal write path.
8738 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008739 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008740 EXTENT_DIRTY | EXTENT_DELALLOC |
8741 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008742 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008743
Josef Bacik2ac55d42010-02-03 19:33:23 +00008744 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8745 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008746 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008747 unlock_extent_cached(io_tree, page_start, page_end,
8748 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008749 ret = VM_FAULT_SIGBUS;
8750 goto out_unlock;
8751 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008752 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008753
8754 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008755 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008756 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008757 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008758 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008759
Chris Masone6dcd2d2008-07-17 12:53:50 -04008760 if (zero_start != PAGE_CACHE_SIZE) {
8761 kaddr = kmap(page);
8762 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8763 flush_dcache_page(page);
8764 kunmap(page);
8765 }
Chris Mason247e7432008-07-17 12:53:51 -04008766 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008767 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008768 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008769
Chris Mason257c62e2009-10-13 13:21:08 -04008770 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8771 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008772 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008773
Josef Bacik2ac55d42010-02-03 19:33:23 +00008774 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008775
8776out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008777 if (!ret) {
8778 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008779 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008780 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008781 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008782out:
Qu Wenruodf480632015-09-08 17:25:54 +08008783 __btrfs_delalloc_release_space(inode, page_start, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008784out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008785 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008786 return ret;
8787}
8788
Josef Bacika41ad392011-01-31 15:30:16 -05008789static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008790{
8791 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008792 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008793 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008794 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008795 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008796 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008797 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008798
Josef Bacik0ef8b722013-10-25 16:13:35 -04008799 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8800 (u64)-1);
8801 if (ret)
8802 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008803
Josef Bacikfcb80c22011-05-03 10:40:22 -04008804 /*
8805 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8806 * 3 things going on here
8807 *
8808 * 1) We need to reserve space for our orphan item and the space to
8809 * delete our orphan item. Lord knows we don't want to have a dangling
8810 * orphan item because we didn't reserve space to remove it.
8811 *
8812 * 2) We need to reserve space to update our inode.
8813 *
8814 * 3) We need to have something to cache all the space that is going to
8815 * be free'd up by the truncate operation, but also have some slack
8816 * space reserved in case it uses space during the truncate (thank you
8817 * very much snapshotting).
8818 *
8819 * And we need these to all be seperate. The fact is we can use alot of
8820 * space doing the truncate, and we have no earthly idea how much space
8821 * we will use, so we need the truncate reservation to be seperate so it
8822 * doesn't end up using space reserved for updating the inode or
8823 * removing the orphan item. We also need to be able to stop the
8824 * transaction and start a new one, which means we need to be able to
8825 * update the inode several times, and we have no idea of knowing how
8826 * many times that will be, so we can't just reserve 1 item for the
8827 * entirety of the opration, so that has to be done seperately as well.
8828 * Then there is the orphan item, which does indeed need to be held on
8829 * to for the whole operation, and we need nobody to touch this reserved
8830 * space except the orphan code.
8831 *
8832 * So that leaves us with
8833 *
8834 * 1) root->orphan_block_rsv - for the orphan deletion.
8835 * 2) rsv - for the truncate reservation, which we will steal from the
8836 * transaction reservation.
8837 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8838 * updating the inode.
8839 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008840 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008841 if (!rsv)
8842 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008843 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008844 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008845
Josef Bacik907cbce2011-08-08 13:46:15 -04008846 /*
Josef Bacik07127182011-08-19 10:29:59 -04008847 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008848 * 1 for updating the inode.
8849 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008850 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008851 if (IS_ERR(trans)) {
8852 err = PTR_ERR(trans);
8853 goto out;
8854 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008855
Josef Bacik907cbce2011-08-08 13:46:15 -04008856 /* Migrate the slack space for the truncate to our reserve */
8857 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8858 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008859 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008860
Chris Mason5a3f23d2009-03-31 13:27:11 -04008861 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008862 * So if we truncate and then write and fsync we normally would just
8863 * write the extents that changed, which is a problem if we need to
8864 * first truncate that entire inode. So set this flag so we write out
8865 * all of the extents in the inode to the sync log so we're completely
8866 * safe.
8867 */
8868 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008869 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008870
Yan, Zheng80825102009-11-12 09:35:36 +00008871 while (1) {
8872 ret = btrfs_truncate_inode_items(trans, root, inode,
8873 inode->i_size,
8874 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08008875 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05008876 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008877 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008878 }
Chris Mason39279cc2007-06-12 06:35:45 -04008879
Josef Bacikfcb80c22011-05-03 10:40:22 -04008880 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008881 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008882 if (ret) {
8883 err = ret;
8884 break;
8885 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008886
Yan, Zheng80825102009-11-12 09:35:36 +00008887 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008888 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008889
8890 trans = btrfs_start_transaction(root, 2);
8891 if (IS_ERR(trans)) {
8892 ret = err = PTR_ERR(trans);
8893 trans = NULL;
8894 break;
8895 }
8896
8897 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8898 rsv, min_size);
8899 BUG_ON(ret); /* shouldn't happen */
8900 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008901 }
8902
8903 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008904 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008905 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008906 if (ret)
8907 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008908 }
8909
Chris Mason917c16b2011-11-08 14:49:59 -05008910 if (trans) {
8911 trans->block_rsv = &root->fs_info->trans_block_rsv;
8912 ret = btrfs_update_inode(trans, root, inode);
8913 if (ret && !err)
8914 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008915
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008916 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008917 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008918 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008919
8920out:
8921 btrfs_free_block_rsv(root, rsv);
8922
Josef Bacik3893e332011-01-31 16:03:11 -05008923 if (ret && !err)
8924 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008925
Josef Bacik3893e332011-01-31 16:03:11 -05008926 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008927}
8928
Sven Wegener3b963622008-06-09 21:57:42 -04008929/*
Chris Masond352ac62008-09-29 15:18:18 -04008930 * create a new subvolume directory/inode (helper for the ioctl).
8931 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008932int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008933 struct btrfs_root *new_root,
8934 struct btrfs_root *parent_root,
8935 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008936{
Chris Mason39279cc2007-06-12 06:35:45 -04008937 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008938 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008939 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008940
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008941 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8942 new_dirid, new_dirid,
8943 S_IFDIR | (~current_umask() & S_IRWXUGO),
8944 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008945 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008946 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008947 inode->i_op = &btrfs_dir_inode_operations;
8948 inode->i_fop = &btrfs_dir_file_operations;
8949
Miklos Szeredibfe86842011-10-28 14:13:29 +02008950 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008951 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008952 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008953
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008954 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8955 if (err)
8956 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008957 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008958 new_root->root_key.objectid, err);
8959
Yan, Zheng76dda932009-09-21 16:00:26 -04008960 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008961
Yan, Zheng76dda932009-09-21 16:00:26 -04008962 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008963 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008964}
8965
Chris Mason39279cc2007-06-12 06:35:45 -04008966struct inode *btrfs_alloc_inode(struct super_block *sb)
8967{
8968 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008969 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008970
8971 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8972 if (!ei)
8973 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008974
8975 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008976 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008977 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008978 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008979 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008980 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008981 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008982 ei->disk_i_size = 0;
8983 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008984 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008985 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008986 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008987 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008988 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008989
Josef Bacik9e0baf62011-07-15 15:16:44 +00008990 spin_lock_init(&ei->lock);
8991 ei->outstanding_extents = 0;
8992 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008993
Josef Bacik72ac3c02012-05-23 14:13:11 -04008994 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008995 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008996
Miao Xie16cdcec2011-04-22 18:12:22 +08008997 ei->delayed_node = NULL;
8998
chandan r9cc97d62012-07-04 12:48:07 +05308999 ei->i_otime.tv_sec = 0;
9000 ei->i_otime.tv_nsec = 0;
9001
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009002 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009003 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009004 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9005 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009006 ei->io_tree.track_uptodate = 1;
9007 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009008 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009009 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009010 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009011 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009012 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009013 RB_CLEAR_NODE(&ei->rb_node);
9014
9015 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009016}
9017
Josef Bacikaaedb552013-10-11 14:44:09 -04009018#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9019void btrfs_test_destroy_inode(struct inode *inode)
9020{
9021 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9022 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9023}
9024#endif
9025
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009026static void btrfs_i_callback(struct rcu_head *head)
9027{
9028 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009029 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9030}
9031
Chris Mason39279cc2007-06-12 06:35:45 -04009032void btrfs_destroy_inode(struct inode *inode)
9033{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009034 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009035 struct btrfs_root *root = BTRFS_I(inode)->root;
9036
Al Virob3d9b7a2012-06-09 13:51:19 -04009037 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009038 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009039 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9040 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009041 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9042 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009043 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009044
Chris Mason5a3f23d2009-03-31 13:27:11 -04009045 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009046 * This can happen where we create an inode, but somebody else also
9047 * created the same inode and we need to destroy the one we already
9048 * created.
9049 */
9050 if (!root)
9051 goto free;
9052
Josef Bacik8a35d952012-05-23 14:26:42 -04009053 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9054 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009055 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009056 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009057 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009058 }
Josef Bacik7b128762008-07-24 12:17:14 -04009059
Chris Masond3977122009-01-05 21:25:51 -05009060 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009061 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9062 if (!ordered)
9063 break;
9064 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009065 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009066 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009067 btrfs_remove_ordered_extent(inode, ordered);
9068 btrfs_put_ordered_extent(ordered);
9069 btrfs_put_ordered_extent(ordered);
9070 }
9071 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009072 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009073 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009074free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009075 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009076}
9077
Al Viro45321ac2010-06-07 13:43:19 -04009078int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009079{
9080 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009081
Naohiro Aota6379ef92013-06-06 09:56:34 +00009082 if (root == NULL)
9083 return 1;
9084
Liu Bofa6ac872013-02-20 14:10:23 +00009085 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009086 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009087 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009088 else
Al Viro45321ac2010-06-07 13:43:19 -04009089 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009090}
9091
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009092static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009093{
9094 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9095
9096 inode_init_once(&ei->vfs_inode);
9097}
9098
9099void btrfs_destroy_cachep(void)
9100{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009101 /*
9102 * Make sure all delayed rcu free inodes are flushed before we
9103 * destroy cache.
9104 */
9105 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04009106 if (btrfs_inode_cachep)
9107 kmem_cache_destroy(btrfs_inode_cachep);
9108 if (btrfs_trans_handle_cachep)
9109 kmem_cache_destroy(btrfs_trans_handle_cachep);
9110 if (btrfs_transaction_cachep)
9111 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009112 if (btrfs_path_cachep)
9113 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05009114 if (btrfs_free_space_cachep)
9115 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009116 if (btrfs_delalloc_work_cachep)
9117 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009118}
9119
9120int btrfs_init_cachep(void)
9121{
David Sterba837e1972012-09-07 03:00:48 -06009122 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009123 sizeof(struct btrfs_inode), 0,
9124 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009125 if (!btrfs_inode_cachep)
9126 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009127
David Sterba837e1972012-09-07 03:00:48 -06009128 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009129 sizeof(struct btrfs_trans_handle), 0,
9130 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009131 if (!btrfs_trans_handle_cachep)
9132 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009133
David Sterba837e1972012-09-07 03:00:48 -06009134 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009135 sizeof(struct btrfs_transaction), 0,
9136 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009137 if (!btrfs_transaction_cachep)
9138 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009139
David Sterba837e1972012-09-07 03:00:48 -06009140 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009141 sizeof(struct btrfs_path), 0,
9142 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009143 if (!btrfs_path_cachep)
9144 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009145
David Sterba837e1972012-09-07 03:00:48 -06009146 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009147 sizeof(struct btrfs_free_space), 0,
9148 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
9149 if (!btrfs_free_space_cachep)
9150 goto fail;
9151
Miao Xie8ccf6f192012-10-25 09:28:04 +00009152 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
9153 sizeof(struct btrfs_delalloc_work), 0,
9154 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
9155 NULL);
9156 if (!btrfs_delalloc_work_cachep)
9157 goto fail;
9158
Chris Mason39279cc2007-06-12 06:35:45 -04009159 return 0;
9160fail:
9161 btrfs_destroy_cachep();
9162 return -ENOMEM;
9163}
9164
9165static int btrfs_getattr(struct vfsmount *mnt,
9166 struct dentry *dentry, struct kstat *stat)
9167{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009168 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009169 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009170 u32 blocksize = inode->i_sb->s_blocksize;
9171
Chris Mason39279cc2007-06-12 06:35:45 -04009172 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009173 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05009174 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009175
9176 spin_lock(&BTRFS_I(inode)->lock);
9177 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9178 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009179 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009180 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009181 return 0;
9182}
9183
Chris Masond3977122009-01-05 21:25:51 -05009184static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9185 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009186{
9187 struct btrfs_trans_handle *trans;
9188 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009189 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009190 struct inode *new_inode = d_inode(new_dentry);
9191 struct inode *old_inode = d_inode(old_dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04009192 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009193 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009194 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009195 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009196 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009197
Li Zefan33345d012011-04-20 10:31:50 +08009198 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009199 return -EPERM;
9200
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009201 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009202 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009203 return -EXDEV;
9204
Li Zefan33345d012011-04-20 10:31:50 +08009205 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9206 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009207 return -ENOTEMPTY;
9208
Chris Mason39279cc2007-06-12 06:35:45 -04009209 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009210 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009211 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009212
9213
9214 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009215 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009216 new_dentry->d_name.name,
9217 new_dentry->d_name.len);
9218
9219 if (ret) {
9220 if (ret == -EEXIST) {
9221 /* we shouldn't get
9222 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309223 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009224 return ret;
9225 }
9226 } else {
9227 /* maybe -EOVERFLOW */
9228 return ret;
9229 }
9230 }
9231 ret = 0;
9232
Chris Mason5a3f23d2009-03-31 13:27:11 -04009233 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009234 * we're using rename to replace one file with another. Start IO on it
9235 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009236 */
Chris Mason8d875f92014-08-12 10:47:42 -07009237 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009238 filemap_flush(old_inode->i_mapping);
9239
Yan, Zheng76dda932009-09-21 16:00:26 -04009240 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009241 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009242 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009243 /*
9244 * We want to reserve the absolute worst case amount of items. So if
9245 * both inodes are subvols and we need to unlink them then that would
9246 * require 4 item modifications, but if they are both normal inodes it
9247 * would require 5 item modifications, so we'll assume their normal
9248 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9249 * should cover the worst case number of items we'll modify.
9250 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04009251 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009252 if (IS_ERR(trans)) {
9253 ret = PTR_ERR(trans);
9254 goto out_notrans;
9255 }
Chris Mason5f39d392007-10-15 16:14:19 -04009256
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009257 if (dest != root)
9258 btrfs_record_root_in_trans(trans, dest);
9259
Yan, Zhenga5719522009-09-24 09:17:31 -04009260 ret = btrfs_set_inode_index(new_dir, &index);
9261 if (ret)
9262 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009263
Miao Xie67de1172013-12-26 13:07:06 +08009264 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009265 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009266 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009267 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009268 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009269 ret = btrfs_insert_inode_ref(trans, dest,
9270 new_dentry->d_name.name,
9271 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009272 old_ino,
9273 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009274 if (ret)
9275 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009276 /*
9277 * this is an ugly little race, but the rename is required
9278 * to make sure that if we crash, the inode is either at the
9279 * old name or the new one. pinning the log transaction lets
9280 * us make sure we don't allow a log commit to come in after
9281 * we unlink the name but before we add the new name back in.
9282 */
9283 btrfs_pin_log_trans(root);
9284 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009285
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009286 inode_inc_iversion(old_dir);
9287 inode_inc_iversion(new_dir);
9288 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009289 old_dir->i_ctime = old_dir->i_mtime = ctime;
9290 new_dir->i_ctime = new_dir->i_mtime = ctime;
9291 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04009292
Chris Mason12fcfd22009-03-24 10:24:20 -04009293 if (old_dentry->d_parent != new_dentry->d_parent)
9294 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9295
Li Zefan33345d012011-04-20 10:31:50 +08009296 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009297 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9298 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9299 old_dentry->d_name.name,
9300 old_dentry->d_name.len);
9301 } else {
Al Viro92986792011-03-04 17:14:37 +00009302 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009303 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009304 old_dentry->d_name.name,
9305 old_dentry->d_name.len);
9306 if (!ret)
9307 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009308 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009309 if (ret) {
9310 btrfs_abort_transaction(trans, root, ret);
9311 goto out_fail;
9312 }
Chris Mason39279cc2007-06-12 06:35:45 -04009313
9314 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009315 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009316 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08009317 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009318 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9319 root_objectid = BTRFS_I(new_inode)->location.objectid;
9320 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9321 root_objectid,
9322 new_dentry->d_name.name,
9323 new_dentry->d_name.len);
9324 BUG_ON(new_inode->i_nlink == 0);
9325 } else {
9326 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009327 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009328 new_dentry->d_name.name,
9329 new_dentry->d_name.len);
9330 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009331 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009332 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009333 if (ret) {
9334 btrfs_abort_transaction(trans, root, ret);
9335 goto out_fail;
9336 }
Chris Mason39279cc2007-06-12 06:35:45 -04009337 }
Josef Bacikaec74772008-07-24 12:12:38 -04009338
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009339 ret = btrfs_add_link(trans, new_dir, old_inode,
9340 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009341 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009342 if (ret) {
9343 btrfs_abort_transaction(trans, root, ret);
9344 goto out_fail;
9345 }
Chris Mason39279cc2007-06-12 06:35:45 -04009346
Miao Xie67de1172013-12-26 13:07:06 +08009347 if (old_inode->i_nlink == 1)
9348 BTRFS_I(old_inode)->dir_index = index;
9349
Li Zefan33345d012011-04-20 10:31:50 +08009350 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04009351 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00009352 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009353 btrfs_end_log_trans(root);
9354 }
Chris Mason39279cc2007-06-12 06:35:45 -04009355out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009356 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009357out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009358 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009359 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009360
Chris Mason39279cc2007-06-12 06:35:45 -04009361 return ret;
9362}
9363
Miklos Szeredi80ace852014-07-23 15:15:32 +02009364static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9365 struct inode *new_dir, struct dentry *new_dentry,
9366 unsigned int flags)
9367{
9368 if (flags & ~RENAME_NOREPLACE)
9369 return -EINVAL;
9370
9371 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9372}
9373
Miao Xie8ccf6f192012-10-25 09:28:04 +00009374static void btrfs_run_delalloc_work(struct btrfs_work *work)
9375{
9376 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009377 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009378
9379 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9380 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009381 inode = delalloc_work->inode;
9382 if (delalloc_work->wait) {
9383 btrfs_wait_ordered_range(inode, 0, (u64)-1);
9384 } else {
9385 filemap_flush(inode->i_mapping);
9386 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9387 &BTRFS_I(inode)->runtime_flags))
9388 filemap_flush(inode->i_mapping);
9389 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00009390
9391 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009392 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009393 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009394 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009395 complete(&delalloc_work->completion);
9396}
9397
9398struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9399 int wait, int delay_iput)
9400{
9401 struct btrfs_delalloc_work *work;
9402
9403 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
9404 if (!work)
9405 return NULL;
9406
9407 init_completion(&work->completion);
9408 INIT_LIST_HEAD(&work->list);
9409 work->inode = inode;
9410 work->wait = wait;
9411 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009412 WARN_ON_ONCE(!inode);
9413 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9414 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009415
9416 return work;
9417}
9418
9419void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9420{
9421 wait_for_completion(&work->completion);
9422 kmem_cache_free(btrfs_delalloc_work_cachep, work);
9423}
9424
Chris Masond352ac62008-09-29 15:18:18 -04009425/*
9426 * some fairly slow code that needs optimization. This walks the list
9427 * of all the inodes with pending delalloc and forces them to disk.
9428 */
Miao Xie6c255e62014-03-06 13:55:01 +08009429static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9430 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009431{
Chris Masonea8c2812008-08-04 23:17:27 -04009432 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009433 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009434 struct btrfs_delalloc_work *work, *next;
9435 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009436 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009437 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009438
Miao Xie8ccf6f192012-10-25 09:28:04 +00009439 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009440 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009441
Miao Xie573bfb72014-03-06 13:55:03 +08009442 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009443 spin_lock(&root->delalloc_lock);
9444 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009445 while (!list_empty(&splice)) {
9446 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009447 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009448
Miao Xieeb73c1b2013-05-15 07:48:22 +00009449 list_move_tail(&binode->delalloc_inodes,
9450 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009451 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009452 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009453 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009454 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009455 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009456 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009457
9458 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009459 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009460 if (delay_iput)
9461 btrfs_add_delayed_iput(inode);
9462 else
9463 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009464 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009465 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009466 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009467 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009468 btrfs_queue_work(root->fs_info->flush_workers,
9469 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009470 ret++;
9471 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009472 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009473 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009474 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009475 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009476 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009477
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009478out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009479 list_for_each_entry_safe(work, next, &works, list) {
9480 list_del_init(&work->list);
9481 btrfs_wait_and_free_delalloc_work(work);
9482 }
9483
Miao Xieeb73c1b2013-05-15 07:48:22 +00009484 if (!list_empty_careful(&splice)) {
9485 spin_lock(&root->delalloc_lock);
9486 list_splice_tail(&splice, &root->delalloc_inodes);
9487 spin_unlock(&root->delalloc_lock);
9488 }
Miao Xie573bfb72014-03-06 13:55:03 +08009489 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009490 return ret;
9491}
9492
9493int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9494{
9495 int ret;
9496
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009497 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009498 return -EROFS;
9499
Miao Xie6c255e62014-03-06 13:55:01 +08009500 ret = __start_delalloc_inodes(root, delay_iput, -1);
9501 if (ret > 0)
9502 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009503 /*
9504 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009505 * we have to make sure the IO is actually started and that
9506 * ordered extents get created before we return
9507 */
9508 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009509 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009510 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009511 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009512 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9513 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009514 }
9515 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009516 return ret;
9517}
9518
Miao Xie6c255e62014-03-06 13:55:01 +08009519int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9520 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009521{
9522 struct btrfs_root *root;
9523 struct list_head splice;
9524 int ret;
9525
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009526 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009527 return -EROFS;
9528
9529 INIT_LIST_HEAD(&splice);
9530
Miao Xie573bfb72014-03-06 13:55:03 +08009531 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009532 spin_lock(&fs_info->delalloc_root_lock);
9533 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009534 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009535 root = list_first_entry(&splice, struct btrfs_root,
9536 delalloc_root);
9537 root = btrfs_grab_fs_root(root);
9538 BUG_ON(!root);
9539 list_move_tail(&root->delalloc_root,
9540 &fs_info->delalloc_roots);
9541 spin_unlock(&fs_info->delalloc_root_lock);
9542
Miao Xie6c255e62014-03-06 13:55:01 +08009543 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009544 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009545 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009546 goto out;
9547
Miao Xie6c255e62014-03-06 13:55:01 +08009548 if (nr != -1) {
9549 nr -= ret;
9550 WARN_ON(nr < 0);
9551 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009552 spin_lock(&fs_info->delalloc_root_lock);
9553 }
9554 spin_unlock(&fs_info->delalloc_root_lock);
9555
Miao Xie6c255e62014-03-06 13:55:01 +08009556 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009557 atomic_inc(&fs_info->async_submit_draining);
9558 while (atomic_read(&fs_info->nr_async_submits) ||
9559 atomic_read(&fs_info->async_delalloc_pages)) {
9560 wait_event(fs_info->async_submit_wait,
9561 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9562 atomic_read(&fs_info->async_delalloc_pages) == 0));
9563 }
9564 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009565out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009566 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009567 spin_lock(&fs_info->delalloc_root_lock);
9568 list_splice_tail(&splice, &fs_info->delalloc_roots);
9569 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009570 }
Miao Xie573bfb72014-03-06 13:55:03 +08009571 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009572 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009573}
9574
Chris Mason39279cc2007-06-12 06:35:45 -04009575static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9576 const char *symname)
9577{
9578 struct btrfs_trans_handle *trans;
9579 struct btrfs_root *root = BTRFS_I(dir)->root;
9580 struct btrfs_path *path;
9581 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009582 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009583 int err;
9584 int drop_inode = 0;
9585 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309586 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009587 int name_len;
9588 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009589 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009590 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009591 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009592
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009593 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009594 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9595 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009596
Josef Bacik9ed74f22009-09-11 16:12:44 -04009597 /*
9598 * 2 items for inode item and ref
9599 * 2 items for dir items
9600 * 1 item for xattr if selinux is on
9601 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009602 trans = btrfs_start_transaction(root, 5);
9603 if (IS_ERR(trans))
9604 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009605
Li Zefan581bb052011-04-20 10:06:11 +08009606 err = btrfs_find_free_ino(root, &objectid);
9607 if (err)
9608 goto out_unlock;
9609
Josef Bacikaec74772008-07-24 12:12:38 -04009610 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009611 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04009612 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009613 if (IS_ERR(inode)) {
9614 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009615 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009616 }
Chris Mason39279cc2007-06-12 06:35:45 -04009617
Casey Schauflerad19db72011-12-15 10:09:07 -05009618 /*
9619 * If the active LSM wants to access the inode during
9620 * d_instantiate it needs these. Smack checks to see
9621 * if the filesystem supports xattrs by looking at the
9622 * ops vector.
9623 */
9624 inode->i_fop = &btrfs_file_operations;
9625 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009626 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009627 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9628
9629 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9630 if (err)
9631 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009632
Josef Bacika1b075d2010-11-19 20:36:11 +00009633 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009634 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009635 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009636
9637 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009638 if (!path) {
9639 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009640 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009641 }
Li Zefan33345d012011-04-20 10:31:50 +08009642 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009643 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009644 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009645 datasize = btrfs_file_extent_calc_inline_size(name_len);
9646 err = btrfs_insert_empty_item(trans, root, path, &key,
9647 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009648 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009649 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009650 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009651 }
Chris Mason5f39d392007-10-15 16:14:19 -04009652 leaf = path->nodes[0];
9653 ei = btrfs_item_ptr(leaf, path->slots[0],
9654 struct btrfs_file_extent_item);
9655 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9656 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009657 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009658 btrfs_set_file_extent_encryption(leaf, ei, 0);
9659 btrfs_set_file_extent_compression(leaf, ei, 0);
9660 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9661 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9662
Chris Mason39279cc2007-06-12 06:35:45 -04009663 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009664 write_extent_buffer(leaf, symname, ptr, name_len);
9665 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009666 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009667
Chris Mason39279cc2007-06-12 06:35:45 -04009668 inode->i_op = &btrfs_symlink_inode_operations;
9669 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -04009670 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009671 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009672 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009673 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009674 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009675 goto out_unlock_inode;
9676 }
9677
9678 unlock_new_inode(inode);
9679 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009680
9681out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009682 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009683 if (drop_inode) {
9684 inode_dec_link_count(inode);
9685 iput(inode);
9686 }
Liu Bob53d3f52012-11-14 14:34:34 +00009687 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009688 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009689
9690out_unlock_inode:
9691 drop_inode = 1;
9692 unlock_new_inode(inode);
9693 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009694}
Chris Mason16432982008-04-10 10:23:21 -04009695
Josef Bacik0af3d002010-06-21 14:48:16 -04009696static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9697 u64 start, u64 num_bytes, u64 min_size,
9698 loff_t actual_len, u64 *alloc_hint,
9699 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009700{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009701 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9702 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009703 struct btrfs_root *root = BTRFS_I(inode)->root;
9704 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009705 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009706 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009707 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009708 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009709 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009710
Josef Bacik0af3d002010-06-21 14:48:16 -04009711 if (trans)
9712 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009713 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009714 if (own_trans) {
9715 trans = btrfs_start_transaction(root, 3);
9716 if (IS_ERR(trans)) {
9717 ret = PTR_ERR(trans);
9718 break;
9719 }
Yan Zhengd899e052008-10-30 14:25:28 -04009720 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009721
Chris Mason154ea282013-03-05 11:11:26 -05009722 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9723 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009724 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009725 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009726 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009727 if (own_trans)
9728 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009729 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009730 }
9731
Yan Zhengd899e052008-10-30 14:25:28 -04009732 ret = insert_reserved_file_extent(trans, inode,
9733 cur_offset, ins.objectid,
9734 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009735 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009736 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009737 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009738 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009739 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009740 btrfs_abort_transaction(trans, root, ret);
9741 if (own_trans)
9742 btrfs_end_transaction(trans, root);
9743 break;
9744 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009745
Chris Masona1ed8352009-09-11 12:27:37 -04009746 btrfs_drop_extent_cache(inode, cur_offset,
9747 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009748
Josef Bacik5dc562c2012-08-17 13:14:17 -04009749 em = alloc_extent_map();
9750 if (!em) {
9751 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9752 &BTRFS_I(inode)->runtime_flags);
9753 goto next;
9754 }
9755
9756 em->start = cur_offset;
9757 em->orig_start = cur_offset;
9758 em->len = ins.offset;
9759 em->block_start = ins.objectid;
9760 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009761 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009762 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009763 em->bdev = root->fs_info->fs_devices->latest_bdev;
9764 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9765 em->generation = trans->transid;
9766
9767 while (1) {
9768 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009769 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009770 write_unlock(&em_tree->lock);
9771 if (ret != -EEXIST)
9772 break;
9773 btrfs_drop_extent_cache(inode, cur_offset,
9774 cur_offset + ins.offset - 1,
9775 0);
9776 }
9777 free_extent_map(em);
9778next:
Yan Zhengd899e052008-10-30 14:25:28 -04009779 num_bytes -= ins.offset;
9780 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009781 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009782
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009783 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009784 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009785 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009786 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009787 (actual_len > inode->i_size) &&
9788 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009789 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009790 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009791 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009792 i_size = cur_offset;
9793 i_size_write(inode, i_size);
9794 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009795 }
9796
Yan Zhengd899e052008-10-30 14:25:28 -04009797 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009798
9799 if (ret) {
9800 btrfs_abort_transaction(trans, root, ret);
9801 if (own_trans)
9802 btrfs_end_transaction(trans, root);
9803 break;
9804 }
Yan Zhengd899e052008-10-30 14:25:28 -04009805
Josef Bacik0af3d002010-06-21 14:48:16 -04009806 if (own_trans)
9807 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009808 }
Yan Zhengd899e052008-10-30 14:25:28 -04009809 return ret;
9810}
9811
Josef Bacik0af3d002010-06-21 14:48:16 -04009812int btrfs_prealloc_file_range(struct inode *inode, int mode,
9813 u64 start, u64 num_bytes, u64 min_size,
9814 loff_t actual_len, u64 *alloc_hint)
9815{
9816 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9817 min_size, actual_len, alloc_hint,
9818 NULL);
9819}
9820
9821int btrfs_prealloc_file_range_trans(struct inode *inode,
9822 struct btrfs_trans_handle *trans, int mode,
9823 u64 start, u64 num_bytes, u64 min_size,
9824 loff_t actual_len, u64 *alloc_hint)
9825{
9826 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9827 min_size, actual_len, alloc_hint, trans);
9828}
9829
Chris Masone6dcd2d2008-07-17 12:53:50 -04009830static int btrfs_set_page_dirty(struct page *page)
9831{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009832 return __set_page_dirty_nobuffers(page);
9833}
9834
Al Viro10556cb22011-06-20 19:28:19 -04009835static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009836{
Li Zefanb83cc962010-12-20 16:04:08 +08009837 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009838 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009839
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009840 if (mask & MAY_WRITE &&
9841 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9842 if (btrfs_root_readonly(root))
9843 return -EROFS;
9844 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9845 return -EACCES;
9846 }
Al Viro2830ba72011-06-20 19:16:29 -04009847 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009848}
Chris Mason39279cc2007-06-12 06:35:45 -04009849
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009850static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9851{
9852 struct btrfs_trans_handle *trans;
9853 struct btrfs_root *root = BTRFS_I(dir)->root;
9854 struct inode *inode = NULL;
9855 u64 objectid;
9856 u64 index;
9857 int ret = 0;
9858
9859 /*
9860 * 5 units required for adding orphan entry
9861 */
9862 trans = btrfs_start_transaction(root, 5);
9863 if (IS_ERR(trans))
9864 return PTR_ERR(trans);
9865
9866 ret = btrfs_find_free_ino(root, &objectid);
9867 if (ret)
9868 goto out;
9869
9870 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9871 btrfs_ino(dir), objectid, mode, &index);
9872 if (IS_ERR(inode)) {
9873 ret = PTR_ERR(inode);
9874 inode = NULL;
9875 goto out;
9876 }
9877
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009878 inode->i_fop = &btrfs_file_operations;
9879 inode->i_op = &btrfs_file_inode_operations;
9880
9881 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009882 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9883
Chris Masonb0d5d102014-09-08 13:08:51 -07009884 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9885 if (ret)
9886 goto out_inode;
9887
9888 ret = btrfs_update_inode(trans, root, inode);
9889 if (ret)
9890 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009891 ret = btrfs_orphan_add(trans, inode);
9892 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009893 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009894
Filipe Manana5762b5c2014-08-01 00:10:32 +01009895 /*
9896 * We set number of links to 0 in btrfs_new_inode(), and here we set
9897 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9898 * through:
9899 *
9900 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9901 */
9902 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009903 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009904 d_tmpfile(dentry, inode);
9905 mark_inode_dirty(inode);
9906
9907out:
9908 btrfs_end_transaction(trans, root);
9909 if (ret)
9910 iput(inode);
9911 btrfs_balance_delayed_items(root);
9912 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009913 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009914
9915out_inode:
9916 unlock_new_inode(inode);
9917 goto out;
9918
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009919}
9920
Filipe Mananab38ef712014-11-13 17:01:45 +00009921/* Inspired by filemap_check_errors() */
9922int btrfs_inode_check_errors(struct inode *inode)
9923{
9924 int ret = 0;
9925
9926 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
9927 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
9928 ret = -ENOSPC;
9929 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
9930 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
9931 ret = -EIO;
9932
9933 return ret;
9934}
9935
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009936static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009937 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009938 .lookup = btrfs_lookup,
9939 .create = btrfs_create,
9940 .unlink = btrfs_unlink,
9941 .link = btrfs_link,
9942 .mkdir = btrfs_mkdir,
9943 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009944 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009945 .symlink = btrfs_symlink,
9946 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009947 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009948 .setxattr = btrfs_setxattr,
9949 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009950 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009951 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009952 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009953 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009954 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009955 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009956 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009957};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009958static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009959 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009960 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009961 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009962 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009963 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009964};
Yan, Zheng76dda932009-09-21 16:00:26 -04009965
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009966static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009967 .llseek = generic_file_llseek,
9968 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009969 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009970 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009971#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009972 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009973#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009974 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009975 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009976};
9977
Chris Masond1310b22008-01-24 16:13:08 -05009978static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009979 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009980 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009981 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009982 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009983 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009984 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009985 .set_bit_hook = btrfs_set_bit_hook,
9986 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009987 .merge_extent_hook = btrfs_merge_extent_hook,
9988 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009989};
9990
Chris Mason35054392009-01-21 13:11:13 -05009991/*
9992 * btrfs doesn't support the bmap operation because swapfiles
9993 * use bmap to make a mapping of extents in the file. They assume
9994 * these extents won't change over the life of the file and they
9995 * use the bmap result to do IO directly to the drive.
9996 *
9997 * the btrfs bmap call would return logical addresses that aren't
9998 * suitable for IO and they also will change frequently as COW
9999 * operations happen. So, swapfile + btrfs == corruption.
10000 *
10001 * For now we're avoiding this by dropping bmap.
10002 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010003static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010004 .readpage = btrfs_readpage,
10005 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010006 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010007 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010008 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010009 .invalidatepage = btrfs_invalidatepage,
10010 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010011 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010012 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010013};
10014
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010015static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010016 .readpage = btrfs_readpage,
10017 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010018 .invalidatepage = btrfs_invalidatepage,
10019 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010020};
10021
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010022static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010023 .getattr = btrfs_getattr,
10024 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010025 .setxattr = btrfs_setxattr,
10026 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010027 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010028 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -050010029 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010030 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010031 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010032 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010033 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010034};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010035static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010036 .getattr = btrfs_getattr,
10037 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010038 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010039 .setxattr = btrfs_setxattr,
10040 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -040010041 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -040010042 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010043 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010044 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010045 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010046};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010047static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010048 .readlink = generic_readlink,
10049 .follow_link = page_follow_link_light,
10050 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +000010051 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010052 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010053 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010054 .setxattr = btrfs_setxattr,
10055 .getxattr = btrfs_getxattr,
10056 .listxattr = btrfs_listxattr,
10057 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010058 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010059};
Yan, Zheng76dda932009-09-21 16:00:26 -040010060
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010061const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010062 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010063 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010064};